1
0
Fork 0

day(25): add solution

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-25 11:40:39 +01:00
parent a8b68b19b9
commit 675e55eed2
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B
3 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1,65 @@
package year2021.day25
import product
import readInput
typealias Cucumbers = List<List<Char>>
fun printCucumbers(input: Cucumbers) {
println(input.joinToString("\n") { it.joinToString("") })
}
fun isFreeLocation(input: Cucumbers, x: Int, y: Int): Boolean =
input[y][x] == '.'
fun moveSomewhere(input: Cucumbers, facing: Char, dx: Int, dy: Int): Cucumbers {
val output = input.toList().map { it.toMutableList() }
val height = input.size
val width = input.first().size
for ((y, x) in product(input.indices, input.first().indices)) {
if (input[y][x] != facing) {
continue
}
val newX = (x + dx) % width
val newY = (y + dy) % height
if (isFreeLocation(input, newX, newY)) {
output[y][x] = '.'
output[newY][newX] = facing
}
}
return output
}
fun moveEast(input: Cucumbers): Cucumbers = moveSomewhere(input, '>', 1, 0)
fun moveSouth(input: Cucumbers): Cucumbers = moveSomewhere(input, 'v', 0, 1)
fun move(input: Cucumbers): Cucumbers = moveSouth(moveEast(input))
fun part1(input: Cucumbers): Int {
var counter = 0
var previous: Cucumbers = emptyList()
var current = input
while (previous != current) {
previous = current
current = move(previous)
counter++
}
return counter
}
fun part2(input: Cucumbers): String = "Merry Christmas I guess…"
fun main() {
val smallerSample = readInput(25, "smaller_sample").map(String::toList)
val sample = readInput(25, "sample").map(String::toList)
val input = readInput(25, "input").map(String::toList)
check(part1(sample) == 58)
println(part1(input))
println(part2(input))
}

View file

@ -0,0 +1,9 @@
v...>>.vv>
.vv>>.vv..
>>.>v>...v
>>v>>.>.v.
v>v.vv.v..
>.>>..v...
.vv..>.>v.
v.v..>>v.v
....v..v.>

View file

@ -0,0 +1,7 @@
...>...
.......
......>
v.....>
......>
.......
..vvv..