diff --git a/src/main/kotlin/no/rodland/advent/SpacePos.kt b/src/main/kotlin/no/rodland/advent/SpacePos.kt index 64c6441d..53270d5d 100644 --- a/src/main/kotlin/no/rodland/advent/SpacePos.kt +++ b/src/main/kotlin/no/rodland/advent/SpacePos.kt @@ -193,6 +193,8 @@ data class Pos(val x: Int, val y: Int) : SpacePos(), Comparable { operator fun minus(other: Pos): Pos = Pos(x - other.x, y - other.y) + fun manhattanDistance(p: Pos): Int = abs(x - p.x) + abs(y - p.y) + operator fun plus(other: Pos): Pos = Pos(x + other.x, y + other.y) fun lineTo(other: Pos): List { val xDelta = (other.x - x).sign diff --git a/src/main/kotlin/no/rodland/advent_2023/Day11.kt b/src/main/kotlin/no/rodland/advent_2023/Day11.kt new file mode 100644 index 00000000..4e346007 --- /dev/null +++ b/src/main/kotlin/no/rodland/advent_2023/Day11.kt @@ -0,0 +1,67 @@ +package no.rodland.advent_2023 + +import no.rodland.advent.Day +import no.rodland.advent.Pos +import kotlin.math.max +import kotlin.math.min + +// template generated: 11/12/2023 +// Fredrik Rødland 2023 + +class Day11(val input: List) : Day> { + + private val parsed = input.parse() + + override fun partOne(): Long { + return findDistances(2) + } + + override fun partTwo(): Long { + return findDistances(1000000) + } + + private fun findDistances(expansion: Int): Long { + val maxX = input.maxOf { it.length } + val maxY = input.size + val missingCol = (0 until maxX) - parsed.map { it.x }.toSet() + val missingRow = (0 until maxY) - parsed.map { it.y }.toSet() + val pairs = parsed + .flatMap { pos1 -> + parsed.mapNotNull { pos2 -> + if (pos1 != pos2) { + if (pos1 < pos2) { + pos1 to pos2 + } else { + pos2 to pos1 + } + } else { + null + } + } + } + .distinct() + .map { (from, to) -> + val distance = from.manhattanDistance(to) + val crossingCols = missingCol.count { it in (min(from.x, to.x)..max(to.x, from.x)) } + val crossingRows = missingRow.count { it in (min(from.y, to.y)..max(to.y, from.y)) } + (distance + (expansion - 1) * (crossingRows + crossingCols)).toLong() + } + return pairs.sum() + } + + + override fun List.parse(): Set { + val maxX = maxOf { it.length } + return indices.flatMap { y -> + (0 until maxX).mapNotNull { x -> + if (this[y][x] == '#') { + Pos(x, y) + } else { + null + } + } + }.toSet() + } + + override val day = "11".toInt() +} diff --git a/src/test/kotlin/no/rodland/advent_2023/Day11Test.kt b/src/test/kotlin/no/rodland/advent_2023/Day11Test.kt new file mode 100644 index 00000000..dab8b797 --- /dev/null +++ b/src/test/kotlin/no/rodland/advent_2023/Day11Test.kt @@ -0,0 +1,91 @@ +package no.rodland.advent_2023 + +import no.rodland.advent.* +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import readFile + +@Suppress("ClassName") +@DisableSlow +internal class Day11Test { + private val data11 = "2023/input_11.txt".readFile() + private val test11 = listOf( + "...#......", + ".......#..", + "#.........", + "..........", + "......#...", + ".#........", + ".........#", + "..........", + ".......#..", + "#...#.....", + ) + + private val resultTestOne = 374L + private val resultTestTwo = 82000210L + private val resultOne = 9805264L + private val resultTwo = 779032247216L + + val test = defaultTestSuiteParseOnInit( + Day11(data11), + Day11(test11), + resultTestOne, + resultOne, + resultTestTwo, + resultTwo, + { Day11(data11) }, + { Day11(test11) }, + numTestPart1 = 5, + numTestPart2 = 5 + ) + + @Nested + inner class Init { + @Test + fun `11,-,example,1`() { + report(AOCTest({ "123".toInt() }, Unit, 123, 5, "11".toInt(), Part.TWO, false, "example")) + } + + @Test + fun `11,-,example,2`() { + report(test.initTest.copy()) + } + + @Test + fun `11,-,test,init`() { + report(test.initTest) + } + + @Test + fun `11,-,live,init`() { + report(test.initLive) + } + } + + @Nested + inner class `Part 1` { + @Test + fun `11,1,test`() { + report(test.testPart1) + } + + @Test + fun `11,1,live,1`() { + report(test.livePart1) + } + } + + @Nested + inner class `Part 2` { + @Test + fun `11,2,test`() { + report(test.testPart2) + } + + @Test + fun `11,2,live,1`() { + report(test.livePart2) + } + } +} diff --git a/src/test/resources/2023/input_11.txt b/src/test/resources/2023/input_11.txt index e69de29b..e89d7f78 100644 --- a/src/test/resources/2023/input_11.txt +++ b/src/test/resources/2023/input_11.txt @@ -0,0 +1,140 @@ +.......#.....#.....................#.............#.................#.........................#.............................................. +#.....................#.......#.................................................................................#.......#................... +.......................................................................#.........................#..................................#....... +................................................................#..............#..............................................#............. +.........................................#............................................................#....................................# +.................#..................................................#....................................................................... +...........................#...............................................................................#................................ +....#.......#...........................................#..........................#.............................#.......................... +....................#.............#........#................................................................................................ +...................................................#...................................................#...................#................ +.........................#........................................#..................................................#...................... +......#..........#...........................................#...........#............#..................................................... +......................................#..........................................#.........................#.....................#.......... +.#...................#..................................#.......................................#........................................... +............#.................................#.....................#....................................................................... +............................................................................................................................................ +..............................#.....................................................................................#..................#.... +....................................................................................#........#......#....................................... +.........................#.........................#............#...........................................#............................... +.#......#.....#.......................#.................#........................................................#.....#.................... +...........................................................................#.................................................#.............. +...............................................#.......................................#...............#..........................#......... +......................#..................#.................................................................................................. +..........................................................................................................................#................. +......#...........................#.........................................................#........................#...................... +..............................................................................................................................#............. +..#.......................#.................#............................................................................................#.. +..................#....................................................#................#................................................... +....................................................#.....#....................#............................................................ +...............................#............................................................................................................ +..............................................#...............#..........................................................#...........#...... +.#.........#........................#..................................................................#.................................... +..........................................................................#.......................#........................................# +...................#......................................................................#................................................. +.............................................................................................................................#.............. +.....#...................................................................................................................................... +........................................................#........................#.......................................................... +.............................#...............#.....................#.....#............#..................#..............................#... +............................................................................................#......#...........#............................ +.........................#........#...................................................................................#..................... +...........................................................................................................................................# +.....................................................#.....#.................................................................#.............. +#.........................................................................................................#.......................#......... +................#...........................................................#.....................#......................................... +..............................#..............#...................................#.......#...........................#...................... +........#................#.........#............................#............................................#.............#.........#...... +............................................................................................................................................ +.......................................................#...............#.............................#...........................#.......... +.............#.................................#............................................................................................ +............................................................................................................................................ +..#.............................................................................#...........................................#...........#... +............................................................................................................................................ +......................................#..........................................................#................#......................... +............................#................#......#......................#...........#.................................................... +............................................................................................................................................ +....................................................................................................#................#..........#........... +.#............#...................#...............................#........................................#.............................#.. +.........#...............................................................#.................................................................. +.......................#....................#........#...........................................#.......................................... +............................................................#.....................................................#......................... +..............................#.......................................................................#...................#.......#......... +............#.......................#...................#............................#...................................................... +...................................................#......................................#..............................................#.. +.#................#...................................................#........................#............................................ +......#....................................................................#................................#............................... +...............................#....................................................................................#....................... +.........................#.....................#......#.............................................#.................................#..... +............................................................................................................................................ +...............#.....................#....................................................#................................................. +..................................................................................................................#......................... +....#......#.....................................#..............................#............................#............#.....#........... +....................#.............................................#....................#.........#.......................................#.. +.........................................................#..............#.............................#..................................... +............................#...........#...........................................................................#....................... +............................................................................................................................................ +#..................................#...............................................................#..........#............................. +.......#......................................................................................#.................................#.......#... +.....................#....................#...............................#.....#......................................#.................... +................................#..........................#......#....................#..........................#......................... +..............#...................................#........................................................................#...............# +....#....................................................................................................#.................................. +.......................#.................................................................................................................... +......................................#................................#..................#................................................. +.....................................................#............................................#...............................#......... +............................................................................................................................................ +............................................................................................................................................ +....#.................#..................................#.........................#..........................#............................. +.............#........................................................................................#..................#.................. +......................................#............................................................................#........................ +.........#....................................................#.........#............................................................#...... +.............................................................................................#.............................................. +.............................#...........#............................................................................#..................... +#..................#................................................#....................#.......................................#.........# +...................................................................................................#......#................................. +...........#.............................................#...........................#...................................................... +....................................#....................................................................................................... +..................................................#..........#.........#......................#.....................#....................... +.#......#....................................#..............................................................#............................... +.............................#.............................................#................................................................ +.......................#...............................................................................#....................#............... +....................................................#.....................................#...........................................#..... +................#.....................#..................#.................................................................................# +...#.....#..........................................................#................#...................................................... +.............................................#...............#................................#......#...................................... +...............................#...............................................#............................................................ +.............................................................................................................................#.........#.... +.....................#................................................#.................................................#................... +...............#.........................#.......................#..................#....................................................... +.....#......................................................................................................#.......................#....... +....................................#.................#.........................#..................#........................................ +........................................................................................................................................#... +.........#..............#.....................................#........................................#.....................#.............. +...............................................#...................#........................................................................ +......................................#.............#..................................#.....#.......................#...................... +............#.....#...............................................................................#......................................... +...#...................................................................................................................................#.... +.......................#.........................#.......................................................................................... +.........................................................#...........#.........................#........#..........#.............#.......... +............................#..............#....................#........................................................#.................. +.......#.................................................................#................#................................................. +.................#..................................................................................#........................#...........#.. +...................................................#.......#................................................................................ +.........................................#......................................#......................................#.................... +....................................#...........................................................................#........................... +..............................#.............................................................#...........#.......................#......#.... +.........#...............................................#..............#................................................................... +........................#....................#.....................................#........................................................ +#........................................................................................................................................... +............................#......................................................................#.......#................................ +......................................#..................................................................................................... +.....#............#.........................................#..........................#..........................................#......... +...........................................#......................#..............................................#.......................... +..............#..............................................................................#............................#.............#... +..#......#...........................................#..............................#..................#.....#.............................. +....................#..........#.....#...............................#...................................................................... +............................................................................................................................................ +.................................................#.......................................................................................... +.............#..........#...............................#...............#.............#..........#.........#........#....................... +............................................#................#.................#............................................................ +....................................#....................................................................................#.............#.... \ No newline at end of file