mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +00:00
day 18, thank you Djikstra
This commit is contained in:
parent
656034e39f
commit
76aecbc04a
4 changed files with 137 additions and 19 deletions
|
@ -269,16 +269,19 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "334",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 27.725968
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"solved": true,
|
||||
"result": "20,12",
|
||||
"attempts": [
|
||||
"2915",
|
||||
"40,41"
|
||||
],
|
||||
"time": 15841.686313
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
24
README.md
24
README.md
|
@ -28,7 +28,7 @@
|
|||
[](src/day15)
|
||||
[](src/day16)
|
||||
[](src/day17)
|
||||

|
||||
[](src/day18)
|
||||

|
||||

|
||||

|
||||
|
@ -160,9 +160,9 @@ Both parts: 1.913ms
|
|||
|
||||
```
|
||||
Day 14
|
||||
Time part 1: 1.492ms
|
||||
Time part 2: -
|
||||
Both parts: 1.492ms
|
||||
Time part 1: 0.764ms
|
||||
Time part 2: 826.27ms
|
||||
Both parts: 827.034ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -181,16 +181,16 @@ Both parts: 132.546ms
|
|||
|
||||
```
|
||||
Day 17
|
||||
Time part 1: -
|
||||
Time part 2: 8693.239ms
|
||||
Both parts: 8693.239ms
|
||||
Time part 1: 0.057ms
|
||||
Time part 2: 8772.936ms
|
||||
Both parts: 8772.994ms
|
||||
```
|
||||
|
||||
```
|
||||
Day 18
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 26.466ms
|
||||
Time part 2: 1115.347ms
|
||||
Both parts: 1141.812ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 33/50
|
||||
Total time: 159534.453ms
|
||||
Total stars: 36/50
|
||||
Total time: 161581.562ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day18/README.md
Normal file
9
src/day18/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 18 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/18)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
106
src/day18/index.ts
Normal file
106
src/day18/index.ts
Normal file
|
@ -0,0 +1,106 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const ints = (line: string) => [...line.matchAll(/\d+/g)].map(Number)
|
||||
const parseInput = (rawInput: string) => rawInput.split('\n').map(ints)
|
||||
|
||||
const deltas: [Coord, Coord, Coord, Coord] = [
|
||||
[ 0, 1], // right
|
||||
[ 1, 0], // down
|
||||
[ 0, -1], // left
|
||||
[-1, 0], // up
|
||||
]
|
||||
|
||||
type Coord = [number, number]
|
||||
|
||||
const bold = (text: string) => {
|
||||
return `\x1b[44;37;1m${text}\x1b[0m`
|
||||
}
|
||||
|
||||
type Step = {
|
||||
r: number
|
||||
c: number
|
||||
cost: number
|
||||
prev: Step | null
|
||||
}
|
||||
|
||||
const MAP_SIZE = 70
|
||||
const getKey = ([r, c]: [number, number]) => (MAP_SIZE + 1) * r + c
|
||||
type Key = ReturnType<typeof getKey>
|
||||
|
||||
const navigate = (input: number[][], bytesDropped): number => {
|
||||
// start with the corrupted cells as seen. BFS will skip them
|
||||
const seen = new Set(input.slice(0,bytesDropped).map(getKey))
|
||||
const start: Step = { r: 0, c: 0, cost: 0, prev: null }
|
||||
const queue: Step[] = [start]
|
||||
while (true) {
|
||||
process.stdout.write(`\rSeen: ${seen.size} cells `)
|
||||
const cur = queue.shift()
|
||||
if (cur === undefined) return -1
|
||||
const { r, c, cost } = cur
|
||||
const key = getKey([r, c])
|
||||
if (seen.has(key)) continue
|
||||
seen.add(key)
|
||||
for (const [dr, dc] of deltas) {
|
||||
const nr = r + dr
|
||||
const nc = c + dc
|
||||
const key = getKey([nr, nc])
|
||||
if (seen.has(key)) continue
|
||||
const ncost = cost + 1
|
||||
if (nr === MAP_SIZE && nc === MAP_SIZE) return ncost // GOT IT
|
||||
if (nr < 0 || nc < 0 || nr > MAP_SIZE || nc > MAP_SIZE) continue
|
||||
const nextStep: Step = { r: nr, c: nc, cost: ncost, prev: cur }
|
||||
queue.push(nextStep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
return navigate(input, 1024)
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
for (let i = 1025; i < input.length; i++) {
|
||||
const result = navigate(input, i)
|
||||
process.stdout.write(`${i}`)
|
||||
if (result === -1) {
|
||||
return input[i - 1].join()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
// {
|
||||
// input: `5,4
|
||||
// 4,2
|
||||
// 4,5
|
||||
// 3,0
|
||||
// 2,1
|
||||
// 6,3
|
||||
// 2,4
|
||||
// 1,5
|
||||
// 0,6
|
||||
// 3,3
|
||||
// 2,6
|
||||
// 5,1`,
|
||||
// expected: 22,
|
||||
// },
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
// {
|
||||
// input: ``,
|
||||
// expected: "",
|
||||
// },
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue