-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay06Lanternfish.kt
31 lines (26 loc) · 1.04 KB
/
Day06Lanternfish.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
package adventofcode.year2021
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day06Lanternfish(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val fish by lazy { input.split(",").map(String::toInt) }
private val fishByAge by lazy { fish.groupingBy { it }.eachCount().map { (age, count) -> age to count.toLong() }.toMap() }
override fun partOne() =
generateSequence(fish) { previous ->
previous.flatMap { if (it == 0) listOf(6, 8) else listOf(it - 1) }
}
.take(80 + 1)
.last()
.size
override fun partTwo() =
generateSequence(fishByAge) { previous ->
previous
.flatMap { (age, count) -> if (age == 0) listOf(6 to count, 8 to count) else listOf(age - 1 to count) }
.groupBy { (age, _) -> age }
.map { (age, count) -> age to count.sumOf { it.second } }
.toMap()
}
.take(256 + 1)
.last()
.values
.sum()
}