-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.ts
94 lines (84 loc) · 2.01 KB
/
part2.ts
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
import * as fs from 'fs';
const input = fs
.readFileSync('input', 'utf8')
.split('\n')
.map((x) => x.split(''));
let result = 0;
const cache = {};
function rollRocksNorth() {
for (let i = 0; i < input.length; i++) {
for (let j = 0; j < input[i].length; j++) {
if (input[i][j] == 'O') {
for (let k = i - 1; k >= 0 && input[k][j] == '.'; k--) {
input[k][j] = 'O';
input[k + 1][j] = '.';
}
}
}
}
}
function rollRocksWest() {
for (let i = 0; i < input.length; i++) {
for (let j = 0; j < input[i].length; j++) {
if (input[i][j] == 'O') {
for (let k = j - 1; k >= 0 && input[i][k] == '.'; k--) {
input[i][k] = 'O';
input[i][k + 1] = '.';
}
}
}
}
}
function rollRocksSouth() {
for (let i = input.length - 1; i >= 0; i--) {
for (let j = 0; j < input[i].length; j++) {
if (input[i][j] == 'O') {
for (let k = i + 1; k < input.length && input[k][j] == '.'; k++) {
input[k][j] = 'O';
input[k - 1][j] = '.';
}
}
}
}
}
function rollRocksEast() {
for (let i = input.length - 1; i >= 0; i--) {
for (let j = input[i].length - 1; j >= 0; j--) {
if (input[i][j] == 'O') {
for (let k = j + 1; k < input[i].length && input[i][k] == '.'; k++) {
input[i][k] = 'O';
input[i][k - 1] = '.';
}
}
}
}
}
let boardStr = '';
let stepCount = 0;
let loopCount = 0;
for (; stepCount < 1000000000; stepCount++) {
const str = input.map((x) => x.join('')).join('');
const test = cache[str];
if (test) {
if (str == boardStr) break;
if (loopCount <= 0) boardStr = str;
input.splice(0, input.length, ...test.map((x) => [...x]));
loopCount++;
continue;
}
rollRocksNorth();
rollRocksWest();
rollRocksSouth();
rollRocksEast();
cache[str] = input.map((x) => [...x]);
}
for (let i = 0; i < (1000000000 - stepCount) % loopCount; i++) {
rollRocksNorth();
rollRocksWest();
rollRocksSouth();
rollRocksEast();
}
for (let i = 0; i < input.length; i++) {
result += input[i].filter((x) => x == 'O').length * (input.length - i);
}
console.log(result);