From 87b75cdcf3585ddbe5f463d569c38514a6c7d274 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Sat, 21 Dec 2024 10:17:24 +0200 Subject: [PATCH] solved day 21 --- src/2024/day21.js | 76 +++++++++++++++++++++++ src/2024/day21.spec.js | 24 +++++++ src/2024/day21.txt | 6 ++ src/2024/events.html | 6 +- src/2024/index.html | 117 ++++++++++++++++++----------------- src/2024/leaderboard.html | 4 +- src/2024/solver.html | 2 +- static/screenshot-events.png | Bin 72891 -> 72784 bytes static/screenshot-solver.png | Bin 55603 -> 55638 bytes 9 files changed, 173 insertions(+), 62 deletions(-) create mode 100644 src/2024/day21.js create mode 100644 src/2024/day21.spec.js create mode 100644 src/2024/day21.txt diff --git a/src/2024/day21.js b/src/2024/day21.js new file mode 100644 index 00000000..a5d6cc3e --- /dev/null +++ b/src/2024/day21.js @@ -0,0 +1,76 @@ +const keypad = { + 7: { x: 0, y: 0 }, + 8: { x: 1, y: 0 }, + 9: { x: 2, y: 0 }, + 4: { x: 0, y: 1 }, + 5: { x: 1, y: 1 }, + 6: { x: 2, y: 1 }, + 1: { x: 0, y: 2 }, + 2: { x: 1, y: 2 }, + 3: { x: 2, y: 2 }, + X: { x: 0, y: 3 }, + 0: { x: 1, y: 3 }, + A: { x: 2, y: 3 }, +}; + +const arrowpad = { + "X": { x: 0, y: 0 }, + "^": { x: 1, y: 0 }, + "A": { x: 2, y: 0 }, + "<": { x: 0, y: 1 }, + "v": { x: 1, y: 1 }, + ">": { x: 2, y: 1 }, +}; + +function bestTypes(from, to, forbidden, pads) { + const queue = [{ ...from, push: "" }]; + let min = Infinity; + while (queue.length) { + const { x, y, push } = queue.shift(); + if (x === forbidden.x && y === forbidden.y) continue; + if (x === to.x && y === to.y) { + min = Math.min(min, type(`${push}A`, pads.slice(1))); + } + if (to.x > x) queue.push({ x: x + 1, y, push: `${push}>` }); + if (to.x < x) queue.push({ x: x - 1, y, push: `${push}<` }); + if (to.y > y) queue.push({ x, y: y + 1, push: `${push}v` }); + if (to.y < y) queue.push({ x, y: y - 1, push: `${push}^` }); + } + return min; +} + +let memory = {}; +function type(code, pads) { + if (pads.length === 0) return code.length; + if (memory[`${code},${pads.length}`]) return memory[`${code},${pads.length}`]; + let pad = pads[0]; + let from = pad["A"]; + let pushes = 0; + for (let i = 0; i < code.length; i++) { + const to = pad[code[i]]; + pushes += bestTypes(from, to, pad["X"], pads); + from = to; + } + memory[`${code},${pads.length}`] = pushes; + return pushes; +} + +export function part1(input) { + const codes = input.split("\n"); + return codes + .map(code => { + let pushes = type(code, [keypad, arrowpad, arrowpad]); + return parseInt(code) * pushes; + }) + .reduce((a, b) => a + b, 0); +} + +export function part2(input) { + const codes = input.split("\n"); + return codes + .map(code => { + let pushes = type(code, [keypad, ...new Array(25).fill(arrowpad)]); + return parseInt(code) * pushes; + }) + .reduce((a, b) => a + b, 0); +} diff --git a/src/2024/day21.spec.js b/src/2024/day21.spec.js new file mode 100644 index 00000000..bfb024f4 --- /dev/null +++ b/src/2024/day21.spec.js @@ -0,0 +1,24 @@ +import { part1, part2 } from "./day21.js"; +import readInput from "../utils/read-input.js"; + +const input = readInput(import.meta.url); + +describe("day21 2024", () => { + describe("part1", () => { + test("it should work for part 1 examples", () => { + expect( + part1(["029A", "980A", "179A", "456A", "379A"].join("\n")), + ).toEqual(126384); + }); + + test("it should work for part 1 input", () => { + expect(part1(input)).toEqual(136780); + }); + }); + + describe("part2", () => { + test("it should work for part 2 input", () => { + expect(part2(input)).toEqual(167538833832712); + }); + }); +}); diff --git a/src/2024/day21.txt b/src/2024/day21.txt new file mode 100644 index 00000000..6fcbcfd1 --- /dev/null +++ b/src/2024/day21.txt @@ -0,0 +1,6 @@ +129A +176A +985A +170A +528A + diff --git a/src/2024/events.html b/src/2024/events.html index 76fdfa7d..7c9fa5f7 100644 --- a/src/2024/events.html +++ b/src/2024/events.html @@ -22,7 +22,7 @@

