Skip to content

Commit

Permalink
solved day 21
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 21, 2024
1 parent bf2127d commit 87b75cd
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 62 deletions.
76 changes: 76 additions & 0 deletions src/2024/day21.js
Original file line number Diff line number Diff line change
@@ -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);
}
24 changes: 24 additions & 0 deletions src/2024/day21.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
6 changes: 6 additions & 0 deletions src/2024/day21.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
129A
176A
985A
170A
528A

6 changes: 3 additions & 3 deletions src/2024/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1 class="title-global"><a href="index.html">Advent of Code</a></h1>
</nav>
<div class="user">Shahar Talmi <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <a
href="https://www.wix.engineering/" target="_blank" class="sponsor-badge"
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">40*</span></div>
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">42*</span></div>
</div>
<div>
<h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=</span><a
Expand All @@ -32,7 +32,7 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
<main>
<article>
<p>From here, you can access all of the events (and the corresponding puzzles, leaderboards, stats, etc) ever run on Advent of Code:</p>
<div class="eventlist-event"><a href="../2024/solver.html">[2024]</a> <span class="star-count">40*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2024/solver.html">[2024]</a> <span class="star-count">42*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2023/solver.html">[2023]</a> <span class="star-count">50*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2022/solver.html">[2022]</a> <span class="star-count">50*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2021/solver.html">[2021]</a> <span class="star-count">50*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
Expand All @@ -42,7 +42,7 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
<div class="eventlist-event"><a href="../2017/solver.html">[2017]</a> <span class="star-count">50*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2016/solver.html">[2016]</a> <span class="star-count">50*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2015/solver.html">[2015]</a> <span class="star-count">50*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<p>Total stars: <span class="star-count">490*</span></article>
<p>Total stars: <span class="star-count">492*</span></article>
</main>
</body>
</html>
Loading

0 comments on commit 87b75cd

Please sign in to comment.