mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +00:00
day 10
This commit is contained in:
parent
d447c97f4b
commit
20bcd88600
4 changed files with 160 additions and 15 deletions
|
@ -145,16 +145,16 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "782",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 6.147158
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "1694",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 4.868614
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
18
README.md
18
README.md
|
@ -20,7 +20,7 @@
|
|||
[](src/day07)
|
||||
[](src/day08)
|
||||
[](src/day09)
|
||||

|
||||
[](src/day10)
|
||||

|
||||

|
||||

|
||||
|
@ -125,16 +125,16 @@ Both parts: 1.863ms
|
|||
|
||||
```
|
||||
Day 09
|
||||
Time part 1: 239.635ms
|
||||
Time part 2: 53.494ms
|
||||
Both parts: 293.129ms
|
||||
Time part 1: 236.521ms
|
||||
Time part 2: 51.928ms
|
||||
Both parts: 288.449ms
|
||||
```
|
||||
|
||||
```
|
||||
Day 10
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 6.148ms
|
||||
Time part 2: 4.028ms
|
||||
Both parts: 10.176ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 18/50
|
||||
Total time: 38378.002ms
|
||||
Total stars: 20/50
|
||||
Total time: 38383.499ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day10/README.md
Normal file
9
src/day10/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 10 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/10)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
136
src/day10/index.ts
Normal file
136
src/day10/index.ts
Normal file
|
@ -0,0 +1,136 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput: string) => rawInput.split('\n').map(line => line.split('').map(Number))
|
||||
|
||||
const directions = [
|
||||
[-1, 0], // up
|
||||
[0, 1], // right
|
||||
[1, 0], // down
|
||||
[0, -1] // left
|
||||
]
|
||||
|
||||
const sum = (a,b) => a + b
|
||||
|
||||
const getKey = (r: number, c: number) => `${r},${c}`
|
||||
type Key = ReturnType<typeof getKey>
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
const trailheads: Key[] = []
|
||||
const exitsMap = new Map<Key, Key[]>()
|
||||
const get = (r: number, c: number) => {
|
||||
return (input[r] ?? [])[c]
|
||||
}
|
||||
input.forEach((row, r) => {
|
||||
row.forEach((alt, c) => {
|
||||
const key = getKey(r, c)
|
||||
if (alt === 0) {
|
||||
trailheads.push(key)
|
||||
}
|
||||
const outs: Key[] = []
|
||||
directions.forEach(([dr, dc]) => {
|
||||
if (get(r + dr, c + dc) === alt + 1) {
|
||||
outs.push(getKey(r + dr, c + dc))
|
||||
}
|
||||
})
|
||||
exitsMap.set(key, outs)
|
||||
})
|
||||
})
|
||||
const reachablePeaks = new Map<Key, Set<Key>>()
|
||||
const hike = (key: Key, alt: number = 0): Set<Key> => {
|
||||
let ans = reachablePeaks.get(key)
|
||||
if (ans !== undefined) return ans
|
||||
ans = new Set<Key>()
|
||||
if (alt === 9) {
|
||||
ans.add(key)
|
||||
reachablePeaks.set(key, ans)
|
||||
return ans
|
||||
}
|
||||
const branches = (exitsMap.get(key) ?? []).map(k => hike(k,alt+1))
|
||||
branches.forEach(peaks => {
|
||||
peaks.forEach(peak => ans.add(peak))
|
||||
})
|
||||
reachablePeaks.set(key, ans)
|
||||
return ans
|
||||
}
|
||||
return trailheads.map(t => hike(t).size).reduce(sum, 0)
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
const trailheads: Key[] = []
|
||||
const exitsMap = new Map<Key, Key[]>()
|
||||
const get = (r: number, c: number) => {
|
||||
return (input[r] ?? [])[c]
|
||||
}
|
||||
input.forEach((row, r) => {
|
||||
row.forEach((alt, c) => {
|
||||
const key = getKey(r, c)
|
||||
if (alt === 0) {
|
||||
trailheads.push(key)
|
||||
}
|
||||
const outs: Key[] = []
|
||||
directions.forEach(([dr, dc]) => {
|
||||
if (get(r + dr, c + dc) === alt + 1) {
|
||||
outs.push(getKey(r + dr, c + dc))
|
||||
}
|
||||
})
|
||||
exitsMap.set(key, outs)
|
||||
})
|
||||
})
|
||||
const cache = new Map<Key, number>()
|
||||
const hike = (key: Key, alt: number): number => {
|
||||
if (alt === 9) {
|
||||
return 1
|
||||
}
|
||||
let ans = cache.get(key)
|
||||
if (ans !== undefined) return ans
|
||||
const exitTotals = (exitsMap.get(key) ?? []).map(k => hike(k,alt+1))
|
||||
ans = exitTotals.reduce(sum, 0)
|
||||
cache.set(key, ans)
|
||||
return ans
|
||||
}
|
||||
return trailheads.map(t => hike(t,0)).reduce(sum, 0)
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `0123
|
||||
1234
|
||||
8765
|
||||
9876`,
|
||||
expected: 1,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `012345
|
||||
123456
|
||||
234567
|
||||
345678
|
||||
4.6789
|
||||
56789.`,
|
||||
expected: 227
|
||||
},
|
||||
{
|
||||
input: `89010123
|
||||
78121874
|
||||
87430965
|
||||
96549874
|
||||
45678903
|
||||
32019012
|
||||
01329801
|
||||
10456732`,
|
||||
expected: 81,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue