generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay06.kt
67 lines (52 loc) · 1.68 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package year2021.`06`
import readInput
fun main() {
fun cycle(input: Map<Long, Long>): Map<Long, Long> {
val resultMap = mutableMapOf<Long, Long>()
input.keys
.sortedDescending()
.forEach {
when (it) {
0L -> {
resultMap[6] = (resultMap[6] ?: 0) + (input[0] ?: 0)
resultMap[8] = (resultMap[8] ?: 0) + (input[0] ?: 0)
}
else -> {
resultMap[it - 1] = (input[it] ?: 0)
}
}
}
return resultMap
}
fun functionWithMap(input: List<String>, cycleAmount: Int): Long {
val items = input.first().split(",").map { it.toLong() }
val initialMap = mutableMapOf<Long, Long>()
items.forEach {
if (initialMap.containsKey(it)) {
initialMap[it] = initialMap[it]!! + 1
} else {
initialMap[it] = 1
}
}
var resultMap: Map<Long, Long> = initialMap
repeat(cycleAmount) {
resultMap = cycle(resultMap)
}
return resultMap.values.sum()
}
// endregion
fun part1(input: List<String>): Long {
return functionWithMap(input, 80)
}
fun part2(input: List<String>): Long {
return functionWithMap(input, 256)
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day06_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 5934L)
val input = readInput("Day06")
println(part1(input))
println(part2(input))
}