This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.js
189 lines (173 loc) · 11.7 KB
/
14.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
const input = `#....O#O.......O.......#O.......#...#O#.O...O...O..O#..#.O......OOO.OOO...#O.O.........OO.#..O....O.
##.#O..........OO.#.#..#...O..O.O##....O.....#....O.#....O....O.#..OOO..#..O.OO..##.#O#OO..##OO..#..
#.O.#O.O.O.......#.....##.##......#.###..###........OO...#..OO.....O....O......OOO.OOO.......OO.O#.O
O#.....#......#..##O#O.#..#O#.O.O.#...O....O.#......#....#.....O.#........O....O...O..O#...O...#O...
O#..#.OO.O#..#O##.#.#..O...OO..#..O#...#.....O.#.OO#...O.OO.O.O.....#...#.O..##...O.#.O.....O.##..O#
..OOO...#O#...O...OO.#...#OO.....#O..#.#.#OO...O.O#.......#..O..O..##..O#.O.....#O..OO#......O.O.O.O
..O.......O.....##OO#OO.#..#...O.O...O...O...#OOOO.O.O..#..O....O....OO#O.O.OOOO...#OO#.OO.O.#...#.#
#..O.......O.O...O.....#.O...O.#.....##.#...#.#.O...O...#.O.....O....#........#.O.#.#O#......O#..O.O
..#.#OO..#..OO.#..O.O.OO....OOOO...OO#..O..O...OO.O..O.O...#..#.....#.......O...O.......##O#....#.O#
.O.O#..O..O.#OO..O...#.O..O##O..##OO.OO.O.OO.O#...O...O........#.O..#......O#O#.O........OO..O......
.O#...........O..#...O.O......#O..###O....#O...OO...........#OO....O..OO..O..O.#O..O......#.O.O..#..
O..O#....OO....#.##.O#....OO.......O.......O....##.O.O....O#...#.O.O###...#....#..#...#...#OO....O##
###.O..#O..#O.#.#......##...OO...#.....OO.#O#..#...#..##O..#...O#....#O#...O..O...O...O..O.O..O..#.O
OOO.#.#..O..O#.O....#...O.OO###.#........OO.#.O#.#.....O..O..O#...O.O.O#.#O......O.#OOO#.#O.#O.O..OO
...##O#..O.#O......OO.#..OO....OOOO#...O...O.#....#..O...........OOO#.O.OO..O..O...O.#..O..OO##.O#..
.#.....#..#.....#..#OOO.O#O..O#O#..O.....OO..O##.O.OOO...#O....#O...O#...#O..O#O##....#.....O.O..O.#
OO............O....O##..OO.#..O.O.....#O...#.##O.#.O.OO#....#...O..O.#.##...........##....O.OO..O...
.##O#OO......O#...OO#...##O.O.......O....OOO..O..#.#...OOO.#..#.#..O..O#..O.....#.#.OO...O.....O##.O
O..#OOO#.O...O.......O#....O.O...O......O.......OO....##......OO#.....##...O#.............O#.....O#.
..O..##OO..O...#..OO..#...#.O.O..#..O...O.###..O.......#.O....O.##....OO.#..O.......#.O.O...O.O.####
.....O..OOO##..#.........O......O..O.#O##O#..O#O......##..O.O..#.......#.O##.#..#.#....O.OO.O.#O....
##....O..#.....#O.O...O#O#O.#.....O#.O.#..#O..O...O.#.#.....O.......#O...............#O......OO#..#.
..#O....#.......O..#O..#.OO..O...##.O.....O.#....#O..O.#O.O#O...O...O........#..OOO#..#....OO.##..O.
.#O#OO....OO.O...O..#..#O.O..#...#..#O.O....O#..OO.....O.#....#O..#....O.....#O.#......OO#O.........
.O..#.O#..O#.O..#.#.......#O......O.....O.#O...#O.#.#..OO.O..#..O............#OO#..#..O..#O..O.O.#..
..#..O.......#O##O.......O.O#O....OOO..OOO..O.#..OO..#O..O#......#.###O.O.#O.#.....#..O.##.OO.#.....
.O.#...O...O...O#..O.##O...#.OO......OOO.OO..#...#...OO#..O.#...OO#....###..O.#..#.....#....O.O.....
....O.OO..O#........#.#.#O..#.O....O...###.....O.O.#O#....O...O...##.#O#..O.#......OO.#####..#...#O.
O...#OO..##O##.......O#.O#...OOO.....O.O..#..OO......O#.##....#......O##......#OO#.#.........OOOO..#
O.O..##..O..#.#O.....#.OO.O...OO..#...O...###.....O..#....#.....#..O.O..#O.O#OO....#..OO.OO#....OO..
.OOO#..#.O.........O..O...O........OO#....#..#..##....O.....#......O....O#.O.O.OO..##...O.#....#....
....O...##O...#...OO.#O....O..O.O.O....#...O#O#..##.O.......O#.#..OO.#...#....O.....O..O#O#..#.O....
..#....O.O....#..O.OO.......O#..OO......O.O...OO.......O..#..O....OO#..O.O......#.......#...#....#.O
.....O#.........#...O.O..O.O#..O....OO..O.....O....O..#O..O.O....#...#.OO..O.#.#..O.#O...#..O..#....
#....#..##...O#OO....#..#.#O...............#.O........O..#.##............#.O#...#..##...O.....#..#.O
#.OO...O...O##...#O#..O....O.O..OO..O.OO.#OO.O..O...O#........#O.O....#.#.OO.....O....#..#O..O.#.###
......O....#...........#...#.........O.O....#..#...#..#.O#....#.O.#.O..#...O..#.......O.#..O#...#...
.#O.OO#.#...#.O.#..OO....O.......O#OO.O...........#.OO..#...##.O.#....O....##O....#..#...O#..O.O..O.
.#..##...#.O#O.O..O.......O..#...OO#......##.#O.#.O#O...##..#.O...#O.....O....O.....#......O..#.....
#O.O.O.OO.....O#..#....O.O#.O.OO.O#..O.#O.....O#.O#.O.O.........O...#...O.O..#O.#.#O#......#OO...##.
....#O....#.#.#O.O.OO.O.#..O.O...#...#...O....O........O#O.......#OO.#.........#....OO...O..O.......
.#.O#O..#.O.#...O#O.#......#.....O.O.O.#OOOO..##OOO.O.O....O..O#OO......OOO..O.##...O.O.#.....O.O#O.
##..........O...O.#...#O............#O.O......O..O#.....O......O..##.O.#O#.#......#..#....O...#..O..
..#..#..O#..#..O#O.........O##......##O.O....##.OOO#..#........O#OO..#.#.O.OO.#.OO#O.#.O..#.......##
.OO....O..OO...O.#..O....#O......##O.OOO..#.#O...#......O..#.#.....O#...OO.#...#O###...O...#........
O...O..OO..#.....OOO.#..O.##....#.O..O.OOO..#...O.#O..........#...O##....O.O..O...O...O.O.#O..O#.O#.
OO.O#O.##O.#.#..O.O.#.#..#...O..O..#...OO.OOO..O.O...OOO.#..O..O..OO.O##O#.#O...O...#...O.#O.#......
..OO.#.##.............#..#O.......O.O.#O..O.OOOO....OO..O..........#O.#..O.##.........#.....O.......
.O..O.##O#...O.#.O.#.......##.#...#....O#..O..OO............###.....#.O.O......##.#..O...........O.#
.##...#OO.O.O#.....O..O.OO##O.#..O..O...OO..#..O.O..O.O.O....O..............O#.###OO.#.#.#O#....#...
..O#...#.....O.OOO.....#O..O.#.#.....##....##O#..............O...O..#O##.#..O.....O..#...O.#O...O..O
O..OO.O....##O##O#......O..#O.O........O##O...#...O..O.....O.##..#....OO#O..O..#.....##..OO....O..O.
OO#.#OO.....O###....#O..O...#...#O..#O.#.O#.O.....O#O.O..O....O....##.....#....O.....#O...##....O.#.
#.#..#O#..O.#.#OO..##...#...#..OO.#..O....O..#...OO....OO..#..#....#....#O#.O...OO...#O..........O..
O..O.O.....O.....#.........O....O#......OO#..O..#.#.O##..#O.#..#...#..O.O......#.O..O..#.O......OO..
.#.......#..#.O.....O.#O#.#.#.....#O####.......O.#O........###..##...##..O..##...O.OO..#..O.O......#
.#.O.#.........#O.O.O...O..OO...O..O.O..O....O#..........O...O.O#.OO#..OOO.O..#O.O.O#O..O#.#..#OO..O
#.#.#...#.#.....#..#........O..#..#..###..O..O...O.O#..##.OOO.O...O.#O...O.......OOO#....#.#...O.#..
...#.O...O#O..O...#..O..O..O#.#...#..#....O.O..O...OO.#..O.OO.##O..OOOOO..#......#..OO##...OOO..O..O
.O...O..O.#O.....OO...#OO#..O#.....O..#.O.#.O#.O.##.O.OOO#OO..#..OO#OOO.O....O.....O#O.O..O..O##.O##
O.O.............O.......OOO#.....#.......#....#.O..##.#...#...OO......#..#..O.O...O.#.O.O..##O#..#..
O...#...##.O.#.##.O#.#..............O.O.#......###...OO......O.#..#.#O.......O#.#.O..O.OO#..O.....O.
OO#.....#.....#.#.#O.O#..#.#...........#...##.....#....O#...#....O#..#O.#O......O#.#.#.O..#.O##...#.
..#....O..##..#.###.OO.#......O..................##O#O....#OO#......O..O...##.##O....OO..#..#OO##.##
...#........O.#...O.##.#..#O..O......#....O##.#..OO..O#..#O......O.....#..O..#.OO.O.#..OO....O..#.#O
.O#..O......O.#OOO.....#.O...OO#.#.........#...O...O.#...##O.........#O#.O.#.#O.#..#.##.....O...OO..
.....OOOO..O.O####..#.O....#..OO......O.O.......OO#OO#O.OO..O##....#.........OO...#....O.O..O..#....
#O...#..#...O.OO#..##O...O.#......O..O##.#...#....OO..#...#..O....#.O...O##.#O#....#...#...O.O...#.O
..#.OOO.....O#......##.....#..O#O...#..#...........O#O.OO...#....#.........O....#..O.#...#..#..#.#O.
...OO.#..O...O..#.#O.##O.#..O.O.#O....O.....#..#O#.O....O.OO#.O..#..#...#...O...##.#.....O......O#O.
.###O....#......O.O..O...#...O..#......O.........O..O...##....#.#...#..........O##O...#..O..O.OO##..
..O.#O.O#...#..O#...O..#O..#.OO.....#.O.....##.O##.##...O..#...#O.O...#......#O...O...OO.#....O#..O.
...O.O...O.O.O.O#O.###..#.##O......O.O.O#..O...#.#O.O...##..##O.OOOO...#...O....O.##O#O.....#O#..O#O
....OOO#.....O..O.......#O#...........#.....#.#.#.....O..OO#.....O.OO..#...####.O.#O#.....#..#....OO
O##..OO..O#.#..O...#.....OOO......O.....O.....O#OO....O...O....O.#...#...OO...#..#....#.......O#.O.#
..#OOOOO.O#..#....##O.O#.....O.O.......O#......O#..#......#.O##.#...#O...O........O......O.O..O.....
..OO........O..O#..O.O.#..O.O#.O#...O.......##..O#......#O.O.#.#......O..#..#O.O.#.#.O#......##O.O..
.#......#..#...O..OOOO#.O#..........O.....#...OOO...O...O.#O..O..O...O..O...#.O....#O#......OO..OO..
...OO....O#O#OO...O.OOO.#...O.....#.O..O.O....#.O.#.O.#.#O.....O..#.##...OO#..#.O....#O.#.O.#O.O.#..
..#..#..O......#.#O.O...#.OO.....OOO...###.....O...O#..##.O...##O#..#.O.#.....O..#..O.O.OOOO..O...##
.#....O.O.O..O.O........O.#O..#OO#.##..O#..#.....OO#..O.#O..O..#.......O..#.....##....O..O.O#.......
....O...#OOO..##O...#OO...O...O......##......O....O..O..O....OO..#O..#....#..OO##.O.#.#.#...#.OO#...
.OO#..#...O.......O..#....OO..O#.....OO.##...##...#O......O.....#........#.##.O.....O..O..O..O.....O
.O..O#...#..O.OO...O....##.O..##...O..##O.#....O#.O....O...O.#O....#O......OO#..........O....##OO.#.
..OO..##.#.O.O....O....#O...O#......##......O.O.O#......O.#O......##.O.......O....OO#O.O..##O.O...O.
#.O....#..##........O#O.OO.#.#..OOO.#.O...#.##...#.O#....#.......#..O#O....O.O...O..O....OO.O#....O.
..#....OO..........O##.##..O..#..O.......#.........##OO....#.O...O.O##.......OO...#O#....O..##..OO..
#O.##O#....#.OOO.......O..#.O.O..O.#.OOOO#.#.#...##.O.OO......O..OO#.#...#.O##O.O........O.#...O.#..
#....O.OO#.#....O.#...O....#..O...#..O.OO..O..#..#..O#.#..OOO.O.O..#.O.#.#.......#.O#..O.##....OO...
..#.....#.O...O.O...O...O.#.#.O...OO#.#.#.....O#O.OOO.O.#......O.O.....O.#..O....O.O.OOOO#...O#.##.#
..O.O#O..OO......O......OO.#.#.....OO#...O..O......OOO..#..O...O#.##..O...#.......#.#.O....#..##.O.O
...O..#O..O#O...#..#...##.#....O.#...#.OOO.#O.......O...O....#...O...##...O..O..#..#...O#O..#...#.O.
..#..##.O#O...OO##..#..OO.#......O......O....#.#.O..O.......OO.#O.......#O...#.O.O.#....##O#O..OO...
.OO...O..O...#.OOO..OO.O...#O.......#.#.....OO..O..#.OO###.O.#O........#O.O#.O...............O.....#
....#O..#..#....O.##O.O#.O...#O..O#..#....O#.O...O....O......##...##O...#.#...#..#.O..O..#..#..O.O#O
.....#..O#...O.#.#.#......#O.O..#.#....O....##.O##O..O.#O..O.O.....#O....#..#.#..O.#O#......OOO#...O
.#..O...##.O.....##.#....#O.O.##..#.......O.....#.#..O##........#.O..........#.#O.#..#......#O#..#O.
OO.#...O...O#.......OO.OOO#.O#....#.OO...O..OO....OO#O.O#....O..O....O..O.O..O....O........O.O.#....
.O...#.......O..#OO.....#O.O...#O.#O..#.#.....#....O....#.....O..#O.#.O......O.O.OOOO.....O...#....#
.#OO.....#.#O..O........O.#O..O..#......O.O....OO.#..OO#..O#.#O.#O.O...#......O#O....#.#O...#...#.OO`;
const example1 = `O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....`;
(async function main () {
const grid = input.split('\n').map(line => line.split(''))
const n = grid.length
console.log('---------')
console.log('| |')
console.log('| START |')
console.log('| |')
console.log('---------')
console.time('start')
for (let part2 of [false]) {
const hash = {}
const anses = {}
let ans = 0
let cycle = 0
const tilts = [
[0, n, true, 1],
[0, n, false, 1],
[n-1, -1, true, -1],
[n-1, -1, false, -1],
]
for (let w = 1; w < 1000000000; w++) {
for (let t = 0; t < tilts.length; t++) {
for (let i = 0; i < n; i++) {
let [j, l, vertical, sign] = tilts[t]
let pos = j
for (; j != l; j += sign) {
const rock = vertical ? grid[j][i] : grid[i][j]
if (rock == '#') {
pos = j + sign
}
if (rock == 'O') {
if (vertical) {
grid[j][i] = '.'
grid[pos][i] = 'O'
} else {
grid[i][j] = '.'
grid[i][pos] = 'O'
}
pos += sign
}
}
}
}
const shot = grid.map(row => row.join('')).join('')
if (hash[shot]) {
cycle = [hash[shot], w];
break;
} else hash[shot] = w
map = Array(n).fill(0)
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 'O') {
map[i] += 1
}
}
}
ans = map.reduce((acc, v, i) => v ? acc + (n - i) * v : acc, 0)
anses[w] = ans
}
console.log(anses[(1000000000 - cycle[0] + 1)%(cycle[1] - cycle[0]) + cycle[0] - 1])
}
console.timeEnd('start')
})()