-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.js
300 lines (278 loc) · 10.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const encodeState = ({
roomA = [],
roomB = [],
roomC = [],
roomD = [],
cornerOne = [],
cornerTwo = [],
hallwayOne = [],
hallwayTwo = [],
hallwayThree = []
}) => {
return [
roomA.join(''),
roomB.join(''),
roomC.join(''),
roomD.join(''),
cornerOne.join(''),
cornerTwo.join(''),
hallwayOne.join(''),
hallwayTwo.join(''),
hallwayThree.join(''),
].join('|')
}
const isBlocked = (roomName, hallwayName, state) => {
if (hallwayName === 'cornerOne') {
if (roomName === 'roomA') return false
else if (roomName === 'roomB') return state.hallwayOne.length === 1
else if (roomName === 'roomC') return state.hallwayOne.length === 1 || state.hallwayTwo.length === 1
else if (roomName === 'roomD') return state.hallwayOne.length === 1 || state.hallwayTwo.length === 1 || state.hallwayThree.length === 1
} else if (hallwayName === 'cornerTwo') {
if (roomName === 'roomA') return state.hallwayThree.length === 1 || state.hallwayTwo.length === 1 || state.hallwayOne.length === 1
else if (roomName === 'roomB') return state.hallwayThree.length === 1 || state.hallwayTwo.length === 1
else if (roomName === 'roomC') return state.hallwayThree.length === 1
else if (roomName === 'roomD') return false
} else if (hallwayName === 'hallwayOne') {
if (roomName === 'roomA') return false
else if (roomName === 'roomB') return false
else if (roomName === 'roomC') return state.hallwayTwo.length === 1
else if (roomName === 'roomD') return state.hallwayTwo.length === 1 || state.hallwayThree.length === 1
} else if (hallwayName === 'hallwayTwo') {
if (roomName === 'roomA') return state.hallwayOne.length === 1
else if (roomName === 'roomB') return false
else if (roomName === 'roomC') return false
else if (roomName === 'roomD') return state.hallwayThree.length === 1
} else if (hallwayName === 'hallwayThree') {
if (roomName === 'roomA') return state.hallwayTwo.length === 1 || state.hallwayOne.length === 1
else if (roomName === 'roomB') return state.hallwayTwo.length === 1
else if (roomName === 'roomC') return false
else if (roomName === 'roomD') return false
}
}
const canBeEntered = (roomName, state) => state[roomName].every(el => el === roomName[4])
const costLookupTable = {
'A': 1,
'B': 10,
'C': 100,
'D': 1000,
}
const getCost = (roomName, hallwayName, state, element) => {
const previousCost = state.cost
const costMultiplier = costLookupTable[element]
let movementSteps = 0
// Move to hallway outside of room
movementSteps += 3 - state[roomName].length
if (allCorners.includes(hallwayName)) {
// Move to top of corner
movementSteps += 1 - state[hallwayName].length
}
if (hallwayName === 'cornerOne') {
if (roomName === 'roomA') movementSteps += 1
else if (roomName === 'roomB') movementSteps += 3
else if (roomName === 'roomC') movementSteps += 5
else if (roomName === 'roomD') movementSteps += 7
} else if (hallwayName === 'cornerTwo') {
if (roomName === 'roomA') movementSteps += 7
else if (roomName === 'roomB') movementSteps += 5
else if (roomName === 'roomC') movementSteps += 3
else if (roomName === 'roomD') movementSteps += 1
} else if (hallwayName === 'hallwayOne') {
if (roomName === 'roomA') movementSteps += 1
else if (roomName === 'roomB') movementSteps += 1
else if (roomName === 'roomC') movementSteps += 3
else if (roomName === 'roomD') movementSteps += 5
} else if (hallwayName === 'hallwayTwo') {
if (roomName === 'roomA') movementSteps += 3
else if (roomName === 'roomB') movementSteps += 1
else if (roomName === 'roomC') movementSteps += 1
else if (roomName === 'roomD') movementSteps += 3
} else if (hallwayName === 'hallwayThree') {
if (roomName === 'roomA') movementSteps += 5
else if (roomName === 'roomB') movementSteps += 3
else if (roomName === 'roomC') movementSteps += 1
else if (roomName === 'roomD') movementSteps += 1
}
return previousCost + (movementSteps * costMultiplier)
}
const allCorners = ['cornerOne', 'cornerTwo']
const allBetweenDoors = ['hallwayOne', 'hallwayTwo', 'hallwayThree']
const allHallways = [...allCorners, ...allBetweenDoors]
const allRooms = ['roomA', 'roomB', 'roomC', 'roomD'].reverse()
const dfsRecursiveMemoized = (state, cache) => {
// console.log(state)
// console.log(cache)
if (state.roomA && state.roomA.length === 4 && state.roomA.every(el => el === 'A') && state.cornerOne.length === 0 && state.cornerTwo.length === 0 && state.hallwayOne.length === 0 && state.hallwayTwo.length === 0 && state.hallwayThree.length === 0) {
return state
}
const encodedState = encodeState(state)
if (cache.has(encodedState)) return cache.get(encodedState)
let lowestCostState = { cost: Infinity }
const unfinishedRooms = allRooms.filter(roomName => !canBeEntered(roomName, state))
// From room to corner
allCorners.forEach(cornerName => {
if (state[cornerName].length < 2) {
unfinishedRooms.forEach(roomName => {
if (!isBlocked(roomName, cornerName, state)) {
const stateOfBranch = dfsRecursiveMemoized(
{
...state,
[roomName]: state[roomName].slice(1),
[cornerName]: [state[roomName][0], ...state[cornerName]],
cost: getCost(roomName, cornerName, state, state[roomName][0]),
moveHistory: [...state.moveHistory, `from ${roomName} to ${cornerName}`],
}
, cache)
if (stateOfBranch.cost < lowestCostState.cost) lowestCostState = stateOfBranch
// console.log(`from ${roomName} to ${cornerName}`)
}
})
}
})
// From room to hallway
allBetweenDoors.forEach(betweenDoorsName => {
if (state[betweenDoorsName].length === 0) {
unfinishedRooms.forEach(roomName => {
if (!isBlocked(roomName, betweenDoorsName, state)) {
const stateOfBranch = dfsRecursiveMemoized(
{
...state,
[roomName]: state[roomName].slice(1),
[betweenDoorsName]: [state[roomName][0], ...state[betweenDoorsName]],
cost: getCost(roomName, betweenDoorsName, state, state[roomName][0]),
moveHistory: [...state.moveHistory, `from ${roomName} to ${betweenDoorsName}`],
}
, cache)
if (stateOfBranch.cost < lowestCostState.cost) lowestCostState = stateOfBranch
// console.log(`from ${roomName} to ${betweenDoorsName}`)
}
})
}
})
const openRooms = allRooms.filter(roomName => canBeEntered(roomName, state))
// from hallway to room
openRooms.forEach(roomName => {
allHallways.forEach(hallwayName => {
if (state[hallwayName].length > 0 && state[hallwayName][0] === roomName[4]) {
if (!isBlocked(roomName, hallwayName, state)) {
const stateOfBranch = dfsRecursiveMemoized(
{
...state,
[roomName]: [state[hallwayName][0], ...state[roomName]],
[hallwayName]: state[hallwayName].slice(1),
cost: getCost(roomName, hallwayName, state, state[hallwayName][0]),
moveHistory: [...state.moveHistory, `from ${hallwayName} to ${roomName}`],
}
, cache)
if (stateOfBranch.cost < lowestCostState.cost) lowestCostState = stateOfBranch
// console.log(`from ${hallwayName} to ${roomName}`)
}
}
})
})
cache.set(encodedState, lowestCostState)
return lowestCostState
}
const printState = ({
roomA = [],
roomB = [],
roomC = [],
roomD = [],
cornerOne = [],
cornerTwo = [],
hallwayOne = [],
hallwayTwo = [],
hallwayThree = [],
cost = 0,
moveHistory = [],
}) => {
const x1 = cornerOne.reverse().join('') || '..'
const x2 = cornerTwo.join('') || '..'
const h1 = hallwayOne.length === 1 ? hallwayOne[0] : '.'
const h2 = hallwayTwo.length === 1 ? hallwayTwo[0] : '.'
const h3 = hallwayThree.length === 1 ? hallwayThree[0] : '.'
const a1 = roomA.length >= 1 ? roomA[0] : '.'
const a2 = roomA.length >= 2 ? roomA[1] : '.'
const a3 = roomA.length >= 3 ? roomA[2] : '.'
const a4 = roomA.length >= 4 ? roomA[3] : '.'
const b1 = roomB.length >= 1 ? roomB[0] : '.'
const b2 = roomB.length >= 2 ? roomB[1] : '.'
const b3 = roomB.length >= 3 ? roomB[2] : '.'
const b4 = roomB.length >= 4 ? roomB[3] : '.'
const c1 = roomC.length >= 1 ? roomC[0] : '.'
const c2 = roomC.length >= 2 ? roomC[1] : '.'
const c3 = roomC.length >= 3 ? roomC[2] : '.'
const c4 = roomC.length >= 4 ? roomC[3] : '.'
const d1 = roomD.length >= 1 ? roomD[0] : '.'
const d2 = roomD.length >= 2 ? roomD[1] : '.'
const d3 = roomD.length >= 3 ? roomD[2] : '.'
const d4 = roomD.length >= 4 ? roomD[3] : '.'
let grid = `cost: ${cost}
#############
#${x1}.${h1}.${h2}.${h3}.${x2}#
###${a1}#${b1}#${c1}#${d1}###
#${a2}#${b2}#${c2}#${d2}#
#${a3}#${b3}#${c3}#${d3}#
#${a4}#${b4}#${c4}#${d4}#
#########`
console.log(moveHistory.join('\n'))
console.log(grid)
}
// const roomA = [
// 'B',
// 'C',
// ]
// const roomB = [
// 'C',
// 'D',
// ]
// const roomC = [
// 'A',
// 'D',
// ]
// const roomD = [
// 'B',
// 'A',
// ]
const roomA = [
'B',
'D',
'D',
'C',
]
const roomB = [
'C',
'C',
'B',
'D',
]
const roomC = [
'A',
'B',
'A',
'D',
]
const roomD = [
'B',
'A',
'C',
'A',
]
const initialState = {
roomA: roomA,
roomB: roomB,
roomC: roomC,
roomD: roomD,
cornerOne: [],
cornerTwo: [],
hallwayOne: [],
hallwayTwo: [],
hallwayThree: [],
cost: 0,
moveHistory: [],
}
console.log('initialState: ', initialState)
printState(initialState)
const stateCache = new Map()
const partTwo = dfsRecursiveMemoized(initialState, stateCache)
console.log('partTwo: ', partTwo)
printState(partTwo)