-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
173 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
129A | ||
176A | ||
985A | ||
170A | ||
528A | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.