From ad6829def6cba531d80e948ead0ff97f9d2c81d5 Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Fri, 6 Dec 2024 02:21:08 -0500 Subject: [PATCH] day 6, buggy part 2 --- .aocrunner.json | 10 ++-- README.md | 16 +++--- src/day06/README.md | 9 ++++ src/day06/index.ts | 120 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 src/day06/README.md create mode 100644 src/day06/index.ts diff --git a/.aocrunner.json b/.aocrunner.json index fd7e9dd..ae1450a 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -77,16 +77,18 @@ }, { "part1": { - "solved": false, + "solved": true, "result": null, "attempts": [], "time": null }, "part2": { "solved": false, - "result": null, - "attempts": [], - "time": null + "result": "1878", + "attempts": [ + "1878" + ], + "time": 2128.985844 } }, { diff --git a/README.md b/README.md index 6f7309c..865dd55 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ [![Day](https://badgen.net/badge/03/%E2%98%85%E2%98%85/green)](src/day03) [![Day](https://badgen.net/badge/04/%E2%98%85%E2%98%85/green)](src/day04) [![Day](https://badgen.net/badge/05/%E2%98%85%E2%98%85/green)](src/day05) -![Day](https://badgen.net/badge/06/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/06/%E2%98%85%E2%98%86/yellow)](src/day06) ![Day](https://badgen.net/badge/07/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/08/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/09/%E2%98%86%E2%98%86/gray) @@ -97,16 +97,16 @@ Both parts: 250.695ms ``` Day 05 -Time part 1: 29.068ms -Time part 2: 53.891ms -Both parts: 82.959ms +Time part 1: 28.307ms +Time part 2: 35.288ms +Both parts: 63.594ms ``` ``` Day 06 -Time part 1: - +Time part 1: 1.668ms Time part 2: - -Both parts: - +Both parts: 1.668ms ``` ``` @@ -243,8 +243,8 @@ Both parts: - ``` ``` -Total stars: 10/50 -Total time: 342.015ms +Total stars: 11/50 +Total time: 324.318ms ``` diff --git a/src/day06/README.md b/src/day06/README.md new file mode 100644 index 0000000..1a5019a --- /dev/null +++ b/src/day06/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2024 - day 6 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2024/day/6) + +## Notes + +... \ No newline at end of file diff --git a/src/day06/index.ts b/src/day06/index.ts new file mode 100644 index 0000000..9a2459b --- /dev/null +++ b/src/day06/index.ts @@ -0,0 +1,120 @@ +import run from "aocrunner" + +const parseInput = (rawInput: string) => rawInput.split('\n') + +const part1 = (rawInput: string) => { + const input = parseInput(rawInput) + const rawCol = rawInput.indexOf('^') + let width = rawInput.indexOf('\n') + let position = [Math.floor(rawCol / (width + 1)), rawCol % (width + 1)] + let visited = new Set() + const directions = [ + [-1,0], + [0,1], + [1,0], + [0,-1] + ] + let dir = 0 + while(true) { + visited.add(position.join()) + const nextPosition = [position[0] + directions[dir][0], position[1] + directions[dir][1]] + const next = (input[nextPosition[0]] ?? [])[nextPosition[1]] + if (next === undefined) { + break // done! + } + if (next === "#") { + dir = (dir + 1) % 4 + continue + } + position = nextPosition + } + return visited.size +} + +const part2 = (rawInput: string) => { + const input = parseInput(rawInput) + const rawCol = rawInput.indexOf('^') + let width = rawInput.indexOf('\n') + const directions = [ + [-1,0], + [0,1], + [1,0], + [0,-1] + ] + let obstructions = new Set() + const walk = ( + initialPosition: number[], + tempObstacle?: [number, number], + initialVisited = new Set, + initialDir = 0 + ) => { + let position = [...initialPosition] + let visited = new Set(initialVisited) + let dir = initialDir + while(true) { + const key = [...position, dir].join() + if (visited.has(key)) { + // we have entered a loop! + obstructions.add(tempObstacle.join()) + return + } + const nextPosition: [number, number] = [position[0] + directions[dir][0], position[1] + directions[dir][1]] + const next = (input[nextPosition[0]] ?? [])[nextPosition[1]] + if (next === undefined) { + // we are done! + return obstructions.size + } + if (tempObstacle == null && next != "#" && !obstructions.has(nextPosition.join())) { + walk(position, nextPosition, visited, dir) + } + visited.add(key) + if (next === "#" || (tempObstacle != null && nextPosition.join() === tempObstacle.join())) { + dir = (dir + 1) % 4 + continue + } + position = nextPosition + } + } + return walk([Math.floor(rawCol / (width + 1)), rawCol % (width + 1)]) +} + +run({ +// part1: { +// tests: [ +// { +// input: `....#..... +// .........# +// .......... +// ..#....... +// .......#.. +// .......... +// .#..^..... +// ........#. +// #......... +// ......#...`, +// expected: 41, +// }, +// ], +// solution: part1, +// }, + part2: { + tests: [ + { + input: `....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#...`, + expected: 6, + }, + ], + solution: part2, + }, + trimTestInputs: true, + onlyTests: false, +})