Skip to content

Commit fea2e04

Browse files
committed
solved day 20
1 parent 9366676 commit fea2e04

File tree

9 files changed

+317
-56
lines changed

9 files changed

+317
-56
lines changed

src/2024/day20.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function findPath(input) {
2+
let map = input.split("\n").map(line => line.split(""));
3+
let sy = map.findIndex(line => line.includes("S"));
4+
let sx = map[sy].indexOf("S");
5+
let ey = map.findIndex(line => line.includes("E"));
6+
let ex = map[ey].indexOf("E");
7+
let queue = [{ x: sx, y: sy }];
8+
let visited = new Set([`${sx},${sy}`]);
9+
let path = [];
10+
map[ey][ex] = ".";
11+
while (queue.length) {
12+
let { x, y } = queue.shift();
13+
path.push({ x, y });
14+
if (x === ex && y === ey) break;
15+
const neighbors = [
16+
{ x: x + 1, y },
17+
{ x: x - 1, y },
18+
{ x, y: y + 1 },
19+
{ x, y: y - 1 },
20+
].filter(({ x, y }) => map[y]?.[x] === "." && !visited.has(`${x},${y}`));
21+
neighbors.forEach(({ x, y }) => {
22+
visited.add(`${x},${y}`);
23+
queue.push({ x, y });
24+
});
25+
}
26+
return path;
27+
}
28+
29+
export function part1(input, save = 100, cheat = 2) {
30+
let path = findPath(input);
31+
let count = 0;
32+
for (let i = 0; i < path.length; i++) {
33+
for (let j = i + save; j < path.length; j++) {
34+
const distance =
35+
Math.abs(path[j].x - path[i].x) + Math.abs(path[j].y - path[i].y);
36+
const saved = j - i - distance;
37+
if (saved >= save && distance <= cheat) count++;
38+
}
39+
}
40+
return count;
41+
}
42+
43+
export function part2(input, save = 100) {
44+
return part1(input, save, 20);
45+
}

src/2024/day20.spec.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { part1, part2 } from "./day20.js";
2+
import readInput from "../utils/read-input.js";
3+
4+
const input = readInput(import.meta.url);
5+
6+
describe("day20 2024", () => {
7+
describe("part1", () => {
8+
test("it should work for part 1 examples", () => {
9+
expect(
10+
part1(
11+
[
12+
"###############",
13+
"#...#...#.....#",
14+
"#.#.#.#.#.###.#",
15+
"#S#...#.#.#...#",
16+
"#######.#.#.###",
17+
"#######.#.#...#",
18+
"#######.#.###.#",
19+
"###..E#...#...#",
20+
"###.#######.###",
21+
"#...###...#...#",
22+
"#.#####.#.###.#",
23+
"#.#...#.#.#...#",
24+
"#.#.#.#.#.#.###",
25+
"#...#...#...###",
26+
"###############",
27+
].join("\n"),
28+
2,
29+
),
30+
).toEqual(44);
31+
});
32+
33+
test("it should work for part 1 input", () => {
34+
expect(part1(input)).toEqual(1459);
35+
});
36+
});
37+
38+
describe("part2", () => {
39+
test("it should work for part 2 examples", () => {
40+
expect(
41+
part2(
42+
[
43+
"###############",
44+
"#...#...#.....#",
45+
"#.#.#.#.#.###.#",
46+
"#S#...#.#.#...#",
47+
"#######.#.#.###",
48+
"#######.#.#...#",
49+
"#######.#.###.#",
50+
"###..E#...#...#",
51+
"###.#######.###",
52+
"#...###...#...#",
53+
"#.#####.#.###.#",
54+
"#.#...#.#.#...#",
55+
"#.#.#.#.#.#.###",
56+
"#...#...#...###",
57+
"###############",
58+
].join("\n"),
59+
50,
60+
),
61+
).toEqual(285);
62+
});
63+
64+
test("it should work for part 2 input", () => {
65+
expect(part2(input)).toEqual(1016066);
66+
});
67+
});
68+
});

src/2024/day20.txt

Lines changed: 142 additions & 0 deletions
Large diffs are not rendered by default.

src/2024/events.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h1 class="title-global"><a href="index.html">Advent of Code</a></h1>
2222
</nav>
2323
<div class="user">Shahar Talmi <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <a
2424
href="https://www.wix.engineering/" target="_blank" class="sponsor-badge"
25-
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">38*</span></div>
25+
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">40*</span></div>
2626
</div>
2727
<div>
2828
<h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=</span><a
@@ -32,7 +32,7 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
3232
<main>
3333
<article>
3434
<p>From here, you can access all of the events (and the corresponding puzzles, leaderboards, stats, etc) ever run on Advent of Code:</p>
35-
<div class="eventlist-event"><a href="../2024/solver.html">[2024]</a> <span class="star-count">38*</span> <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
35+
<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>
3636
<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>
3737
<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>
3838
<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>
@@ -42,7 +42,7 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
4242
<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>
4343
<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>
4444
<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>
45-
<p>Total stars: <span class="star-count">488*</span></article>
45+
<p>Total stars: <span class="star-count">490*</span></article>
4646
</main>
4747
</body>
4848
</html>

0 commit comments

Comments
 (0)