diff --git a/.aocrunner.json b/.aocrunner.json index 788ee9b..43c6b9a 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -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 } }, { diff --git a/README.md b/README.md index 54425cc..f383dcc 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ [![Day](https://badgen.net/badge/15/%E2%98%85%E2%98%85/green)](src/day15) [![Day](https://badgen.net/badge/16/%E2%98%85%E2%98%85/green)](src/day16) [![Day](https://badgen.net/badge/17/%E2%98%85%E2%98%85/green)](src/day17) -![Day](https://badgen.net/badge/18/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/18/%E2%98%85%E2%98%85/green)](src/day18) ![Day](https://badgen.net/badge/19/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/20/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/21/%E2%98%86%E2%98%86/gray) @@ -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 ``` diff --git a/src/day18/README.md b/src/day18/README.md new file mode 100644 index 0000000..96ab406 --- /dev/null +++ b/src/day18/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2024 - day 18 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2024/day/18) + +## Notes + +... \ No newline at end of file diff --git a/src/day18/index.ts b/src/day18/index.ts new file mode 100644 index 0000000..3bcf789 --- /dev/null +++ b/src/day18/index.ts @@ -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 + +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, +})