Advent of Code 2021 - Day 1

As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine.

The problem is here.

Day 1a

export const d1a = ({input}: DayProps) =>
  R.pipe(
    input,
    s => s.trim().split(/\s+/),
    R.map(parseInt),
    R.reduce.indexed((n, d, i, ds) => n + Number(i && d > ds[i - 1]), 0),
  ).toString()

R in this case is Remeda, which is like Ramda but tuned for TypeScript.

Interesting stuff:

  • Using R.map instead of Array.prototype.map avoids the whole issue of parseInt taking a radix parameter. Remeda will only pass a single argument to R.map; if you want more, you have to use R.map.indexed.

  • Converting booleans to numbers is remarkably soothing. Think of the keypresses saved.

Here’s the result with the sample input:

and here’s the result with my own input:

Day 1b

The problem statement says that we’re using a sliding window to mitigate noise in the input. The prescribed implementation uses a sum over the window, though, so in reality it’s just comparing the start of the previous window with the end of the new window:

Window 1: A + B + C
Window 2: B + C + D

(B + C + D) > (A + B + C) ≡ D > A

Apart from dbg (will explain below), the only difference from the first part is the index for comparison:

export const d1b = ({input, dbg}: DayProps) =>
  R.pipe(
    input,
    s => s.trim().split(/\s+/),
    R.map(parseInt),
    R.reduce.indexed(
      (n, d, i, ds) =>
        n + Number(i > 2 && d > ds[i - 3] && (dbg(d, '(increased)'), true)),
      0,
    ),
  ).toString()

Here’s the result with the sample input, with debugging enabled:

and here’s the result with my own input: