Skip to content

Commit

Permalink
day 14
Browse files Browse the repository at this point in the history
  • Loading branch information
spalberg committed Dec 21, 2024
1 parent e54ff44 commit aa47e65
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
80 changes: 80 additions & 0 deletions days/14/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { loadInput } from "utils";

const defaultWidth = 101;
const defaultHeight = 103;

export function part1(
input: Array<string>,
width = defaultWidth,
height = defaultHeight,
) {
const simulate100 = simulate(100, width, height);
const positions = input.map(parseInput).map(simulate100);
const halfWidth = Math.floor(width / 2);
const halfHeight = Math.floor(height / 2);
let q1 = 0, q2 = 0, q3 = 0, q4 = 0;
for (const { x, y } of positions) {
if (x < halfWidth && y < halfHeight) {
q1++;
} else if (x > halfWidth && y < halfHeight) {
q2++;
} else if (x < halfWidth && y > halfHeight) {
q3++;
} else if (x > halfWidth && y > halfHeight) {
q4++;
}
}
return q1 * q2 * q3 * q4;
}

export function part2(input: Array<string>) {
const robots = input.map(parseInput);
return simulateUntilUnique(robots, defaultWidth, defaultHeight);
}

type Robot = {
x: number;
y: number;
vx: number;
vy: number;
};

function simulate(seconds: number, width: number, height: number) {
return (robot: Robot) => {
const x = (((robot.x + robot.vx * seconds) % width) + width) % width;
const y = (((robot.y + robot.vy * seconds) % height) + height) % height;
return { x, y };
};
}

function simulateUntilUnique(
robots: Array<Robot>,
width: number,
height: number,
) {
outer: for (let s = 1;; s++) {
const positions = new Set<string>();
const simulateS = simulate(s, width, height);
for (const robot of robots) {
const { x, y } = simulateS(robot);
const key = `${x},${y}`;
if (positions.has(key)) {
continue outer;
}
positions.add(key);
}
return s;
}
}

function parseInput(line: string): Robot {
const [l, r] = line.split(" v=");
const [x, y] = l.split("=")[1].split(",").map(Number);
const [vx, vy] = r.split(",").map(Number);
return { x, y, vx, vy };
}

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

Check warning on line 80 in days/14/main.ts

View check run for this annotation

Codecov / codecov/patch

days/14/main.ts#L78-L80

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

describe("day 14 example", () => {
const input = [
"p=0,4 v=3,-3",
"p=6,3 v=-1,-3",
"p=10,3 v=-1,2",
"p=2,0 v=2,-1",
"p=0,0 v=1,3",
"p=3,0 v=-2,-2",
"p=7,6 v=-1,-3",
"p=3,0 v=-1,-2",
"p=9,3 v=2,3",
"p=7,3 v=-1,2",
"p=2,4 v=2,-3",
"p=9,5 v=-3,-3",
];

it("part 1", () => {
expect(part1(input, 11, 7)).toBe(12);
});
});

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

it("part 2", async () => {
expect(part2(await loadInput(14))).toBe(7138);
});
});
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]) {
for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14]) {
days.set(day, await import(`./${day}/main.ts`));
}

Expand Down
2 changes: 1 addition & 1 deletion inputs

0 comments on commit aa47e65

Please sign in to comment.