-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.ts
70 lines (62 loc) · 1.76 KB
/
part1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as fs from 'fs';
import type { Point } from '../../types';
const input = fs.readFileSync('input', 'utf8');
type Stream = { input: string; index: number };
const rocks = 2022;
const shapes = [
['####'],
['.#.', '###', '.#.'],
['..#', '..#', '###'],
['#', '#', '#', '#'],
['##', '##'],
].map((shape) => {
const cells: Point[] = [];
for (let y = 0; y < shape.length; y++) {
for (let x = 0; x < shape[y].length; x++) {
if (shape[y][x] === '#') {
cells.push({ x, y } as Point);
}
}
}
return { cells, shapeHeight: shape.length };
});
const stream: Stream = { input, index: 0 };
const cave: string[][] = [];
const drop = (cave: string[][], cells: Point[], stream: Stream) => {
const pos: Point = { x: 2, y: 0 };
let settled = false;
while (!settled) {
const direction = stream.input[stream.index++ % stream.input.length];
move(cave, pos, cells, { x: direction === '<' ? -1 : 1, y: 0 } as Point);
if (!move(cave, pos, cells, { x: 0, y: 1 } as Point)) {
cells.forEach((cell) => (cave[pos.y + cell.y][pos.x + cell.x] = '#'));
settled = true;
}
}
return pos.y;
};
const move = (cave: string[][], pos: Point, cells: Point[], offset: Point) => {
const next = { x: pos.x + offset.x, y: pos.y + offset.y };
if (cells.every((cell) => cave[next.y + cell.y]?.[next.x + cell.x] === '.')) {
pos.x += offset.x;
pos.y += offset.y;
return true;
}
return false;
};
let depth = 0;
let height = 0;
for (let i = 0; i < rocks; i++) {
const { cells, shapeHeight } = shapes[i % shapes.length];
while (depth < height + shapeHeight + 3) {
cave.unshift(new Array(7).fill('.'));
depth++;
}
while (depth > height + shapeHeight + 3) {
cave.shift();
depth--;
}
const y = drop(cave, cells, stream);
height = Math.max(height, depth - y);
}
console.log(height);