-
Notifications
You must be signed in to change notification settings - Fork 1
/
14.kts
67 lines (67 loc) · 1.98 KB
/
14.kts
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
val platform = System.`in`.bufferedReader().lines().map { it.toCharArray() }.toList()
fun north() {
for (c in 0..<platform[0].size) {
for (r in 0..<platform.size) {
if (platform[r][c] == 'O') {
platform[r][c] = '.'
var i = r - 1; while (i >= 0 && platform[i][c] == '.') i--
platform[i + 1][c] = 'O'
}
}
}
}
fun west() {
for (r in 0..<platform.size) {
for (c in 0..<platform[0].size) {
if (platform[r][c] == 'O') {
platform[r][c] = '.'
var i = c - 1; while (i >= 0 && platform[r][i] == '.') i--
platform[r][i + 1] = 'O'
}
}
}
}
fun south() {
for (c in 0..<platform[0].size) {
for (r in (platform.size-1) downTo 0) {
if (platform[r][c] == 'O') {
platform[r][c] = '.'
var i = r + 1; while (i < platform.size && platform[i][c] == '.') i++
platform[i - 1][c] = 'O'
}
}
}
}
fun east() {
for (r in 0..<platform.size) {
for (c in (platform[0].size - 1) downTo 0) {
if (platform[r][c] == 'O') {
platform[r][c] = '.'
var i = c + 1; while (i < platform[0].size && platform[r][i] == '.') i++
platform[r][i - 1] = 'O'
}
}
}
}
fun state(): List<Int> {
val result = ArrayList<Int>()
platform.forEachIndexed { row, chars -> chars.forEachIndexed { col, c -> if (c == 'O') result.add(platform.size - row) } }
return result
}
north()
var load1 = state().sum()
var cycle: Long = 0
val states = HashMap<List<Int>, Long>()
while (cycle < 1000000000) {
north(); west(); south(); east()
cycle++
val s = state()
if (states.contains(s)) {
val len = cycle - states[s]!!
while (cycle < 1000000000 - len) cycle += len
states.clear()
} else {
states[s] = cycle
}
}
println(listOf(load1, state().sum()))