-
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
317 additions
and
56 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,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); | ||
} |
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,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); | ||
}); | ||
}); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
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.