-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day25.kt
36 lines (34 loc) · 1.38 KB
/
Day25.kt
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
package com.github.ephemient.aoc2021
/** [Day 25](https://adventofcode.com/2022/day/25): Sea cucumber */
class Day25(lines: List<String>) {
private val height = lines.size
private val width = lines.maxOfOrNull { it.length } ?: 0
private val initialState = CharArray(width * height) { lines[it / width].getOrElse(it % width) { '.' } }
fun part1(): Int {
var n = 1
val state = initialState.copyOf()
while (true) {
val a = (0 until height).fold(true) { acc, y ->
val indices = (0 until width).filter { x ->
state[y * width + x] == '>' && state[y * width + (x + 1) % width] == '.'
}
for (x in indices) {
state[y * width + x] = '.'
state[y * width + (x + 1) % width] = '>'
}
acc && indices.isEmpty()
}
val b = (0 until width).fold(true) { acc, x ->
val indices = (0 until height).filter { y ->
state[y * width + x] == 'v' && state[(y + 1) % height * width + x] == '.'
}
for (y in indices) {
state[y * width + x] = '.'
state[(y + 1) % height * width + x] = 'v'
}
acc && indices.isEmpty()
}
if (a && b) return n else n++
}
}
}