generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.kt
41 lines (32 loc) · 1.1 KB
/
Task.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
package day06
import readInput
fun main() {
val input = readInput("day06")
println(solvePartOne(input))
println(solvePartTwo(input))
}
fun solvePartOne(input: List<String>): Long = input.createFishColony()
.processDays(80)
.count()
fun solvePartTwo(input: List<String>): Long = input.createFishColony()
.processDays(256)
.count()
private fun List<String>.createFishColony() = flatMap { it.split(',') }
.filter { value -> value.isNotEmpty() }
.map { value -> value.toInt() }
.groupBy { value -> value }
.mapValues { (_, values) -> values.size.toLong() }
private fun FishColony.processDays(count: Int) = (0 until count)
.fold(this) { colony, _ -> processADay(colony) }
private fun processADay(input: FishColony): FishColony = buildMap {
for ((key, value) in input) {
if (key > 0) {
this[key - 1] = value + (this[key - 1] ?: 0)
} else {
this[8] = value
this[6] = value + (this[6] ?: 0)
}
}
}
private fun FishColony.count() = map { (_, values) -> values }.sum()
private typealias FishColony = Map<Int, Long>