generated from henningBunk/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day06.kt
43 lines (31 loc) · 1.11 KB
/
Day06.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
package day06
import common.InputRepo
import common.readSessionCookie
import common.solve
fun main(args: Array<String>) {
val day = 6
val input = InputRepo(args.readSessionCookie()).get(day = day)
solve(day, input, ::solveDay06Part1, ::solveDay06Part2)
}
fun calculateFish(startingFish: List<Int>, days: Int): Long {
val lanternFishes = LongArray(days + 10)
for (startFish in startingFish) {
lanternFishes[startFish + 1] += 1L
}
for (i in 1 until days) {
lanternFishes[i + 7] += lanternFishes[i]
lanternFishes[i + 9] += lanternFishes[i]
}
val relevantFish = lanternFishes.copyOfRange(0, days + 1)
return relevantFish.sum() + startingFish.count()
}
fun solveDay06Part1(input: List<String>): Long {
val startingFish = parseStartingFish(input)
return calculateFish(startingFish, 80)
}
fun solveDay06Part2(input: List<String>): Long {
val startingFish = parseStartingFish(input)
return calculateFish(startingFish, 256)
}
private fun parseStartingFish(input: List<String>) =
input.map { line -> line.split(",").map(String::toInt) }.flatten()