diff --git a/aoc/puzzles/2023/day21/readme.md b/aoc/puzzles/2023/day21/readme.md new file mode 100644 index 0000000..d87c509 --- /dev/null +++ b/aoc/puzzles/2023/day21/readme.md @@ -0,0 +1,8 @@ +# Advent of Code 2023 - Day 21: [Step Counter](https://adventofcode.com/2023/day/21) + +## [Write Up](https://codingap.github.io/advent-of-code/writeups/2023/day21) +## Results +|| **Part 1** | **Part 2** | +|:--:|:---:|:---:| +| **Results** | 3605 | 596734624269210 | +| **Time (in ms)** | 50.71 | 37.63 | \ No newline at end of file diff --git a/aoc/puzzles/2023/day21/solution.js b/aoc/puzzles/2023/day21/solution.js new file mode 100644 index 0000000..5661804 --- /dev/null +++ b/aoc/puzzles/2023/day21/solution.js @@ -0,0 +1,111 @@ +/** + * aoc/puzzles/2023/day21/solution.js + * + * ~~ Step Counter ~~ + * this is my solution for this advent of code puzzle + * + * by alex prosser + * 12/20/2023 + */ + +/** + * code for part 1 of the advent of code puzzle + * + * @param {string} input + * @returns {Promise} the result of part 1 + */ +const part1 = async input => { + // parse input + const grid = input.split(/\n/).map(line => line.split('')); + + // find starting position + let starting = { x: 0, y: 0 }; + for (let y = 0; y < grid.length; y++) { + for (let x = 0; x < grid[0].length; x++) { + if (grid[y][x] == 'S') starting = { x, y }; + } + } + + // do bfs with multiple visited for each step + // doesn't re-search anything already searched + let visited = []; + let queue = [{ x: starting.x, y: starting.y, steps: 0 }]; + while (queue.length != 0) { + let current = queue.shift(); + + if (current.steps != 64) { + if (visited[current.steps] == null) visited[current.steps] = new Set(); + + // searchs up, down, left, and right + [{ x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 }].forEach(neighbor => { + const newPosition = { x: current.x + neighbor.x, y: current.y + neighbor.y, steps: current.steps + 1 }; + if (newPosition.x >= 0 && newPosition.x < grid[0].length && newPosition.y >= 0 && newPosition.y < grid.length && + grid[newPosition.y][newPosition.x] != '#' && + !visited[current.steps].has(`${newPosition.x},${newPosition.y}`)) { + visited[current.steps].add(`${newPosition.x},${newPosition.y}`); + queue.push(newPosition); + } + }) + } + } + + return visited[visited.length - 1].size; +} + +/** + * code for part 2 of the advent of code puzzle + * + * @param {string} input + * @returns {Promise} the result of part 2 + */ +const part2 = async input => { + // https://github.com/villuna/aoc23/wiki/A-Geometric-solution-to-advent-of-code-2023,-day-21 + // this helped me so much to understand what we are doing + // result = (n+1)^2 * odd_squares + n^2 * even_squares - (n+1) * odd_corners + n * even_corners + + // n: how many grid sizes we are going through (202300 for this puzzle) + // odd_squares: how many spots have an odd parity ((x+y+1) % 2 == 1) + // even_squares: how many spots have an even parity ((x+y+1) % 2 == 0) + // odd_corners: how many spots have an odd parity and distance > 65 + // even_corners: how many spots have an even parity and distance > 65 + + // parse input + const grid = input.split(/\n/).map(line => line.split('')); + + // find starting position + let starting = { x: 0, y: 0 }; + for (let y = 0; y < grid.length; y++) { + for (let x = 0; x < grid[0].length; x++) { + if (grid[y][x] == 'S') starting = { x, y }; + } + } + + // do bfs with multiple visited for each step + let distances = {}; + let queue = [{ x: starting.x, y: starting.y, steps: 0 }]; + while (queue.length != 0) { + let current = queue.shift(); + if (distances[`${current.x},${current.y}`] != null) continue; + distances[`${current.x},${current.y}`] = current.steps; + + // searchs up, down, left, and right + [{ x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 }].forEach(neighbor => { + const newPosition = { x: current.x + neighbor.x, y: current.y + neighbor.y, steps: current.steps + 1 }; + if (newPosition.x >= 0 && newPosition.x < grid[0].length && newPosition.y >= 0 && newPosition.y < grid.length && + grid[newPosition.y][newPosition.x] != '#' && + distances[`${newPosition.x},${newPosition.y}`] == null) { + queue.push(newPosition); + } + }) + } + + const n = (26501365 - Math.floor(grid.length / 2)) / grid.length; + const evenSquares = Object.values(distances).filter(distance => distance % 2 == 0).length; + const oddSquares = Object.values(distances).filter(distance => distance % 2 == 1).length; + const evenCorners = Object.values(distances).filter(distance => distance % 2 == 0 && distance > Math.floor(grid.length / 2)).length; + const oddCorners = Object.values(distances).filter(distance => distance % 2 == 1 && distance > Math.floor(grid.length / 2)).length; + + return Math.pow(n + 1, 2) * oddSquares + Math.pow(n, 2) * evenSquares - (n + 1) * oddCorners + n * evenCorners - n; +} + +export { part1, part2 }; \ No newline at end of file diff --git a/aoc/src/puzzles.json b/aoc/src/puzzles.json index b367032..d1ad582 100644 --- a/aoc/src/puzzles.json +++ b/aoc/src/puzzles.json @@ -892,8 +892,9 @@ "title": "Aplenty", "stars": 2 }, + null, { - "title": "Pulse Propagation", + "title": "Step Counter", "stars": 2 } ] diff --git a/index.html b/index.html index 64783a1..14ef339 100644 --- a/index.html +++ b/index.html @@ -28,7 +28,7 @@

