-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.