Day 10: Pipe Maze
On first glance the puzzle reminded me of the flow puzzles from Bioshock, and I've been playing them a lot recently replaying through the first game. I also thought it was going to be very convoluted, but as I thought about it more, I had a fairly straight forward plan. That plan worked, but implementing it was very difficult.
Yet again I had a lot of trouble with the recursive loop, debugging what would be the end point for it, and many bugs and hiccups along the way. Got there in the end, and I probably spent at least 3.5 hours on it. I think I need to take up Tim's advice to start writing unit tests and learning more about that. So that's on the horizon now for things to learn.
Looking at part two now – just no. I could probably get there in the end, but I don't have time for it really. It looks very challenging and will likely mean intense computation, so another part of the challenge will be writing a very elegant solution.
Please note, running the real input in browser could crash it.
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
const inputToUse = testInput;
const map = [];
const inputLines = inputToUse.split(/\n/);
inputLines.forEach(function(line,i) {
const trimmedLine = line.trim();
const nodes = trimmedLine.split('');
nodes.forEach(function(node,j) {
var isStartNode = false;
if( node == 'S' ) {
isStartNode = true;
}
const obj = {
coords:[i,j],
value: node,
isStartNode: isStartNode
}
map.push(obj);
});
});
console.log('fresh map', map);
// takes a start position, returns an array of connected positions
function getConnectedNodes(map, currentCoords, prevCoords) {
// console.log('Getting connected nodes');
// console.log('prevCoords',prevCoords);
console.log('--- currentCoords',currentCoords);
const currentNode = Object.values(map).find( (obj) => {
return obj.coords.toString() === currentCoords.toString();
});
const currentNodeValue = currentNode.value;
const upCoords = [currentCoords[0]-1, currentCoords[1]];
const downCoords = [currentCoords[0]+1, currentCoords[1]];
const rightCoords = [currentCoords[0], currentCoords[1]+1];
const leftCoords = [currentCoords[0], currentCoords[1]-1];
var excludeUpNode = false;
var excludeDownNode = false;
var excludeRightNode = false;
var excludeLeftNode = false;
if( prevCoords ) {
if( upCoords.toString() == prevCoords.toString() ) {
excludeUpNode = true;
}
if( rightCoords.toString() == prevCoords.toString() ) {
excludeRightNode = true;
}
if( downCoords.toString() == prevCoords.toString() ) {
excludeDownNode = true;
}
if( leftCoords.toString() == prevCoords.toString() ) {
excludeLeftNode = true;
}
}
if( currentNodeValue == '-' ) {
excludeUpNode = true;
excludeDownNode = true;
}
if( currentNodeValue == '|' ) {
excludeLeftNode = true;
excludeRightNode = true;
}
if( currentNodeValue == 'L' ) {
excludeLeftNode = true;
excludeDownNode = true;
}
if( currentNodeValue == 'F' ) {
excludeLeftNode = true;
excludeUpNode = true;
}
if( currentNodeValue == '7' ) {
excludeUpNode = true;
excludeRightNode = true;
}
if( currentNodeValue == 'J' ) {
excludeRightNode = true;
excludeDownNode = true;
}
const connectedNodes = [];
if( !excludeUpNode && upCoords[0] >= 0 ) {
const upNode = Object.values(map).find( (obj) => {
return obj.coords.toString() === upCoords.toString();
});
const upNodeValue = upNode.value;
if( upNodeValue === '|' ||
upNodeValue === '7' ||
upNodeValue === 'F' ||
upNodeValue === 'S' ) {
connectedNodes.push(upNode);
}
} else {
console.log('upCoords are disallowed');
}
if( !excludeRightNode ) {
const rightNode = Object.values(map).find( (obj) => {
return obj.coords.toString() === rightCoords.toString();
});
// console.log('rightNode',rightNode);
if( rightNode ) {
console.log('rightCoords',rightCoords);
const rightNodeValue = rightNode.value;
// console.log('rightNodeValue',rightNodeValue);
if( rightNodeValue === '-' ||
rightNodeValue === '7' ||
rightNodeValue === 'J' ||
rightNodeValue === 'S' ) {
connectedNodes.push(rightNode);
}
} else {
console.log('rightCoords are disallowed');
}
} else {
console.log('rightCoords are disallowed');
}
if( !excludeDownNode ) {
const downNode = Object.values(map).find( (obj) => {
return obj.coords.toString() === downCoords.toString();
});
if( downNode ) {
console.log('downNode',downNode);
const downNodeValue = downNode.value;
console.log('downCoords',downCoords);
console.log('downNodeValue',downNodeValue);
if( downNodeValue === '|' ||
downNodeValue === 'J' ||
downNodeValue === 'L' ||
downNodeValue === 'S' ) {
connectedNodes.push(downNode);
}
} else {
console.log('downCoords are disallowed');
}
} else {
console.log('downCoords are disallowed');
}
if( !excludeLeftNode && leftCoords[1] >= 0 ) {
console.log('leftCoords',leftCoords);
const leftNode = Object.values(map).find( (obj) => {
return obj.coords.toString() === leftCoords.toString();
});
const leftNodeValue = leftNode.value;
if( leftNodeValue === '-' ||
leftNodeValue === 'F' ||
leftNodeValue === 'L' ||
leftNodeValue === 'S' ) {
connectedNodes.push(leftNode);
}
} else {
console.log('leftCoords are disallowed');
}
return connectedNodes;
}
const startNode = Object.values(map).find( (obj) =>{
return obj.isStartNode === true;
});
console.log('startNode',startNode);
const startNodeCoords = startNode.coords;
// const startNodeCoords = [3,4];
const startPaths = getConnectedNodes(map, startNodeCoords, null);
console.log('startPaths',startPaths);
startPaths.forEach(function(pathStartNode, i) {
var pathEnded = false;
var stepsFromStart = 1;
var examinedCoords = pathStartNode.coords;
var prevCoords = startNodeCoords;
pathStartNode.stepsFromStart = stepsFromStart;
var loops = 0;
while( !pathEnded ) {
console.log('––– Start a loop');
const connectedNodes = getConnectedNodes(map, examinedCoords, prevCoords);
const nextNode = connectedNodes[0];
if( nextNode ) {
// console.log('nextNode',nextNode);
const nextNodeCoords = nextNode.coords;
if( nextNodeCoords.toString() === startNodeCoords.toString() ) {
pathEnded = true;
} else {
stepsFromStart = stepsFromStart + 1;
// if it's already has a stepsFromStart distance, we made need to overwrite it
const existingStepsFromStart = nextNode.stepsFromStart;
if( existingStepsFromStart ) {
if( stepsFromStart < existingStepsFromStart ) {
nextNode.stepsFromStart = stepsFromStart;
}
} else {
nextNode.stepsFromStart = stepsFromStart;
}
// switch to examine the next node in path
prevCoords = examinedCoords;
examinedCoords = nextNode.coords;
}
}
else {
pathEnded = true;
}
}
});
console.log('map with steps',map);
var furthestStep = 0;
map.forEach(function(node) {
const distanceFromStart = node.stepsFromStart;
if( distanceFromStart ) {
if( distanceFromStart > furthestStep ) {
furthestStep = distanceFromStart;
}
}
});
console.log('furthestStep',furthestStep);