-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday22.js
154 lines (136 loc) · 4.1 KB
/
day22.js
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
'use strict';
const fs = require('fs');
const cl = console.log;
fs.readFile('day22.txt', 'utf-8', (err, input) => {
if (err) throw err;
const lines = input.trim().split(/\r?\n/)
function cubs() {
return lines.map(line => {
let power = line.substring(0, 3) === 'on ';
let bounds = line.match(/-?\d+/g).map(Number);
return newCubList(power, bounds);
});
}
cl(reactorReboot(cubs().map(trim50).filter(isReal)));
cl(reactorReboot(cubs()));
});
function reactorReboot(cubs) {
let reactor = [];
for (let cub of cubs) {
reactor = add(reactor, cub);
}
let count = 0;
for (let cub of reactor) {
const sz = size(cub);
count += sz;
}
return count;
}
function* add(reactor, cub) {
for (let existing of reactor) {
if (areSeparate(existing, cub)) {
yield existing;
} else {
yield* update(existing, cub);
}
}
if (cub.power) {
yield cub
};
}
function* update(old, cub) {
if (areSeparate(old, cub)) {
yield old;
}
else {
yield* [
left(old.dimensions, cub.dimensions),
right(old.dimensions, cub.dimensions),
top(old.dimensions, cub.dimensions),
bottom(old.dimensions, cub.dimensions),
back(old.dimensions, cub.dimensions),
front(old.dimensions, cub.dimensions)
].map(dimensions => newCub(old.power, dimensions))
.filter(isReal);
}
}
// With X=left/right, y=up/down, z=front/back
// ##########
// ### # top # ###
// # # ########## # #
// #l# #r#
// #e# ########## #i#
// #f# # front # #g#
// #t# ########## #h#
// # # #t#
// # # ########## # #
// ### # bottom # ###
// ##########
function left([[x1Low], yDims1, zDims1], [[x2Low]]) {
return [[x1Low, x2Low - 1], yDims1, zDims1];
}
function right([[_x1Low, x1High], yDims1, zDims1], [[_x2Low, x2High]]) {
return [[x2High + 1, x1High], yDims1, zDims1];
}
function bottom([[x1Low, x1High], [y1Low], zDims1]
, [[x2Low, x2High], [y2Low]]) {
const xDim = [Math.max(x1Low, x2Low), Math.min(x1High, x2High)];
return [xDim, [y1Low, y2Low - 1], zDims1];
}
function top([[x1Low, x1High], [_y1Low, y1High], zDims1]
, [[x2Low, x2High], [_y2Low, y2High]]) {
const xDim = [Math.max(x1Low, x2Low), Math.min(x1High, x2High)];
return [xDim, [y2High + 1, y1High], zDims1];
}
function back([[x1Low, x1High], [y1Low, y1High], [z1Low]]
, [[x2Low, x2High], [y2Low, y2High], [z2Low]]) {
const xDim = [Math.max(x1Low, x2Low), Math.min(x1High, x2High)];
const yDim = [Math.max(y1Low, y2Low), Math.min(y1High, y2High)];
return [xDim, yDim, [z1Low, z2Low - 1]];
}
function front([[x1Low, x1High], [y1Low, y1High], [_z1Low, z1High]]
, [[x2Low, x2High], [y2Low, y2High], [_z2Low, z2High]]) {
const xDim = [Math.max(x1Low, x2Low), Math.min(x1High, x2High)];
const yDim = [Math.max(y1Low, y2Low), Math.min(y1High, y2High)];
return [xDim, yDim, [z2High + 1, z1High]];
}
function areSeparate(cub1, cub2) {
for (let i = 0; i < cub1.dimensions.length; i++) {
let [low1, high1] = cub1.dimensions[i];
let [low2, high2] = cub2.dimensions[i];
if (low1 > high2 || low2 > high1) {
return true;
}
}
return false;
}
function isReal(cub) {
for (let [low, high] of cub.dimensions) {
if (high < low) {
return false;
}
}
return true;
}
function size(cub) {
const length = ([low, high]) => 1 + high - low;
return cub.dimensions.map(length).reduce((a, b) => a * b);
}
function trim50(cub) {
cub.dimensions = cub.dimensions.map(([low, high]) => [Math.max(low, -50), Math.min(high, 50)]);
return cub;
}
function newCubList(power, list) {
const dimensions = [
list.slice(0, 2),
list.slice(2, 4),
list.slice(4, 6)
]
return newCub(power, dimensions);
}
function newCub(power, dimensions) {
return {
power,
dimensions
};
}