-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ts
67 lines (57 loc) · 2.08 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
import { readFileSync } from 'fs';
let input = readFileSync(__dirname + '/input.txt', 'utf-8');
let wind = input.trim().split('');
const width = 7;
const shapes = [
[[0, 0], [1, 0], [2, 0], [3, 0]], // Rock shape: –
[[1, 0], [0, 1], [1, 1], [2, 1], [1, 2]], // Rock shape: +
[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2]], // Rock shape: ⅃
[[0, 0], [0, 1], [0, 2], [0, 3]], // Rock shape: |
[[0, 0], [1, 0], [0, 1], [1, 1]], // Rock shape: □
]
class Rock {
points: number[][];
constructor(index: number, height: number) {
let shape = shapes[index % shapes.length].slice();
this.points = shape.map(coord => [coord[0] + 2, coord[1] + height + 3]);
}
canMove(offset: number[], tower: Tower): boolean {
for (const point of this.points) {
let newPoint = [point[0] + offset[0], point[1] + offset[1]];
if (tower.rocks.includes(newPoint.join(','))) return false; // Would intersect current rock
if (newPoint[0] < 0 || newPoint[0] >= width || newPoint[1] < 0) return false; // Would be out of bounds
}
return true;
}
move(offset: number[]) {
this.points = this.points.map(p => [p[0] + offset[0], p[1] + offset[1]]);
}
}
class Tower {
rocks: string[] = [];
height: number = 0;
constructor() {
}
addRock(rock: Rock) {
for (const point of rock.points) {
if (point[1] >= this.height) this.height = point[1] + 1;
this.rocks.push(point.join(','));
}
}
}
const rockCount = 2022;
let iteration = 0;
let tower = new Tower();
for (let i = 0; i < rockCount; i++) {
let falling = true;
let rock = new Rock(i, tower.height);
while(falling) {
let direction = wind[iteration++ % wind.length];
let offset = (direction == '<') ? [-1,0] : [1,0];
if (rock.canMove(offset, tower)) rock.move(offset);
let down = [0,-1];
rock.canMove(down, tower) ? rock.move(down) : falling = false;
}
tower.addRock(rock);
}
console.log('Answer:', tower.height);