-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11Part1.js
199 lines (171 loc) · 6.29 KB
/
day11Part1.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
// parse input
// (separate function for incrementing adjacent octopuses (i.e. function that gets adjacent octopuses and calls the increment by 1 function on them) - call in next step)
// increment one day function - calls increment by 1 function
// increment by 1
// if less than 9, increase by 1
// if 9, become 0; increment flashes; increment adjacent octopuses (i.e. call function)
// function that takes days as parameter; increments days; returns flashes
// ---
// (rewrote this to start again, but in the process realised that the issue was my definition of adjacent)
// one step:
// run increment function on every octopus
// increment function:
// increment
// where energy level > 9
// increment flashes
// run increment function on adjacent
// set energy levels above 9 back to 0
// ---
function readInput(fileName) {
const fs = require('fs');
const inputArray = [];
try {
const data = fs.readFileSync(fileName, 'UTF-8');
const lines = data.split(/\r?\n/);
lines.forEach((line) => {
inputArray.push(line.split(''));
});
} catch (err) {
console.error(err);
}
// inputArray.forEach(line => {
// line.forEach(number => {
// number = parseInt(number);
// });
// });
for (let i = 0; i < inputArray.length; i++) {
for (let j = 0; j < inputArray[i].length; j++) {
inputArray[i][j] = parseInt(inputArray[i][j]);
}
}
console.log(inputArray);
return inputArray;
}
// takes ints
function getAdjacentCoordinates(i, j) {
let adjacentCoordinates = [];
let validIndexes = [];
// writing it out like this for readability
adjacentCoordinates.push((i-1).toString() + ',' + j.toString());
adjacentCoordinates.push((i+1).toString() + ',' + j.toString());
adjacentCoordinates.push(i.toString() + ',' + (j-1).toString());
adjacentCoordinates.push(i.toString() + ',' + (j+1).toString());
// diagonals that I didn't realise were included
adjacentCoordinates.push((i-1).toString() + ',' + (j-1).toString());
adjacentCoordinates.push((i-1).toString() + ',' + (j+1).toString());
adjacentCoordinates.push((i+1).toString() + ',' + (j-1).toString());
adjacentCoordinates.push((i+1).toString() + ',' + (j+1).toString());
adjacentCoordinates.forEach(point => {
if (!point.includes('-') && !point.includes('10')) {
validIndexes.push(point);
}
});
console.log(validIndexes);
return validIndexes;
}
// 'increment' given point function (does other stuff)
// increment point unless 9
// 9s change to 0 (and increment flash count), plus 'increment' adjacent points
// takes ints
function incrementPoint(inputArray, i, j, flashCount, flashedArray) {
let adjacentCoordinates = getAdjacentCoordinates(i, j);
console.log(adjacentCoordinates);
// if (inputArray[i][j] < 9) {
// inputArray[i][j]++;
// } else {
// inputArray[i][j] = 0;
// flashedArray.push(i.toString() + ',' + j.toString());
// flashCount++;
// adjacentCoordinates.forEach(coordinates => {
// let coordinateArray = coordinates.split(',');
// let newI = parseInt(coordinateArray[0]);
// let newJ = parseInt(coordinateArray[1]);
// console.log('newI, newJ', newI, newJ);
// incrementPoint(inputArray, newI, newJ, flashCount, flashedArray);
// });
// }
inputArray[i][j]++;
if (inputArray[i][j] > 9) {
console.log(flashCount);
flashCount++;
console.log(flashCount);
flashedArray.push(i.toString() + ',' + j.toString());
inputArray[i][j] = 0;
adjacentCoordinates.forEach(coordinates => {
let coordinateArray = coordinates.split(',');
let newI = parseInt(coordinateArray[0]);
let newJ = parseInt(coordinateArray[1]);
console.log('newI, newJ', newI, newJ);
incrementPoint(inputArray, newI, newJ, flashCount, flashedArray);
});
}
console.log(inputArray);
console.log('current flash count:', flashCount);
}
function step(inputArray, flashCount) {
let flashedArray = [];
for (let i = 0; i < inputArray.length; i++) {
for (let j = 0; j < inputArray[i].length; j++) {
incrementPoint(inputArray, i, j, flashCount, flashedArray);
}
}
for (let k = 0; k < flashedArray; k++) {
let coordinates = flashedArray[k].split(',');
inputArray[coordinates[0]][coordinates[1]] = 0;
}
console.log(inputArray);
console.log(flashCount);
}
// function incrementDay(fileName, inputArray, flashCount) {
// for (let i = 0; i < inputArray.length; i++) {
// for (let j = 0; j < inputArray[i].length; j++) {
// incrementPoint(inputArray, i, j, flashCount);
// }
// }
// console.log(flashCount);
// }
// // "days" is supposed to be steps
// function incrementDays(fileName, flashCount, days) {
// const inputArray = readInput(fileName);
// for (let i = 0; i < days; i++) {
// incrementDay(fileName, inputArray, flashCount);
// }
// console.log(flashCount);
// }
function runTests() {
// readInput('day11TestInput.txt');
// getAdjacentCoordinates(0, 0);
// getAdjacentCoordinates(0, 9);
// getAdjacentCoordinates(9, 0);
// getAdjacentCoordinates(9, 9);
// getAdjacentCoordinates(9, 5);
// getAdjacentCoordinates(3, 2);
// getAdjacentCoordinates(6, 7);
// let inputArray = [[0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0]];
// incrementPoint(inputArray, 0, 0, 0);
// let inputArray2 = [[0, 0, 0, 0, 0],
// [0, 0, 9, 0, 0],
// [0, 0, 9, 0, 0],
// [0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0]];
// incrementPoint(inputArray2, 2, 2, 0);
// let inputArray3 = [[1, 1, 1, 1, 1],
// [1, 9, 9, 9, 1],
// [1, 9, 1, 9, 1],
// [1, 9, 9, 9, 1],
// [1, 1, 1, 1, 1]];
// // incrementPoint(inputArray3, 2, 2, 0);
// step(inputArray3, 0);
let inputArray4 = [[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 9, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]];
// incrementPoint(inputArray3, 2, 2, 0);
step(inputArray4, 0);
}
runTests();