-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.js
237 lines (205 loc) · 5.31 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
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
const fs = require('fs');
/*
...#
.#..
#...
....
...#.......#
........#...
..#....#....
..........#.
...#....
.....#..
.#......
.#......
......#.
*/
const END = 'E';
const LEFT = 'L';
const UP = 'U';
const RIGHT = 'R';
const DOWN = 'D';
const character = (direction) => {
switch (direction) {
case LEFT: return '<';
case RIGHT: return '>';
case UP: return '^';
case DOWN: return 'v';
}
}
const mod = (x, m) => (x % m + m) % m;
const day22a = () => {
// const filename = './data/day22test.txt'; // 6032
const filename = './data/day22.txt'; // 191010
const [grid, instructions] = buildGrid(filename);
// prettyPrint(grid);
// console.log(instructions);
// You begin the path in the leftmost open tile of the top row of tiles.
// Initially, you are facing to the right (from the perspective of how the map is drawn).
pos = [0, grid[0].indexOf('.')];
let direction = RIGHT;
grid[pos[0]][pos[1]] = '>';
let moveStr = '';
let stepsToTake = 0;
let count = 0;
while (instructions.length > 0) {
const inst = instructions.shift();
if ([LEFT, RIGHT, END].includes(inst)) {
// move
stepsToTake = +moveStr;
// console.log(direction, stepsToTake)
switch (direction) {
case LEFT:
pos = stepLeft(pos, stepsToTake, grid);
break;
case RIGHT:
pos = stepRight(pos, stepsToTake, grid);
break;
case UP:
pos = stepUp(pos, stepsToTake, grid);
break;
case DOWN:
pos = stepDown(pos, stepsToTake, grid);
break;
}
if (inst === END) break; // exit end of instruction.
// update direction
direction = changeDirection(direction, inst);
grid[pos[0]][pos[1]] = character(direction);
// console.log(pos, direction);
moveStr = '';
count += 1;
} else {
moveStr += inst;
}
}
// The final password is the sum of 1000 times the row, 4 times the column, and the facing.
// Facing is 0 for right (>), 1 for down (v), 2 for left (<), and 3 for up (^).
let directionValue;
switch (direction) {
case RIGHT: directionValue = 0; break;
case DOWN: directionValue = 1; break;
case LEFT: directionValue = 2; break;
case UP: directionValue = 3; break;
}
const answer = (1000 * (pos[0] + 1)) + (4 * (pos[1] + 1)) + directionValue;
console.log('day22a = ', answer);
// prettyPrint(grid);
}
const stepRight = (pos, steps, grid) => {
let [row, col] = pos;
let newCol = col;
const len = grid[0].length;
while (steps > 0) {
let testCol = mod((col + 1), len);
if (grid[row][testCol] === '#') {
break;
} else if (grid[row][testCol] === ' ') {
col = testCol;
} else {
grid[row][testCol] = '>';
col = testCol;
newCol = testCol;
steps -= 1;
}
}
return [row, newCol];
}
const stepLeft = (pos, steps, grid) => {
let [row, col] = pos;
let newCol = col;
const len = grid[0].length;
while (steps > 0) {
let testCol = mod((col - 1), len);
if (grid[row][testCol] === '#') {
break;
} else if (grid[row][testCol] === ' ') {
col = testCol;
} else {
grid[row][testCol] = '<';
col = testCol;
newCol = testCol;
steps -= 1;
}
}
return [row, newCol];
}
const stepUp = (pos, steps, grid) => {
let [row, col] = pos;
let newRow = row;
const len = grid.length;
while (steps > 0) {
let testRow = mod(row - 1, len);
if (grid[testRow][col] === '#') {
break;
} else if (grid[testRow][col] === ' ') {
row = testRow;
} else {
grid[testRow][col] = '^';
row = testRow;
newRow = testRow;
steps -= 1;
}
}
return [newRow, col];
}
const stepDown = (pos, steps, grid) => {
let [row, col] = pos;
let newRow = row;
const len = grid.length;
while (steps > 0) {
let testRow = mod(row + 1, len);
if (grid[testRow][col] === '#') {
break;
} else if (grid[testRow][col] === ' ') {
row = testRow;
} else {
grid[testRow][col] = 'v';
row = testRow;
newRow = testRow;
steps -= 1;
}
}
return [newRow, col];
}
const changeDirection = (oldDirection, directionStr) => {
let newDirection = '';
switch (oldDirection) {
case LEFT:
newDirection = (directionStr === LEFT) ? DOWN : UP;
break;
case RIGHT:
newDirection = (directionStr === LEFT) ? UP : DOWN;
break;
case UP:
newDirection = (directionStr === LEFT) ? LEFT : RIGHT;
break;
case DOWN:
newDirection = (directionStr === LEFT) ? RIGHT : LEFT;
break;
}
return newDirection;
}
const buildGrid = (filename) => {
let arr = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' }).split('\n');
// get the width of the board.
const width = Math.max(...arr.slice(0, -2).map(l => l.length));
const grid = [];
// build the grid.
for (let line of arr.slice(0, -2)) {
line = line.padEnd(width, ' ');
grid.push([...line]);
}
// extract the move instructions.
const instructions = Array.from(arr.slice(-1)[0]);
// add an END cap.
instructions.push(END);
return [grid, instructions];
}
prettyPrint = (grid) => {
grid.forEach(row => {
console.log(row.join(''));
});
}
module.exports = { day22a };