generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.kt
160 lines (130 loc) · 3.96 KB
/
Day05.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package year2023.`05`
import readInput
import utils.printlnDebug
private const val CURRENT_DAY = "05"
data class RangeMap(
val input: LongRange,
val delta: Long,
)
data class Mapper(
val inputRanges: List<RangeMap>,
)
private fun parseIntoGroups(input: List<String>): List<List<String>> {
val listOfLists: MutableList<List<String>> = mutableListOf()
var currentList = mutableListOf<String>()
input.forEach {
if (it.isBlank()) {
listOfLists.add(currentList)
currentList = mutableListOf()
} else {
currentList.add(it)
}
}
if (currentList.isNotEmpty()) {
listOfLists.add(currentList)
}
return listOfLists
}
private fun parseGroupIntoMapper(group: List<String>): Mapper {
val ranges = group.drop(1)
.map { inputLine -> parseLineIntoSeeds(inputLine) }
.map { (dest, source, delta) ->
RangeMap(
input = source..<source + delta,
delta = dest - source,
)
}
return Mapper(ranges)
}
private fun parseLineIntoSeeds(line: String): Triple<Long, Long, Long> {
val res = line.split(" ")
.mapNotNull { it.toLongOrNull() }
val first = res.first()
val second = res[1]
val third = runCatching { res[2] }
.onFailure { error("Failed on line $line - ${it.message}") }
.getOrThrow()
return Triple(first, second, third)
}
private fun processMappers(
seeds: List<Long>,
mappers: List<Mapper>,
): Long {
val resListLocations = seeds
.asSequence()
.map {
mappers.fold(it) { cur, mapper ->
mapSeedFromInputToOutput(cur, mapper)
}
}
.min()
return resListLocations
}
private fun mapSeedFromInputToOutput(
seed: Long,
mappers: Mapper,
): Long {
val realMapRange = mappers.inputRanges.find {
it.input.contains(seed)
}
return if (realMapRange != null) {
seed + realMapRange.delta
} else {
seed
}
}
fun main() {
fun part1(input: List<String>): Long {
val seeds = input.first().split(" ")
.mapNotNull { it.toLongOrNull() }
val mappers = parseIntoGroups(input).map { parseGroupIntoMapper(it) }
val result = processMappers(seeds, mappers)
printlnDebug { "result : $result" }
return result
}
fun part2(input: List<String>): Long {
val mappers = parseIntoGroups(input).map { parseGroupIntoMapper(it) }
val seedRanges = input.first().split(" ")
.mapNotNull { it.toLongOrNull() }
.windowed(2, 2, false)
.map {
check(it.size == 2)
val first = it.first()
val second = it[1]
(first..<first + second)
}
.also { printlnDebug { "SIZE = ${it.size}" } }
.minOf { sedRange ->
printlnDebug { "Ranges seeds = $sedRange" }
sedRange.minOf { seedItem ->
val mappedValueLocation = mappers.fold(seedItem) { cur, mapper ->
mapSeedFromInputToOutput(
cur,
mapper
)
}
mappedValueLocation
}.also {
printlnDebug { "Counted Ranges seeds = $sedRange min = $it" }
}
}
return seedRanges
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 35L)
val part2Test = part2(testInput)
println(part2Test)
check(part2Test == 46L)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 910845529L)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 77435348L)
}