diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 6513be7..ae3f30a 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - - - - + + + + \ No newline at end of file diff --git a/app/src/test/kotlin/com/example/adventofcode/AdventOfCode2023.kt b/app/src/test/kotlin/com/example/adventofcode/AdventOfCode2023.kt index 174a03e..0f672d1 100644 --- a/app/src/test/kotlin/com/example/adventofcode/AdventOfCode2023.kt +++ b/app/src/test/kotlin/com/example/adventofcode/AdventOfCode2023.kt @@ -8,11 +8,15 @@ import kotlin.math.max import kotlin.math.min import kotlin.math.pow -fun getInput(day: String): List { + +fun getFile(day: String): File { val infile = File("res/2023/${day.padStart(2, '0')}.txt") assertTrue("File does not exist: " + infile.absolutePath, infile.isFile) - return infile.readLines() + return infile } +fun getInput(day: String): List = getFile(day).readLines() + +fun getString(day: String): String = getFile(day).readText() class AdventOfCode2023 { @Test @@ -140,4 +144,34 @@ class AdventOfCode2023 { println("Day 4.2: $sum2") assertEquals(9721255, sum2) } + + @Test + fun day5() { + val input = getString("5").split("\n\n") + val seedLine = input[0].split(" ").drop(1) + val seeds = seedLine.map { it.toLong() } + val maps = input.drop(1).map { mapBlock -> + mapBlock.split("\n").drop(1).map { mapLine -> + val (to, from, len) = mapLine.split(" ").map { it.toLong() } + from..<(from + len) to (to - from) + } + } + fun mapSeeds(seeds: Sequence) = seeds.map { seed -> + fun useMap(item: Long, m: List>): Long { + for (mapping in m) if (item in mapping.first) return item + mapping.second + return item + } + maps.fold(seed) { item, m -> useMap(item, m) } + }.min() + + val loc = mapSeeds(seeds.asSequence()) + println("Day 5.1: $loc") + assertEquals(600279879, loc) + + val loc2 = seedLine.chunked(2) { (s, l) -> + mapSeeds((s.toLong()..