-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.js
192 lines (155 loc) · 4.45 KB
/
day14.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
const fs = require('fs');
const CONTINUE = 'continue';
const OVERFLOW = 'overflow';
const FUBAR = 'FUBAR';
const day14a = () => {
// const filename = './data/day14test.txt'; // 24
const filename = './data/day14.txt'; // 808
const [cs, ce, grid] = createGrid(filename);
// The sand is pouring into the cave from point 500,0
const sandStartRow = 0
const sandStartCol = 500;
let step = 1;
let result, r, c;
while (true) {
[result, [r, c]] = exp(sandStartRow, sandStartCol, grid);
grid[r][c] = 'o';
if (result === OVERFLOW) break;
if (result === FUBAR) {
// something wrong!
console.log(result);
prettyPrint(grid);
break;
}
step += 1;
}
console.log('day14a = ', step - 1);
}
const exp = (r, c, grid) => {
// overflow state when sand gets to the last row.
const extrarows = 2; // this is the last 2 rows added for question b.
if (r === grid.length-1-extrarows) return [OVERFLOW, [r,c]];
// dead-end state
if (grid[r + 1][c - 1] !== '.' &&
grid[r + 1][c] !== '.' &&
grid[r + 1][c + 1] !== '.') return [CONTINUE, [r, c]];
if (grid[r + 1][c] === '.') {
return (exp(r + 1, c, grid));
} else if (grid[r + 1][c - 1] === '.') {
return (exp(r + 1, c - 1, grid));
} else if (grid[r + 1][c + 1] === '.') {
return (exp(r + 1, c + 1, grid));
}
return false;
}
const expB = (r, c, grid) => {
// base-case
if (grid[0][500] !== '.') return [OVERFLOW, [r,c]];
// dead-end state
if (grid[r + 1][c - 1] !== '.' &&
grid[r + 1][c] !== '.' &&
grid[r + 1][c + 1] !== '.') return [CONTINUE, [r, c]];
if (grid[r + 1][c] === '.') {
return (expB(r + 1, c, grid));
} else if (grid[r + 1][c - 1] === '.') {
return (expB(r + 1, c - 1, grid));
} else if (grid[r + 1][c + 1] === '.') {
return (expB(r + 1, c + 1, grid));
}
return false;
}
const day14b = () => {
// const filename = './data/day14test.txt'; // 93
const filename = './data/day14.txt'; // 26625
const [cs, ce, grid] = createGrid(filename);
// The sand is pouring into the cave from point 500,0
const sandStartRow = 0
const sandStartCol = 500;
let step = 1;
let result, r, c;
while (true) {
[result, [r, c]] = expB(sandStartRow, sandStartCol, grid);
grid[r][c] = 'o';
// prettyPrint(grid, cs-1, ce+1);
if (result === OVERFLOW) break;
if (result === FUBAR) {
// something wrong!
console.log(result);
prettyPrint(grid);
break;
}
step += 1;
}
console.log('day14b = ', step - 1);
}
const createGrid = (filename) => {
const lines = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' }).split('\n');
let cs, ce;
let minR = Infinity, maxR = 0, minC = Infinity, maxC = 0;
const arr = [];
lines.forEach(line => {
const ptStrs = line.split('->');
let subArr = [];
ptStrs.forEach(ptStr => {
c = +ptStr.split(',')[0];
r = +ptStr.split(',')[1];
minR = 0;
maxR = Math.max(maxR, r);
minC = Math.min(minC, c);
maxC = Math.max(maxC, c);
subArr.push([r, c]);
});
arr.push(subArr);
});
cs = minC;
ce = maxC;
// increase the row by 2
// double the width.
minC = 0; // set the minimum C to 0
maxC *= 2; // increase the width of the column.
maxR += 2; // add the 2 extra rows for 2nd question but this will not affect question 1.
const grid = [];
for (let row = 0; row <= maxR; row++) {
// fill the floor.
if (row === maxR) {
grid.push(Array(maxC - minC + 1).fill('#'));
}
else {
grid.push(Array(maxC - minC + 1).fill('.'));
}
}
// fill the board.
for (let i = 0; i < arr.length; i++) {
let [r0, c0] = arr[i][0];
for (let j = 0; j < arr[i].length - 1; j++) {
const [r1, c1] = arr[i][j + 1]
// horizontal
if (r0 === r1) {
let cs = Math.min(c0, c1);
let ce = Math.max(c0, c1);
for (let y = cs; y <= ce; y++) {
grid[r0][y - minC] = '#';
}
}
// vertical
if (c0 === c1) {
let rs = Math.min(r0, r1);
let re = Math.max(r0, r1);
for (let x = rs; x < re; x++) {
grid[x][c0 - minC] = '#';
}
}
r0 = r1;
c0 = c1;
}
}
return [cs, ce, grid];
}
// prints inclusive of [colStart, colEnd]
prettyPrint = (grid, colStart, colEnd) => {
grid.forEach(row => {
console.log(row.slice(colStart, colEnd+1).join(''));
});
}
module.exports = { day14a, day14b }