-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.kt
28 lines (24 loc) · 871 Bytes
/
Day10.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
package me.markrobbo
class Day10 {
fun solvePart1(input: List<String>): Int {
return getAdapters(input.map { it.toInt() })
.zipWithNext { a, b -> b - a }
.groupingBy { it }
.eachCount()
.run { getOrDefault(3, 1) * getOrDefault(1, 1) }
}
fun solvePart2(input: List<String>): Long? {
return getAdapters(input.map { it.toInt() })
.drop(1)
.fold(
mapOf(0 to 1L),
{ waysToConnect, adapter ->
waysToConnect.plus(
adapter to (1..3).map { waysToConnect[adapter - it] ?: 0 }.sum()
)
}
)
.run { get(keys.maxOrNull()!!) }
}
private fun getAdapters(input: List<Int>): List<Int> = input.plus(0).plus(input.maxOrNull()!! + 3).sorted()
}