Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day 15 #29

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions days/15/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { loadInput } from "utils";

export function part1(input: Array<string>) {
const { walls, boxes, initialRobotPosition, moves, width, height } =
parseInput(input);
let robotPosition = initialRobotPosition;
// console.log("Initial state:");
for (const move of moves) {
// print(robotPosition, walls, boxes, width, height);
// console.log(`Move ${move}:`);
const directionVector =
directionVectors[move as unknown as keyof typeof directionVectors]!;
const potentialNewRobotPosition = robotPosition + directionVector;
if (walls.has(potentialNewRobotPosition)) continue;
if (!boxes.has(potentialNewRobotPosition)) {
robotPosition = potentialNewRobotPosition;
continue;
}
for (
let next = potentialNewRobotPosition + directionVector;
!walls.has(next);
next += directionVector
) {
if (boxes.has(next)) {
continue;
}
boxes.delete(potentialNewRobotPosition);
boxes.add(next);
robotPosition = potentialNewRobotPosition;
break;
}
}
// print(robotPosition, walls, boxes, width, height);
return boxes.values().reduce((acc, gps) => acc + gps, 0);
}

export function part2(input: Array<string>) {
return 0;
}

Check warning on line 39 in days/15/main.ts

View check run for this annotation

Codecov / codecov/patch

days/15/main.ts#L37-L39

Added lines #L37 - L39 were not covered by tests

function parseInput(input: Array<string>) {
const walls = new Set<number>();
const boxes = new Set<number>();
let initialRobotPosition = -1;
const delimiterIndex = input.indexOf("");
const moves = input.slice(delimiterIndex + 1).join("");
const map = input.slice(0, delimiterIndex);
for (let y = 0; y < map.length; y++) {
const row = map[y];
for (let x = 0; x < row.length; x++) {
const char = row[x];
if (char === ".") continue;
else if (char === "#") walls.add(gpsCoordinate(x, y));
else if (char === "O") boxes.add(gpsCoordinate(x, y));
else if (char === "@") initialRobotPosition = gpsCoordinate(x, y);
}
}
return {
walls,
boxes,
initialRobotPosition,
moves,
width: map.length,
height: map[0].length,
};
}

const directionVectors = {
"^": gpsCoordinate(0, -1),
">": gpsCoordinate(1, 0),
"v": gpsCoordinate(0, 1),
"<": gpsCoordinate(-1, 0),
};

function gpsCoordinate(x: number, y: number) {
return 100 * y + x;
}

function print(
robot: number,
walls: Set<number>,
boxes: Set<number>,
width: number,
height: number,

Check warning on line 84 in days/15/main.ts

View check run for this annotation

Codecov / codecov/patch

days/15/main.ts#L79-L84

Added lines #L79 - L84 were not covered by tests
) {
for (let y = 0; y < height; y++) {
let row = "";
for (let x = 0; x < width; x++) {
const gps = gpsCoordinate(x, y);
if (walls.has(gps)) row += "#";
else if (boxes.has(gps)) row += "O";
else if (robot === gps) row += "@";
else row += ".";
}
console.log(row);
}
console.log("");
}

Check warning on line 98 in days/15/main.ts

View check run for this annotation

Codecov / codecov/patch

days/15/main.ts#L86-L98

Added lines #L86 - L98 were not covered by tests

if (import.meta.main) {
console.log(part1(await loadInput(15)));
console.log(part2(await loadInput(15)));
}

Check warning on line 103 in days/15/main.ts

View check run for this annotation

Codecov / codecov/patch

days/15/main.ts#L101-L103

Added lines #L101 - L103 were not covered by tests
61 changes: 61 additions & 0 deletions days/15/main_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { expect } from "@std/expect";
import { describe, it } from "@std/testing/bdd";
import { part1, part2 } from "./main.ts";
import { loadInput } from "utils";

describe("day 15 example", () => {
it("part 1 small", () => {
expect(part1([
"########",
"#..O.O.#",
"##@.O..#",
"#...O..#",
"#.#.O..#",
"#...O..#",
"#......#",
"########",
"",
"<^^>>>vv<v>>v<<",
])).toBe(2028);
});

it("part 1 large", () => {
expect(part1([
"##########",
"#..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^<<^",
])).toBe(10092);
});

it.ignore("part 2", () => {
expect(part2([])).toBe(-1);
});
});

describe("day 15 solution", () => {
it("part 1", async () => {
expect(part1(await loadInput(15))).toBe(1441031);
});

it.ignore("part 2", async () => {
expect(part2(await loadInput(15))).toBe(-1);
});
});
2 changes: 1 addition & 1 deletion days/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type Solution = { part1: PartFunction; part2: PartFunction };

const days = new Map<number, Solution>();

for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14]) {
for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15]) {
days.set(day, await import(`./${day}/main.ts`));
}

Expand Down
Loading