Skip to content

Commit df5d9be

Browse files
committed
Solved day05 part 1
1 parent 84c07b4 commit df5d9be

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.4.1'
7+
version '0.4.2'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.havox_design.aoc2023.day05
2+
3+
data class Almanac(val seeds: List<Long>, val maps: List<List<Triple<Long, Long, Long>>>) {
4+
fun seedsToLocation(): List<Long> =
5+
seeds
6+
.map { seed -> computeLocation(seed) }
7+
8+
private fun computeLocation(seed: Long): Long {
9+
var current = seed
10+
11+
for (element in maps) {
12+
for (entry in element) {
13+
val (destStart, sourceStart, length) = entry
14+
if (current in sourceStart..<sourceStart + length) {
15+
val delta = current - sourceStart
16+
current = destStart + (delta)
17+
break
18+
}
19+
}
20+
}
21+
return current
22+
}
23+
24+
companion object {
25+
fun from(input: List<String>): Almanac {
26+
val seeds = input[0].substringAfter("seeds: ")
27+
.split(" ")
28+
.filter(String::isNotBlank)
29+
.map(String::toLong)
30+
val sublist = input.subList(1, input.size)
31+
val maps = mutableListOf<List<Triple<Long, Long, Long>>>()
32+
var submap = mutableListOf<Triple<Long, Long, Long>>()
33+
for (i in sublist.indices) {
34+
if ("map:" in sublist[i]) {
35+
if (submap.isNotEmpty()) maps.add(submap)
36+
submap = mutableListOf()
37+
} else {
38+
submap.add(
39+
Triple(
40+
sublist[i].substringBefore(" ").toLong(),
41+
sublist[i].substringAfter(" ").substringBefore(" ").toLong(),
42+
sublist[i].substringAfterLast(" ").toLong()
43+
)
44+
)
45+
}
46+
}
47+
if (submap.isNotEmpty()) maps.add(submap)
48+
return Almanac(seeds, maps)
49+
}
50+
}
51+
}

day05/src/main/kotlin/de/havox_design/aoc2023/day05/Day05.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package de.havox_design.aoc2023.day05
22

33
class Day05(private var filename: String) {
44
fun solvePart1(): Long =
5-
0L
5+
getResourceAsText(filename)
6+
.let { row -> Almanac.from(row.filter(String::isNotBlank)) }
7+
.seedsToLocation()
8+
.min()
69

710
fun solvePart2(): Long =
811
0L

day05/src/test/kotlin/de/havox_design/aoc2023/day05/Day05Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Day05Test {
2727
@JvmStatic
2828
private fun getDataForTestSolvePart1(): Stream<Arguments> =
2929
Stream.of(
30-
Arguments.of("part1sample.txt", 0L)
30+
Arguments.of("part1sample.txt", 35L)
3131
)
3232

3333
@JvmStatic

0 commit comments

Comments
 (0)