aboutsummaryrefslogtreecommitdiff
path: root/2021-kotlin/src/Day13.kt
diff options
context:
space:
mode:
authortchojnacki <tomaszchojnacki2001@gmail.com>2022-08-11 19:24:23 +0200
committertchojnacki <tomaszchojnacki2001@gmail.com>2022-08-11 19:24:23 +0200
commit0f1e145b80813ae2331b7dac5ace0c589654ad2a (patch)
tree25483b8239436dd5aed2fee8811caf0ba893c0bb /2021-kotlin/src/Day13.kt
parent85fb0396bed6a2129b12392941103924b1ab55be (diff)
downloadgleam_aoc2020-0f1e145b80813ae2331b7dac5ace0c589654ad2a.tar.gz
gleam_aoc2020-0f1e145b80813ae2331b7dac5ace0c589654ad2a.zip
Move subproject to avoid IntelliJ module name issues
Diffstat (limited to '2021-kotlin/src/Day13.kt')
-rw-r--r--2021-kotlin/src/Day13.kt99
1 files changed, 0 insertions, 99 deletions
diff --git a/2021-kotlin/src/Day13.kt b/2021-kotlin/src/Day13.kt
deleted file mode 100644
index 0f9b096..0000000
--- a/2021-kotlin/src/Day13.kt
+++ /dev/null
@@ -1,99 +0,0 @@
-object Day13 {
- private sealed class FoldCommand {
- abstract fun dispatch(dots: Set<Pos2D>): Set<Pos2D>
-
- class AlongX(private val x: Int) : FoldCommand() {
- override fun dispatch(dots: Set<Pos2D>): Set<Pos2D> = dots
- .filter { it.x != x }
- .map {
- if (it.x < x) {
- it
- } else {
- Pos2D(2 * x - it.x, it.y)
- }
- }
- .toSet()
- }
-
- class AlongY(private val y: Int) : FoldCommand() {
- override fun dispatch(dots: Set<Pos2D>): Set<Pos2D> = dots
- .filter { it.y != y }
- .map {
- if (it.y < y) {
- it
- } else {
- Pos2D(it.x, 2 * y - it.y)
- }
- }
- .toSet()
- }
- }
-
- private fun parseOrigami(input: List<String>): Pair<Set<Pos2D>, Sequence<FoldCommand>> {
- val dots = mutableSetOf<Pos2D>()
- val commands = mutableListOf<FoldCommand>()
-
- for (line in input) {
- if (line.matches("\\d+,\\d+".toRegex())) {
- val (x, y) = line.split(",").map(String::toInt)
- dots.add(Pos2D(x, y))
- }
-
- if (line.matches("fold along [xy]=\\d+".toRegex())) {
- val equation = line.substring(11)
- val (axis, valueString) = equation.split("=")
- val value = valueString.toInt()
-
- commands.add(
- when (axis) {
- "x" -> FoldCommand.AlongX(value)
- "y" -> FoldCommand.AlongY(value)
- else -> throw IllegalStateException("Illegal axis given!")
- }
- )
- }
- }
-
- return dots to commands.asSequence()
- }
-
- fun part1(input: List<String>): Int {
- val (dots, commands) = parseOrigami(input)
-
- return commands.first().dispatch(dots).size
- }
-
- fun part2(input: List<String>): String {
- val origami = parseOrigami(input)
- var dots = origami.first
- val commands = origami.second
-
- commands.forEach {
- dots = it.dispatch(dots)
- }
-
- val bounds = dots.reduce { max, pos ->
- when ((pos.x > max.x) to (pos.y > max.y)) {
- true to true -> pos
- true to false -> Pos2D(pos.x, max.y)
- false to true -> Pos2D(max.x, pos.y)
- else -> max
- }
- }
-
- val lines = Array(bounds.y + 1) { Array(bounds.x + 1) { ' ' } }
-
- dots.forEach { lines[it.y][it.x] = '#' }
-
- return lines.joinToString("\n") { it.joinToString("") }
- }
-}
-
-fun main() {
- val testInput = readInputAsLines("Day13_test")
- check(Day13.part1(testInput) == 17)
-
- val input = readInputAsLines("Day13")
- println(Day13.part1(input))
- println(Day13.part2(input))
-}