Skip to content

Commit 8982325

Browse files
committed
feat: 2024 Day 14
1 parent 09c0c7c commit 8982325

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

2024/day/14/part/1/solve.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "../../../../../lib/testing/asserts.ts";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
p=0,4 v=3,-3
8+
p=6,3 v=-1,-3
9+
p=10,3 v=-1,2
10+
p=2,0 v=2,-1
11+
p=0,0 v=1,3
12+
p=3,0 v=-2,-2
13+
p=7,6 v=-1,-3
14+
p=3,0 v=-1,-2
15+
p=9,3 v=2,3
16+
p=7,3 v=-1,2
17+
p=2,4 v=2,-3
18+
p=9,5 v=-3,-3`;
19+
20+
assertEquals(solve(input, { width: 11, height: 7 }), 12);
21+
});

2024/day/14/part/1/solve.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const regExp = /^p=(?<px>\d+),(?<py>\d+) v=(?<vx>-?\d+),(?<vy>-?\d+)$/gm;
2+
3+
export default function solve(
4+
input: string,
5+
{ width = 101, height = 103, t = 100 } = {},
6+
) {
7+
const robots = input.matchAll(regExp).map((match) => match.map(Number))
8+
.map(([, px, py, vx, vy]) => ({ px, py, vx, vy })).toArray();
9+
const mid = { x: (width - 1) / 2, y: (height - 1) / 2 };
10+
const quadrantCounts = [0, 0, 0, 0];
11+
for (const { px, py, vx, vy } of robots) {
12+
const fx = mod(px + t * vx, width), fy = mod(py + t * vy, height);
13+
if (fx === mid.x || fy === mid.y) continue;
14+
quadrantCounts[(fy < mid.y ? 0 : 2) + (fx < mid.x ? 0 : 1)]++;
15+
}
16+
return quadrantCounts.reduce((product, count) => product * count, 1);
17+
}
18+
19+
function mod(a: number, b: number) {
20+
return ((a % b) + b) % b;
21+
}

2024/day/14/part/2/solve.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "../../../../../lib/testing/asserts.ts";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
p=0,4 v=3,-3
8+
p=6,3 v=-1,-3
9+
p=10,3 v=-1,2
10+
p=2,0 v=2,-1
11+
p=0,0 v=1,3
12+
p=3,0 v=-2,-2
13+
p=7,6 v=-1,-3
14+
p=3,0 v=-1,-2
15+
p=9,3 v=2,3
16+
p=7,3 v=-1,2
17+
p=2,4 v=2,-3
18+
p=9,5 v=-3,-3`;
19+
20+
assertEquals(solve(input, { width: 11, height: 7 }), 12);
21+
});

2024/day/14/part/2/solve.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const regExp = /^p=(?<px>\d+),(?<py>\d+) v=(?<vx>-?\d+),(?<vy>-?\d+)$/gm;
2+
3+
export default function solve(
4+
input: string,
5+
{ width = 101, height = 103 } = {},
6+
) {
7+
const robots = input.matchAll(regExp).map((match) => match.map(Number))
8+
.map(([, px, py, vx, vy]) => ({ px, py, vx, vy })).toArray();
9+
moments: for (let t = 0;; t++) {
10+
const positionHashes = new Set();
11+
for (const { px, py, vx, vy } of robots) {
12+
const x = mod(px + vx * t, width), y = mod(py + vy * t, height);
13+
const positionHash = `${x},${y}`;
14+
if (positionHashes.has(positionHash)) continue moments;
15+
positionHashes.add(positionHash);
16+
}
17+
return t;
18+
}
19+
}
20+
21+
function mod(a: number, b: number) {
22+
return ((a % b) + b) % b;
23+
}

0 commit comments

Comments
 (0)