Skip to content

Commit a609cee

Browse files
committed
feat: 2022 Day 17 Part 1
1 parent e061b19 commit a609cee

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

2022/day/17/part/1/solve.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "@std/assert";
4+
5+
Deno.test("example", () => {
6+
const input = ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>";
7+
8+
assertEquals(solve(input), 3068);
9+
});

2022/day/17/part/1/solve.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const shapes = [
2+
Object.assign([[0, 0], [1, 0], [2, 0], [3, 0]], { width: 4 }),
3+
Object.assign([[1, 0], [0, 1], [1, 1], [2, 1], [1, 2]], { width: 3 }),
4+
Object.assign([[0, 0], [1, 0], [2, 0], [2, 1], [2, 2]], { width: 3 }),
5+
Object.assign([[0, 0], [0, 1], [0, 2], [0, 3]], { width: 1 }),
6+
Object.assign([[0, 0], [1, 0], [0, 1], [1, 1]], { width: 2 }),
7+
];
8+
9+
const pushCharToDirection: Record<string, number> = { "<": -1, ">": 1 };
10+
11+
export default function solve(input: string) {
12+
const chamber: boolean[][] = [];
13+
for (let i = 0, j = 0; i < 2022; i++) {
14+
const shape = shapes[i % shapes.length];
15+
for (let x = 2, y = chamber.length + 3; y >= 0; y--) {
16+
const dx = pushCharToDirection[input[j++ % input.length]];
17+
if (
18+
x + dx >= 0 && x + dx + shape.width <= 7 &&
19+
!shape.some(([rx, ry]) => chamber[y + ry]?.[x + rx + dx])
20+
) x += dx;
21+
if (y === 0 || shape.some(([rx, ry]) => chamber[y + ry - 1]?.[x + rx])) {
22+
for (const [rx, ry] of shape) {
23+
chamber[y + ry] ??= Array.from({ length: 7 }, () => false);
24+
chamber[y + ry][x + rx] = true;
25+
}
26+
break;
27+
}
28+
}
29+
}
30+
return chamber.length;
31+
}

0 commit comments

Comments
 (0)