-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3.kt
59 lines (48 loc) · 1.69 KB
/
Day3.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
package aoc2021.day03
import aoc2020.day14.binaryToDecimal
fun getPowerConsumption(inputList: List<String>): Long {
var gammaRate = ""
var epsilonRate = ""
for (i in 0 until inputList[0].length) {
val (zeroCount, oneCount) = getBitCounts(inputList, i)
if (zeroCount > oneCount) {
gammaRate += 0
epsilonRate += 1
} else {
gammaRate += 1
epsilonRate += 0
}
}
return binaryToDecimal(gammaRate) * binaryToDecimal(epsilonRate)
}
fun getLifeSupportRating(inputList: List<String>): Long {
val oxygenNumberList = inputList.toMutableList()
val co2NumberList = inputList.toMutableList()
for (i in 0 until inputList[0].length) {
updateValueList(oxygenNumberList, i)
updateValueList(co2NumberList, i, '1', '0')
}
return binaryToDecimal(oxygenNumberList[0]) * binaryToDecimal(co2NumberList[0])
}
private fun updateValueList(
valueList: MutableList<String>,
bitPosition: Int,
bitToKeep: Char = '0',
bitToDiscard: Char = '1'
) {
if (valueList.size > 1) {
val (zeroCount, oneCount) = getBitCounts(valueList, bitPosition)
if (zeroCount > oneCount) {
valueList.removeAll { number -> number[bitPosition] != bitToKeep }
} else {
valueList.removeAll { number -> number[bitPosition] != bitToDiscard }
}
}
}
private fun getBitCounts(inputList: List<String>, i: Int): Pair<Int, Int> {
val positionList = mutableListOf<Int>()
inputList.forEach { number: String ->
positionList.add(Character.getNumericValue(number[i]))
}
return Pair(positionList.count { x -> x == 0 }, positionList.count { x -> x == 1 })
}