-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-14-part-1.js
51 lines (41 loc) · 1 KB
/
day-14-part-1.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
const fs = require('fs');
const fileContents = fs.readFileSync('./day-14-input.txt', 'utf-8').split('\n');
let sum = 0;
const grid = [];
const getGridAsString = () => {
let result = '';
for (const row of grid) {
result += row.join('');
}
return result;
}
const tiltNorth = () => {
for (let row = 1; row < grid.length; row++) {
for (let col = 0; col < grid[0].length; col++) {
if (grid[row - 1][col] === '.' && grid[row][col] === 'O') {
grid[row - 1][col] = 'O';
grid[row][col] = '.';
}
}
}
};
for (const line of fileContents) {
grid.push(line.split(''));
}
let formerGridString = getGridAsString();
while (true) {
tiltNorth();
const newGridString = getGridAsString();
if (formerGridString === getGridAsString()) {
break;
}
formerGridString = newGridString;
}
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[0].length; col++) {
if (grid[row][col] === 'O') {
sum += grid.length - row
}
}
}
console.log(sum);