-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
159 lines (154 loc) · 3.19 KB
/
index.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 run from 'aocrunner'
const parseInput = (rawInput: string) =>
rawInput
.trim()
.split('\n')
.map((line) => line.split(',').map((v) => +v))
const grid = (x: number, y: number, z: number) => [x, y, z].join(',')
const xyz = (grid: string) => grid.split(',').map((v) => +v)
const NEIGHBORS = [
[1, 0, 0],
[-1, 0, 0],
[0, 1, 0],
[0, -1, 0],
[0, 0, 1],
[0, 0, -1],
]
const neighbors = function* (x: number, y: number, z: number) {
for (const [nx, ny, nz] of NEIGHBORS) {
yield [x + nx, y + ny, z + nz]
}
}
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
const gridMap: Set<string> = new Set()
let surfaceArea = 0
for (const [x, y, z] of input) {
gridMap.add(grid(x, y, z))
surfaceArea += 6
for (const [nx, ny, nz] of neighbors(x, y, z)) {
const nGrid = grid(nx, ny, nz)
if (gridMap.has(nGrid)) surfaceArea -= 2
}
}
return surfaceArea.toString()
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
const cubeMap: Set<string> = new Set()
const spaceMap: Set<string> = new Set()
let surfaceArea = 0
const bounds = [Infinity, -Infinity, Infinity, -Infinity, Infinity, -Infinity]
for (const [x, y, z] of input) {
if (x < bounds[0]) bounds[0] = x
if (x > bounds[1]) bounds[1] = x
if (y < bounds[2]) bounds[2] = y
if (y > bounds[3]) bounds[3] = y
if (z < bounds[4]) bounds[4] = z
if (z > bounds[5]) bounds[5] = z
const thisGrid = grid(x, y, z)
cubeMap.add(thisGrid)
spaceMap.delete(thisGrid)
surfaceArea += 6
for (const [nx, ny, nz] of neighbors(x, y, z)) {
const nGrid = grid(nx, ny, nz)
if (cubeMap.has(nGrid)) {
surfaceArea -= 2
} else {
spaceMap.add(nGrid)
}
}
}
const allPocketCubes: Set<string> = new Set()
for (const space of [...spaceMap]) {
if (allPocketCubes.has(space)) continue
const toCheck: Set<string> = new Set([space])
const airCluster: Set<string> = new Set()
let clusterSurfaceArea = 0
let enclosed = true
while (enclosed && toCheck.size > 0) {
const [checking] = toCheck
const [x, y, z] = xyz(checking)
airCluster.add(checking)
toCheck.delete(checking)
clusterSurfaceArea += 6
for (const [nx, ny, nz] of neighbors(x, y, z)) {
if (
nx < bounds[0] ||
nx > bounds[1] ||
ny < bounds[2] ||
ny > bounds[3] ||
nz < bounds[4] ||
nz > bounds[5]
) {
// Out of bounds
enclosed = false
break
}
const nGrid = grid(nx, ny, nz)
const cubeInCluster = airCluster.has(nGrid)
if (cubeInCluster) clusterSurfaceArea -= 2
if (
!cubeMap.has(nGrid) &&
!cubeInCluster &&
!allPocketCubes.has(nGrid)
) {
toCheck.add(nGrid)
}
}
}
if (enclosed) {
surfaceArea -= clusterSurfaceArea
for (const space of [...airCluster]) {
allPocketCubes.add(space)
}
}
}
return surfaceArea.toString()
}
run({
part1: {
tests: [
{
input: `2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5`,
expected: '64',
},
],
solution: part1,
},
part2: {
tests: [
{
input: `2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5`,
expected: '58',
},
],
solution: part2,
},
trimTestInputs: true,
// onlyTests: true,
})