From 5ef64073bbb183dd425ffa360d080ca58a1c08e1 Mon Sep 17 00:00:00 2001 From: tchojnacki Date: Wed, 10 Aug 2022 21:01:47 +0200 Subject: Refactor all days to provide better encapsulation --- src/Day13.kt | 109 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 54 insertions(+), 55 deletions(-) (limited to 'src/Day13.kt') diff --git a/src/Day13.kt b/src/Day13.kt index c8bb8c6..0f9b096 100644 --- a/src/Day13.kt +++ b/src/Day13.kt @@ -1,64 +1,62 @@ -import java.lang.IllegalStateException - -sealed class FoldCommand { - abstract fun dispatch(dots: Set): Set - - class AlongX(private val x: Int) : FoldCommand() { - override fun dispatch(dots: Set): Set = dots - .filter { it.x != x } - .map { - if (it.x < x) { - it - } else { - Pos2D(2 * x - it.x, it.y) +object Day13 { + private sealed class FoldCommand { + abstract fun dispatch(dots: Set): Set + + class AlongX(private val x: Int) : FoldCommand() { + override fun dispatch(dots: Set): Set = dots + .filter { it.x != x } + .map { + if (it.x < x) { + it + } else { + Pos2D(2 * x - it.x, it.y) + } } - } - .toSet() - } + .toSet() + } - class AlongY(private val y: Int) : FoldCommand() { - override fun dispatch(dots: Set): Set = dots - .filter { it.y != y } - .map { - if (it.y < y) { - it - } else { - Pos2D(it.x, 2 * y - it.y) + class AlongY(private val y: Int) : FoldCommand() { + override fun dispatch(dots: Set): Set = dots + .filter { it.y != y } + .map { + if (it.y < y) { + it + } else { + Pos2D(it.x, 2 * y - it.y) + } } - } - .toSet() + .toSet() + } } -} - -fun parseOrigami(input: List): Pair, Sequence> { - val dots = mutableSetOf() - val commands = mutableListOf() - for (line in input) { - if (line.matches("\\d+,\\d+".toRegex())) { - val (x, y) = line.split(",").map(String::toInt) - dots.add(Pos2D(x, y)) - } + private fun parseOrigami(input: List): Pair, Sequence> { + val dots = mutableSetOf() + val commands = mutableListOf() - if (line.matches("fold along [xy]=\\d+".toRegex())) { - val equation = line.substring(11) - val (axis, valueString) = equation.split("=") - val value = valueString.toInt() + for (line in input) { + if (line.matches("\\d+,\\d+".toRegex())) { + val (x, y) = line.split(",").map(String::toInt) + dots.add(Pos2D(x, y)) + } - commands.add( - when (axis) { - "x" -> FoldCommand.AlongX(value) - "y" -> FoldCommand.AlongY(value) - else -> throw IllegalStateException("Illegal axis given!") - } - ) + 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() -} + return dots to commands.asSequence() + } -fun main() { fun part1(input: List): Int { val (dots, commands) = parseOrigami(input) @@ -89,12 +87,13 @@ fun main() { return lines.joinToString("\n") { it.joinToString("") } } +} - +fun main() { val testInput = readInputAsLines("Day13_test") - check(part1(testInput) == 17) + check(Day13.part1(testInput) == 17) val input = readInputAsLines("Day13") - println(part1(input)) - println(part2(input)) + println(Day13.part1(input)) + println(Day13.part2(input)) } -- cgit v1.2.3