diff --git a/src/year2021/day25/Day25.kt b/src/year2021/day25/Day25.kt new file mode 100644 index 0000000..0d73fd4 --- /dev/null +++ b/src/year2021/day25/Day25.kt @@ -0,0 +1,65 @@ +package year2021.day25 + +import product +import readInput + +typealias Cucumbers = List> + +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)) +} diff --git a/src/year2021/day25/sample.txt b/src/year2021/day25/sample.txt new file mode 100644 index 0000000..73a37cc --- /dev/null +++ b/src/year2021/day25/sample.txt @@ -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.> \ No newline at end of file diff --git a/src/year2021/day25/smaller_sample.txt b/src/year2021/day25/smaller_sample.txt new file mode 100644 index 0000000..06d6baa --- /dev/null +++ b/src/year2021/day25/smaller_sample.txt @@ -0,0 +1,7 @@ +...>... +....... +......> +v.....> +......> +....... +..vvv.. \ No newline at end of file