-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.kt
34 lines (28 loc) · 997 Bytes
/
Day1.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
package me.markrobbo
class Day1 {
fun solvePart1(input: List<Int>): Int? {
val pairedValueMap = mutableMapOf<Int, Int>()
input.forEach {
val pairedValue = pairedValueMap[it]
if (pairedValue != null) {
return it * pairedValue
}
pairedValueMap[2020 - it] = it
}
return null
}
fun solvePart2(input: List<Int>): Int? {
val pairedValueMap = input.map { 2020 - it to it }.toMap()
val doublePairedValueMap = mutableMapOf<Int, Pair<Int, Int>>()
input.forEach { value ->
val pairedValues = doublePairedValueMap[value]
if (pairedValues != null) {
return value * pairedValues.first * pairedValues.second
}
pairedValueMap.forEach { (remainder, valueForRemainder) ->
doublePairedValueMap[remainder - value] = Pair(valueForRemainder, value)
}
}
return null
}
}