AOC Day 1 2021

This is part of a greater series about solving advent of code with spreadsheets.

Part One

Read part one from the advent of code website.0 The input is a line-separated list of numbers. The goal is to count how many numbers are greater than the previous one.

(0) This is the easiest way for me to navigate Advent of Code copyright.

Solving Part One

This challenge was easy with spreadsheets.1

(1) This was a confidence I came to regret.
DATA A2 A3 ...
INCREASE - B3 = A3 > A2 repeat →
OUTPUT C3 =
COUNTIF(B3:B,"=TRUE")

I’ve omitted row numbers and columns and just done them inline. I think this format is more readable.

This solution scaled well to the challenge input of 2000 lines.

How Part One Works

The first spreadsheet of this series is worth explaining in full detail.

About the Colors

Generally, each column is like a “stage” of the program. Some stages rely on previous ones. To make these connections clear, columns and their formula references are colored2.

(2) The colors themselves come from Google Sheets. I ripped them using OSX’s Digital Color Meter.

Repeat

Formulas reference other cells by column (named with a letter A-Z), and by row (a number 1-2000). To copy formulas to other cells the user “drags”3 that cell over the row or column. When copied like this the cell formula updates to keep the same “relative” references.

(3) Power users know to double click instead of drag. This will copy the formula to the entire column as long as there’s data to the left. This series forced me to learn.

For example …

1 2 3
A DATA 199 399
B HEADER B2 = A2+1 =>
200+1 =>
201
B3 = A3+1 =>
399+1 =>
400
C HEADER C2 = B2+1 =>
201+1 =>
202

There is a way to instruct Google Sheets to keep a part of the cell reference absolute $. This isn’t used in this example.

Transposing Rows and Columns

Data is often pasted into thousands of rows. This isn’t as interesting as the formulas unique to each column. Additionally, with many columns the table can get quite wide. For these reasons I format tables with columns running along the top to bottom axis. Rows run from the left to right.

For example, a table looking like this in Google Sheets …

A B C ...
1 HEADER1 HEADER2 HEADER3 ...
2 A2 B2 C2 ...
3 A3 B3 C3 ...
... ... ... ... ...

Will look like this in this series …

1 2 3 ...
A HEADER1 A2 A3 ...
B HEADER2 B2 B3 ...
C HEADER3 C2 C3 ...
... ... ... ... ...
Boolean Values

Cells can have non-numeric data types4. My advent of code solutions rely mostly on numbers and booleans.

(4) Types are finicky.

Operators like >, <, and =5 produce booleans. In the INCREASE column I use > to compare the current number to the previous number.

(5) Only one equal sign! It is not == like in a lot of programming languages.
Formulas on Ranges

Cell ranges are continuous blocks of cells. To reference …

(6) Cell ranges are inclusive.

Some spreadsheet functions can act on cell ranges. COUNTIF counts the number of items in a range matching a “criterion”. Some example string criteria are …

Putting It All Together

Part Two

Part two asks7 to count the number of 3-wide sliding windows with a sum greater than the previous one.

(7) Part two isn’t readable from the website until you do part one.

To understand 3-wide sliding windows think …

Solving Part Two

This requires one extra column.

DATA A2 A3 A4 A5 ...
3SUM8 - - B4 =
A2+A3+A4
B5 =
A3+A4+A5
repeat →
INCREASE - - - C5 =
B5 > B4
repeat →
OUTPUT D2 =
COUNTIF(C5:C)
(8) You can simplify away the 3SUM column. Do the sum inline in the INCREASE column A3 + A4 + A5 > A2 + A3 + A4.

You may be interested in the next day's solution.