-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14_2.js
105 lines (93 loc) · 2.3 KB
/
day14_2.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
const { parse } = require('path');
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream('input.txt'),
});
let data = [];
function tiltNorth() {
for (let row = 1; row < data.length; row++) {
for (let i = row; i > 0; i--) {
for (let j = 0; j < data[0].length; j++) {
if (data[i][j] === 'O' && data[i - 1][j] === '.') {
data[i - 1][j] = 'O';
data[i][j] = '.';
}
}
}
}
}
function tiltSouth() {
for (let row = data.length - 2; row >= 0; row--) {
for (let i = row; i < data.length - 1; i++) {
for (let j = 0; j < data[0].length; j++) {
if (data[i][j] === 'O' && data[i + 1][j] === '.') {
data[i + 1][j] = 'O';
data[i][j] = '.';
}
}
}
}
}
function tiltEast() {
for (let col = data[0].length - 2; col >= 0; col--) {
for (let j = col; j < data[0].length; j++) {
for (let i = 0; i < data.length; i++) {
if (data[i][j] === 'O' && data[i][j + 1] === '.') {
data[i][j + 1] = 'O';
data[i][j] = '.';
}
}
}
}
}
function tiltWest() {
for (let col = 0; col < data[0].length; col++) {
for (let j = col; j > 0; j--) {
for (let i = 0; i < data.length; i++) {
if (data[i][j] === 'O' && data[i][j - 1] === '.') {
data[i][j - 1] = 'O';
data[i][j] = '.';
}
}
}
}
}
lineReader.on('line', function (line) {
const lineArr = line.split('');
data.push(lineArr);
});
function printData(data) {
for (let row = 0; row < data.length; row++) {
console.log(data[row].join(''));
}
console.log('');
}
function compareData(x, y) {
return JSON.stringify(x) === y;
}
lineReader.on('close', function () {
const cycles = 1000;
const set = new Set();
for (let i = 0; i < cycles; i++) {
tiltNorth();
tiltWest();
tiltSouth();
tiltEast();
let count = 0;
let offset = data.length;
for (let row = 0; row < data.length; row++) {
for (let col = 0; col < data[0].length; col++) {
if (data[row][col] === 'O') {
count += offset;
}
}
offset--;
}
if (set.has(count)) {
console.log('Repeating ' + i + ' ' + count);
}
set.add(count);
}
// 5 -> 6 -> 6 -> 6
console.log(set);
//printData(data);
});