-
Notifications
You must be signed in to change notification settings - Fork 2
/
day23.js
215 lines (194 loc) · 5.83 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
'use strict';
const fs = require('fs');
const cl = console.log;
const empty = -19;
fs.readFile('day23.txt', 'utf-8', (err, input) => {
if (err) throw err;
const lines1 = input.trim().split(/\r?\n/);
const lines2 = lines1.slice(0, 3).concat(
[' #D#C#B#A#', ' #D#B#A#C#'],
lines1.slice(3));
const [hallway1, rooms1] = newBurrow(lines1);
cl(amphipod(hallway1, rooms1));
const [hallway2, rooms2] = newBurrow(lines2);
cl(amphipod(hallway2, rooms2));
});
function newBurrow(lines) {
const rooms = [];
for (let x = 3; x < 10; x += 2) {
const room = [];
room.pos = x - 1;
for (let y = 2; y < lines.length - 1; y++) {
room.push(ampiNum(lines[y][x]))
}
rooms.push(room);
}
return [new Array(11).fill(empty), rooms];
}
function amphipod(hallway, rooms) {
const bestCost = { cost: Infinity };
const histories = burrowCost(hallway, rooms, 0, bestCost, []);
let history;
for (history of histories);
// displayHistory(history);
return history.at(-1)[2];
}
function* burrowCost(hallway, rooms, cost, bestCost, history) {
if (cost >= bestCost.cost) {
return;
}
history = history.slice(0)
history.push([hallway, rooms, cost]);
if (allRoomsDone(rooms)) {
bestCost.cost = Math.min(bestCost.cost, cost);
yield history;
} else {
yield* moveIn(hallway, rooms, cost, bestCost, history);
yield* moveOut(hallway, rooms, cost, bestCost, history);
}
}
function* moveIn(origHallway, origRooms, cost, bestCost, history) {
for (let origin = 0; origin < origHallway.length; origin++) {
if (!isEmpty(origHallway[origin])) {
const ampi = origHallway[origin];
if (!hasVisitor(origRooms, ampi)
&& hallIsClear(origHallway, origin, origRooms[ampi].pos)
) {
let [hallway, rooms] = copy(origHallway, origRooms);
const home = rooms[ampi];
let dist = Math.abs(origin - home.pos);
let i = 0;
for (; i < home.length && isEmpty(home[i + 1]); i++);
dist += (i + 1);
home[i] = ampi;
hallway[origin] = empty;
const newCost = cost + ampiCost(ampi) * dist;
yield*
burrowCost(hallway, rooms, newCost, bestCost, history);
}
}
}
}
function hallIsClear(hallway, origin, homePos) {
const step = (origin - homePos) / Math.abs(origin - homePos);
for (let i = homePos; i != origin; i += step) {
if (!isEmpty(hallway[i])) {
return false;
}
}
return true;
}
function* moveOut(origHall, origRooms, cost, bestCost, history) {
for (let rmIdx = 0; rmIdx < origRooms.length; rmIdx++) {
if (!roomDone(origRooms, rmIdx) && hasVisitor(origRooms, rmIdx)) {
const destinations =
hallwayDestinations(origHall, origRooms[rmIdx].pos);
for (let destination of destinations) {
let [hallway, rooms] = copy(origHall, origRooms);
const room = rooms[rmIdx];
let stepsOut;
let i = 0;
for (; isEmpty(room[i]); i++);
hallway[destination] = room[i];
room[i] = empty;
stepsOut = i + 1;
const newCost = cost + ampiCost(hallway[destination]) *
(stepsOut + Math.abs(room.pos - destination));
yield*
burrowCost(hallway, rooms, newCost, bestCost, history);
}
}
}
}
function* hallwayDestinations(hallway, start) {
for (let i = start + 1; i < hallway.length; i++) {
if (!isLobby(i)) {
if (!isEmpty(hallway[i])) {
break
}
yield i;
}
}
for (let i = start - 1; i >= 0; i--) {
if (!isLobby(i)) {
if (!isEmpty(hallway[i])) {
break
}
yield i;
}
}
}
function allRoomsDone(rooms) {
for (let i = 0; i < rooms.length; i++) {
if (!roomDone(rooms, i)) {
return false;
}
}
return true;
}
function roomDone(rooms, ampiHome) {
for (const val of rooms[ampiHome]) {
if (val != ampiHome) {
return false;
}
}
return true;
}
function hasVisitor(rooms, ampiHome) {
for (const val of rooms[ampiHome]) {
if (!isEmpty(val) && val != ampiHome) {
return true;
}
}
return false;
}
function isLobby(i) {
switch (i) {
case 2:
case 4:
case 6:
case 8:
return true;
default:
return false;
}
}
function isEmpty(x) {
return x === empty;
}
function copy(hallway, rooms) {
return [
hallway.slice(0),
rooms.map(room => {
const newRoom = room.slice(0);
newRoom.pos = room.pos;
return newRoom;
})
];
}
function ampiNum(ampi) {
return ampi.charCodeAt(0) - 65;
}
function ampiCost(ampi) {
return 10 ** ampi;
}
function displayHistory(history) {
let prevCost = 0;
for (const [hallway, rooms, cost] of history) {
const diff = cost - prevCost;
prevCost = cost;
cl(' ');
cl(`cost: ${cost} (+ ${diff})`);
displayBurrow(hallway, rooms);
}
}
function displayBurrow(hallway, rooms) {
const toChar = (ascii) => String.fromCharCode(ascii + 65);
cl('#############');
cl('#' + hallway.map(toChar).join('') + '#');
cl(`###${toChar(rooms[0][0])}#${toChar(rooms[1][0])}#${toChar(rooms[2][0])}#${toChar(rooms[3][0])}###`)
for (let i = 1; i < rooms[0].length; i++) {
cl(` #${toChar(rooms[0][i])}#${toChar(rooms[1][i])}#${toChar(rooms[2][i])}#${toChar(rooms[3][i])}#`);
}
cl(' #########');
}