Day 1
prelude
https://adventofcode.com/2024/day/1
The input I'm using here has been modified to be different from my actual problem input, since the puzzle author has requested that we not commit our actual inputs.
I'm going to use some utilities I defined to make opening the file easier. inputDay
opens the day's input file, splits it into lines, splits the lines by spaces, and converts anything to numbers that can be converted to numbers. transpose
transposes a 2d array's columns into rows.
import { inputDay, transpose } from "./lib/utilities.js"
Parse the input file, transpose and sort it, and map that to a pair of arrays first
and second
.
const [first, second] = transpose(await inputDay(1)).map(row => row.sort())
How are the numbers distributed?
How about the differences?
part 1
Sum the absolute value of the differences between the columns
first.map((f, i) => Math.abs(f - second[i])).reduce((a, b) => a + b)
part 2
Sum the first column and the count of values in the second column
first
// we don't need to be smart to count the values, the array is small enough
// that we can be dumb and just count by brute force
.map(f => f * second.filter(x => x == f).length)
.reduce((a, b) => a + b)