-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.ts
159 lines (130 loc) · 3.8 KB
/
day22.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import _ from "lodash";
import fs from "fs";
const lines: string[] = _.reject(
fs.readFileSync("inputs/day22.txt", "utf-8").split("\n"),
_.isEmpty
);
interface Brick {
startX: number;
startY: number;
startZ: number;
endX: number;
endY: number;
endZ: number;
}
const parseBrickString = (line: string): Brick => {
const [start, end] = line.split("~");
const [startX, startY, startZ] = start.split(",").map(Number);
const [endX, endY, endZ] = end.split(",").map(Number);
return { startX, startY, startZ, endX, endY, endZ };
};
// Would brick A intersect brick B if it fell?
const intersects = (brickA: Brick, brickB: Brick): boolean => {
const xOverlap = [
Math.max(brickA.startX, brickB.startX),
Math.min(brickA.endX, brickB.endX),
];
const yOverlap = [
Math.max(brickA.startY, brickB.startY),
Math.min(brickA.endY, brickB.endY),
];
const zOverlap = [
Math.max(brickA.startZ, brickB.startZ),
Math.min(brickA.endZ, brickB.endZ),
];
return (
xOverlap[0] <= xOverlap[1] &&
yOverlap[0] <= yOverlap[1] &&
zOverlap[0] <= zOverlap[1]
);
};
// Can we move this brick down without occupying another brick's space in 3d?
const fallsFreely = (brick: Brick, fallenBricks: Brick[]): boolean => {
const brickCopy = _.cloneDeep(brick);
brickCopy.startZ--;
brickCopy.endZ--;
return (
brickCopy.startZ > 0 &&
fallenBricks.every((fallenBrick) => !intersects(brickCopy, fallenBrick))
);
};
// Simulate gravity on all of the bricks, returning a copy
const applyGravity = (bricks: Brick[]): Brick[] => {
const bricksCopy = _.cloneDeep(bricks);
for (let i = 1; i < bricksCopy.length; i++) {
while (true) {
let edited = false;
if (fallsFreely(bricksCopy[i], bricksCopy.slice(0, i))) {
bricksCopy[i].startZ--;
bricksCopy[i].endZ--;
edited = true;
}
if (!edited) {
break;
}
}
}
return bricksCopy;
};
/**
* (Part 1) Brick indices that this brick falls onto first by simulating gravity one unit at a time.
*
* If it hits the ground first (z = 0), then the empty list is returned.
*/
const fallsOntoFirst = (i: number): number[] => {
const brick = _.cloneDeep(bricks[i]);
while (brick.startZ > 0) {
brick.startZ--;
brick.endZ--;
const fallingBricks = _.range(bricks.length).filter(
(j) => j !== i && intersects(brick, bricks[j])
);
if (fallingBricks.length > 0) {
return fallingBricks;
}
}
return [];
};
/**
* (Part 2) How many bricks fall when brick i is removed?
*
* * Assumes that original bricks have gravity applied to them already and are sorted by startZ.
*/
const getFallingBricksCount = (i: number): number => {
let count = 0;
// Prefix before i is what bricks are stationary below us
const fallen = _.cloneDeep(bricks.slice(0, i));
// Suffix after i is what bricks may fall above us
const yetToFall = _.cloneDeep(bricks.slice(i + 1));
while (yetToFall.length > 0) {
const currentStartZ = yetToFall[0].startZ;
const fallingNow = [];
while (yetToFall.length > 0 && yetToFall[0].startZ === currentStartZ) {
fallingNow.push(yetToFall.shift()!);
}
fallingNow.forEach((brick) => {
if (fallsFreely(brick, fallen)) {
brick.startZ--;
brick.endZ--;
count++;
}
fallen.push(brick);
});
}
return count;
};
const bricks = applyGravity(
lines.map(parseBrickString).sort((a, b) => a.startZ - b.startZ)
);
const dangerousBricks = new Set<number>();
_.range(bricks.length)
.map(fallsOntoFirst)
.forEach((supporting) => {
if (supporting.length === 1) {
dangerousBricks.add(supporting[0]);
}
});
// Part 1
console.log(bricks.length - dangerousBricks.size);
// Part 2 (wait a few seconds)
console.log(_.sum(Array.from(dangerousBricks).map(getFallingBricksCount)));