Advent of Code

Shahar Talmi (AoC++) 40*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 42*

   $year=   $year=<
+

Total stars: 492*

\ No newline at end of file diff --git a/src/2024/index.html b/src/2024/index.html index e130526d..3e9b4580 100644 --- a/src/2024/index.html +++ b/src/2024/index.html @@ -22,7 +22,7 @@

Advent of Code

Shahar Talmi (AoC++) 40*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 42*

   $year=   $year=<
          .-----.          .------------------.         
        .--'~ ~ ~|        .-' *       \  /     '-.   1 **
     .--'~  ,* ~ |        |  >o<   \_\_\|_/__/   |   2 **
 .---': ~ '(~), ~|        | >@>O< o-_/.()__------|   3 **
-|@..@'. ~ " ' ~ |        |>O>o<@< \____       .'|   4 **
-|_.~._#'.. ~ ~ *|        | _| |_    ..\_\_ ..'* |   5 **
-| ||| #@@ '''...|        |...     .'  '.'''../..|   6 **
-|@~~~@@@ @@@@   |        |/\ ''.  |    |   -/  :|   7 **
+|#..@'. ~ " ' ~ |        |>O>o<@< \____       .'|   4 **
+|_.~._#'.. ~ ~ *|        | _| |_    ..\_\_ ..'* |   5 **
+| ||| @@@#'''...|        |...     .'  '.'''../..|   6 **
+|@~~~#@@@@      |        |/\ ''.  |    |   -/  :|   7 **
 |~~..--. _____  |        |* /~\ '.|    | - /  .'|   8 **
 '---'  ||[][]_\-|        |~/ * \ :|    |  *..'  |   9 **
        |------- |        |   /\ .'|    |'''~~~~~|  10 **
-       |.......||        |/\ ..'  |    | . .    |  11 **
-       |  -  -  |        |''':::::|    |  . .   |  12 **
-       |'. -   -|        |  .:::::|    |. .'    |  13 **
+       |.......||        |/\ ..'  |    |   .  ' |  11 **
+       |  -  -  |        |''':::::|    |  .     |  12 **
+       |'. -   -|        |   :::::|    |' .'  . |  13 **
        |...'..''|        |...........  :::::|    |..|\..''|  14 **
-       |.  ''.  |        |.  :::::|    ||  15 **
+       |.  ''.  |        |. .:::::|    ||  15 **
        | '.~  '.|        | : :::::|    |AoCo|  16 **
-       |. *'.~ :|        |  '. .  |    |oten|  17 **
+       |. *'.~ :|        |  '.    |    |oten|  17 **
        | '..' .'|        |   'o   |    |*yrs|  18 **
-       | ~ ..'  |        |:   '.  |    ||  19 **
+       | ~ ..'  |        |: . '.  |    ||  19 **
        |'''))   |        | o  * : '.  .'>>o<<|  20 **
-
-
 
 
 
diff --git a/src/2024/leaderboard.html b/src/2024/leaderboard.html
index cd9ee6bb..7c66a6e5 100644
--- a/src/2024/leaderboard.html
+++ b/src/2024/leaderboard.html
@@ -22,7 +22,7 @@ 

Advent of Code

Shahar Talmi (AoC++) 40*
+ title="Member of sponsor: Wix Engineering">(Sponsor) 42*

   $year=   $year=<