Skip to content

Commit

Permalink
solved day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 20, 2024
1 parent 9366676 commit fea2e04
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 56 deletions.
45 changes: 45 additions & 0 deletions src/2024/day20.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function findPath(input) {
let map = input.split("\n").map(line => line.split(""));
let sy = map.findIndex(line => line.includes("S"));
let sx = map[sy].indexOf("S");
let ey = map.findIndex(line => line.includes("E"));
let ex = map[ey].indexOf("E");
let queue = [{ x: sx, y: sy }];
let visited = new Set([`${sx},${sy}`]);
let path = [];
map[ey][ex] = ".";
while (queue.length) {
let { x, y } = queue.shift();
path.push({ x, y });
if (x === ex && y === ey) break;
const neighbors = [
{ x: x + 1, y },
{ x: x - 1, y },
{ x, y: y + 1 },
{ x, y: y - 1 },
].filter(({ x, y }) => map[y]?.[x] === "." && !visited.has(`${x},${y}`));
neighbors.forEach(({ x, y }) => {
visited.add(`${x},${y}`);
queue.push({ x, y });
});
}
return path;
}

export function part1(input, save = 100, cheat = 2) {
let path = findPath(input);
let count = 0;
for (let i = 0; i < path.length; i++) {
for (let j = i + save; j < path.length; j++) {
const distance =
Math.abs(path[j].x - path[i].x) + Math.abs(path[j].y - path[i].y);
const saved = j - i - distance;
if (saved >= save && distance <= cheat) count++;
}
}
return count;
}

export function part2(input, save = 100) {
return part1(input, save, 20);
}
68 changes: 68 additions & 0 deletions src/2024/day20.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { part1, part2 } from "./day20.js";
import readInput from "../utils/read-input.js";

const input = readInput(import.meta.url);

describe("day20 2024", () => {
describe("part1", () => {
test("it should work for part 1 examples", () => {
expect(
part1(
[
"###############",
"#...#...#.....#",
"#.#.#.#.#.###.#",
"#S#...#.#.#...#",
"#######.#.#.###",
"#######.#.#...#",
"#######.#.###.#",
"###..E#...#...#",
"###.#######.###",
"#...###...#...#",
"#.#####.#.###.#",
"#.#...#.#.#...#",
"#.#.#.#.#.#.###",
"#...#...#...###",
"###############",
].join("\n"),
2,
),
).toEqual(44);
});

test("it should work for part 1 input", () => {
expect(part1(input)).toEqual(1459);
});
});

describe("part2", () => {
test("it should work for part 2 examples", () => {
expect(
part2(
[
"###############",
"#...#...#.....#",
"#.#.#.#.#.###.#",
"#S#...#.#.#...#",
"#######.#.#.###",
"#######.#.#...#",
"#######.#.###.#",
"###..E#...#...#",
"###.#######.###",
"#...###...#...#",
"#.#####.#.###.#",
"#.#...#.#.#...#",
"#.#.#.#.#.#.###",
"#...#...#...###",
"###############",
].join("\n"),
50,
),
).toEqual(285);
});

test("it should work for part 2 input", () => {
expect(part2(input)).toEqual(1016066);
});
});
});
142 changes: 142 additions & 0 deletions src/2024/day20.txt

Large diffs are not rendered by default.

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">38*</span></div>
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">40*</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">38*</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">40*</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">488*</span></article>
<p>Total stars: <span class="star-count">490*</span></article>
</main>
</body>
</html>
Loading

0 comments on commit fea2e04

Please sign in to comment.