Skip to content

Commit

Permalink
day 15 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
merrywhether committed Dec 19, 2024
1 parent 99c633d commit e36a501
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/15/enput.txt

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions src/15/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
type Map = string[][];
type Position = [x: number, y: number];
type Move = "<" | "^" | ">" | "v";

const moveMap = {
"<": [-1, 0],
"^": [0, -1],
">": [1, 0],
"v": [0, 1],
};

function getNextPosition(current: Position, move: Move): Position {
const [dx, dy] = moveMap[move];
return [current[0] + dx, current[1] + dy];
}

export async function main(target = "input") {
const dirpath = new URL(".", import.meta.url).pathname;
const text = await Deno.readTextFile(`${dirpath}${target}.txt`);

const { map, moves, start } = text.split("\n").reduce((agg, line, idx) => {
if (line.match(/^[#.O@]+$/)) {
const row = line.split("");
const start = row.findIndex((cell) => cell === "@");
if (start !== -1) {
agg.start = [start, idx];
row[start] = ".";
}
agg.map.push(row);
} else if (line.match(/^[<^>v]+$/)) {
agg.moves.push(...(line.split("") as Move[]));
}

return agg;
}, { map: [] as Map, moves: [] as Move[], start: [0, 0] as Position });

let current = start;
moves.forEach((move) => {
const next = getNextPosition(current, move);

switch (map[next[1]]?.[next[0]]) {
case "#":
return;
case ".":
current = next;
return;
case "O": {
let nextInDirection = getNextPosition(next, move);
while (map[nextInDirection[1]]?.[nextInDirection[0]]) {
if (map[nextInDirection[1]][nextInDirection[0]] === "#") {
break;
}
if (map[nextInDirection[1]][nextInDirection[0]] === "O") {
nextInDirection = getNextPosition(nextInDirection, move);
continue;
}
if (map[nextInDirection[1]][nextInDirection[0]] === ".") {
map[nextInDirection[1]][nextInDirection[0]] = "O";
map[next[1]][next[0]] = ".";
current = next;
return;
}
}
}
}
});

const gpsSum = map.reduce((agg, row, y) => {
return agg + row.reduce((agg2, cell, x) => {
if (cell === "O") {
agg2 += y * 100 + x;
}
return agg2;
}, 0);
}, 0);

return { gpsSum };
}

if (import.meta.main) {
const startTime = performance.now();
console.log(await main());
console.log(`Elapsed: ${Math.round(performance.now() - startTime)}ms`);
}
10 changes: 10 additions & 0 deletions src/15/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
########
#..O.O.#
##@.O..#
#...O..#
#.#.O..#
#...O..#
#......#
########

<^^>>>vv<v>>v<<
21 changes: 21 additions & 0 deletions src/15/sample2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
##########
#..O..O.O#
#......O.#
#.OO..O.O#
#[email protected].#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########

<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^
19 changes: 19 additions & 0 deletions src/15/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { assertEquals } from "@std/assert";
import { main } from "./main.ts";

const target = "sample";

Deno.test(`correct GPS sum for ${target}`, async () => {
const result = await main(target);
assertEquals(result.gpsSum, 2028);
});

Deno.test(`correct GPS sum for sample2`, async () => {
const result = await main("sample2");
assertEquals(result.gpsSum, 10092);
});

// Deno.test(`correct placeholder2 for ${target}`, async () => {
// const result = await main(target);
// assertEquals(result.gpsSum, 0);
// });

0 comments on commit e36a501

Please sign in to comment.