-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23.js
255 lines (216 loc) · 6.38 KB
/
day23.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const fs = require('fs');
const NORTH = 0;
const SOUTH = 1;
const WEST = 2;
const EAST = 3;
const SEARCHDIRS = [NORTH, SOUTH, WEST, EAST];
const day23a = () => {
// const filename = './data/day23test.txt'; // 110
const filename = './data/day23.txt'; // 3925
let cells = buildGrid(filename);
// prettyPrint(cells);
let direction = NORTH;
proposedMap = new Map();
for (let round = 0; round < 10; round++) {
direction = SEARCHDIRS[round % 4];
proposedMap.clear();
// generate the proposed move
for (let cell of cells) {
if (cell.propose(cells, direction)) {
// console.log(`propose ${cell.id} : [${cell.row}-${cell.col}] to [${cell.newPos}]. ${direction}`);
const newKey = key(cell.newPos);
if (!proposedMap.has(newKey)) {
proposedMap.set(newKey, 1);
} else {
proposedMap.set(newKey, proposedMap.get(newKey) + 1);
}
}
}
// do the move.
for (let cell of cells) {
if (cell.canMove && proposedMap.get(key(cell.newPos)) === 1) {
cell.move();
}
}
// prettyPrint(cells);
}
// prettyPrint(cells);
// calculate the empty space.
const minRow = Math.min(...cells.map((c) => c.row));
const maxRow = Math.max(...cells.map((c) => c.row));
const minCol = Math.min(...cells.map((c) => c.col));
const maxCol = Math.max(...cells.map((c) => c.col));
const totalSpaces = (maxRow - minRow + 1) * (maxCol - minCol + 1);
console.log(`day23a = ${totalSpaces} - ${cells.length} = ${totalSpaces - cells.length}`);
}
// this takes several minutes to complete.
const day23b = () => {
// const filename = './data/day23test.txt'; // 20
const filename = './data/day23.txt'; // 903
let cells = buildGrid(filename);
// prettyPrint(cells);
let direction = NORTH;
proposedMap = new Map();
for (let round = 0; round < 1000; round++) {
direction = SEARCHDIRS[round % 4];
proposedMap.clear();
// generate the proposed move
for (let cell of cells) {
if (cell.propose(cells, direction)) {
// console.log(`propose ${cell.id} : [${cell.row}-${cell.col}] to [${cell.newPos}]. ${direction}`);
const newKey = key(cell.newPos);
if (!proposedMap.has(newKey)) {
proposedMap.set(newKey, 1);
} else {
proposedMap.set(newKey, proposedMap.get(newKey) + 1);
}
}
}
// do the move.
let hasMoves = false;
for (let cell of cells) {
if (cell.canMove && proposedMap.get(key(cell.newPos)) === 1) {
hasMoves = true;
cell.move();
}
}
if (!hasMoves) {
console.log('day23b = ', round + 1);
return;
}
// prettyPrint(cells);
}
console.log('day23b failed to find solution');
}
const prettyPrint = (cells) => {
const minRow = Math.min(...cells.map((c) => c.row));
const maxRow = Math.max(...cells.map((c) => c.row));
const minCol = Math.min(...cells.map((c) => c.col));
const maxCol = Math.max(...cells.map((c) => c.col));
const grid = [];
for (let r = minRow; r <= maxRow; r++) {
grid.push(Array.from(Array(maxCol - minCol + 1)).map((i) => '.'));
}
cells.forEach((v) => {
grid[v.row - minRow][v.col - minCol] = '#';
});
grid.forEach((line) => {
console.log(line.join(''));
});
console.log();
};
const buildGrid = (filename) => {
const cells = [];
let arr = fs.readFileSync(filename, { encoding: 'utf8', flag: 'r' }).split('\n');
for (let row = 0; row < arr.length; row++) {
for (let col = 0; col < arr[row].length; col++) {
if (arr[row][col] === '#') {
cells.push(new Cell(row, col, arr[row][col]));
}
}
}
return cells;
};
module.exports = { day23a, day23b };
const key = (pos) => {
return pos.join('-');
};
class Cell {
constructor(row, col, char) {
this.row = row;
this.col = col;
this.char = char;
this.canMove = false;
// the proposed move position
this.newPos = [0, 0];
}
propose = (cells, direction) => {
this.canMove = false;
// condition 1. no neighbor, no moving.
const dN = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
];
for (let [dr, dc] of dN) {
if (cells.find((c) => c.row === this.row + dr && c.col === this.col + dc)) {
this.canMove = true;
break;
}
}
if (!this.canMove) return false;
// condition 2.
for (let dir = direction; dir < direction + 4; dir++) {
switch (dir % 4) {
case NORTH:
if (
!cells.find(
(c) =>
(c.row === this.row - 1 && c.col === this.col - 1) ||
(c.row === this.row - 1 && c.col === this.col) ||
(c.row === this.row - 1 && c.col === this.col + 1)
)
) {
this.canMove = true;
this.newPos = [this.row - 1, this.col];
return true;
}
break;
case SOUTH:
if (
!cells.find(
(c) =>
(c.row === this.row + 1 && c.col === this.col - 1) ||
(c.row === this.row + 1 && c.col === this.col) ||
(c.row === this.row + 1 && c.col === this.col + 1)
)
) {
this.canMove = true;
this.newPos = [this.row + 1, this.col];
return true;
}
break;
case EAST:
if (
!cells.find(
(c) =>
(c.row === this.row - 1 && c.col === this.col + 1) ||
(c.row === this.row && c.col === this.col + 1) ||
(c.row === this.row + 1 && c.col === this.col + 1)
)
) {
this.canMove = true;
this.newPos = [this.row, this.col + 1];
return true;
}
break;
case WEST:
if (
!cells.find(
(c) =>
(c.row === this.row - 1 && c.col === this.col - 1) ||
(c.row === this.row && c.col === this.col - 1) ||
(c.row === this.row + 1 && c.col === this.col - 1)
)
) {
this.canMove = true;
this.newPos = [this.row, this.col - 1];
return true;
}
break;
}
}
this.canMove = false;
return false;
};
move = () => {
this.row = this.newPos[0];
this.col = this.newPos[1];
this.canMove = false;
};
}