-
+
@@ -5508,10 +5508,10 @@
[Source Code]
diff --git a/writeups/2015/day01/index.html b/writeups/2015/day01/index.html index 69555d2..8f116bd 100644 --- a/writeups/2015/day01/index.html +++ b/writeups/2015/day01/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day01/index.html b/writeups/2023/day01/index.html index 3be842d..dedb93b 100644 --- a/writeups/2023/day01/index.html +++ b/writeups/2023/day01/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day02/index.html b/writeups/2023/day02/index.html index 9775c24..1b710de 100644 --- a/writeups/2023/day02/index.html +++ b/writeups/2023/day02/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day03/index.html b/writeups/2023/day03/index.html index 31709ff..d32df04 100644 --- a/writeups/2023/day03/index.html +++ b/writeups/2023/day03/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day04/index.html b/writeups/2023/day04/index.html index ac2e653..c805901 100644 --- a/writeups/2023/day04/index.html +++ b/writeups/2023/day04/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day05/index.html b/writeups/2023/day05/index.html index f1ea68e..c321051 100644 --- a/writeups/2023/day05/index.html +++ b/writeups/2023/day05/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day06/index.html b/writeups/2023/day06/index.html index 27cd28f..b97d2aa 100644 --- a/writeups/2023/day06/index.html +++ b/writeups/2023/day06/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day07/index.html b/writeups/2023/day07/index.html index cf4bc21..440a62f 100644 --- a/writeups/2023/day07/index.html +++ b/writeups/2023/day07/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day08/index.html b/writeups/2023/day08/index.html index 5832c45..dbaa393 100644 --- a/writeups/2023/day08/index.html +++ b/writeups/2023/day08/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day09/index.html b/writeups/2023/day09/index.html index 31164a7..82736ae 100644 --- a/writeups/2023/day09/index.html +++ b/writeups/2023/day09/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day10/index.html b/writeups/2023/day10/index.html index 533a0bf..469e75f 100644 --- a/writeups/2023/day10/index.html +++ b/writeups/2023/day10/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day11/index.html b/writeups/2023/day11/index.html index a88ff77..4b75943 100644 --- a/writeups/2023/day11/index.html +++ b/writeups/2023/day11/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day12/index.html b/writeups/2023/day12/index.html index e04ebba..00fa498 100644 --- a/writeups/2023/day12/index.html +++ b/writeups/2023/day12/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day13/index.html b/writeups/2023/day13/index.html index b8d20a6..c9b1190 100644 --- a/writeups/2023/day13/index.html +++ b/writeups/2023/day13/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day16/index.html b/writeups/2023/day16/index.html index d977f9c..0f43b14 100644 --- a/writeups/2023/day16/index.html +++ b/writeups/2023/day16/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day18/index.html b/writeups/2023/day18/index.html index f21b654..afd3902 100644 --- a/writeups/2023/day18/index.html +++ b/writeups/2023/day18/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+

diff --git a/writeups/2023/day19/index.html b/writeups/2023/day19/index.html index 4d150bc..95d4127 100644 --- a/writeups/2023/day19/index.html +++ b/writeups/2023/day19/index.html @@ -26,7 +26,7 @@ [Back to Hub]
-
+