Skip to content

Commit

Permalink
2023 - Day11 - part 1 & 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 11, 2023
1 parent 169c1dc commit 2a3541f
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/no/rodland/advent/SpacePos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ data class Pos(val x: Int, val y: Int) : SpacePos(), Comparable<Pos> {

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<Pos> {
val xDelta = (other.x - x).sign
Expand Down
67 changes: 67 additions & 0 deletions src/main/kotlin/no/rodland/advent_2023/Day11.kt
Original file line number Diff line number Diff line change
@@ -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<String>) : Day<Long, Long, Set<Pos>> {

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<String>.parse(): Set<Pos> {
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()
}
91 changes: 91 additions & 0 deletions src/test/kotlin/no/rodland/advent_2023/Day11Test.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading

0 comments on commit 2a3541f

Please sign in to comment.