mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 09:06:09 +00:00
day 1
This commit is contained in:
commit
d54c4c67a2
14 changed files with 2376 additions and 0 deletions
9
src/day01/README.md
Normal file
9
src/day01/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 1 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/1)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
64
src/day01/index.ts
Normal file
64
src/day01/index.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput: string) => {
|
||||
const rows = rawInput.split('\n').map(line => line.split(/\s+/).map(Number))
|
||||
let lists: number[][] = [[], []]
|
||||
rows.forEach(row => {
|
||||
row.forEach((value, index) => lists[index].push(value))
|
||||
})
|
||||
return lists
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
input.forEach(list => list.sort())
|
||||
let total = 0
|
||||
for (let i = 0; i < input[0].length; i++) {
|
||||
total += Math.abs(input[0][i] - input[1][i])
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const [left, right] = parseInput(rawInput)
|
||||
right.sort()
|
||||
const counts = new Map<number, number>()
|
||||
right.forEach(entry => {
|
||||
counts.set(entry, (counts.get(entry) ?? 0) + 1)
|
||||
})
|
||||
const total = left.reduce((acc, cur) => { return acc + cur * (counts.get(cur) ?? 0)}, 0)
|
||||
return total
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3`,
|
||||
expected: 11,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3`,
|
||||
expected: 31,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue