1
0
Fork 0

day(03): refactor

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-04 15:57:28 +01:00
parent 91aa2e41b7
commit 47f865d9aa

View file

@ -2,14 +2,11 @@ package day03
import readInput
fun main() {
fun toBits(input: List<String>): List<List<Int>> {
return input.map { row ->
fun toBits(input: List<String>): List<List<Int>> = input.map { row ->
row.map { if (it == '1') 1 else 0 }
}
}
}
fun part1(input: List<List<Int>>): Int {
fun part1(input: List<List<Int>>): Int {
var gamma = 0
var epsilon = 0
@ -17,19 +14,14 @@ fun main() {
gamma = gamma.shl(1)
epsilon = epsilon.shl(1)
val ones = input.indices.map { input[it][i] }.sum()
if (2 * ones > input.size) {
gamma++
} else {
epsilon++
}
val ones = input.indices.sumOf { input[it][i] }
if (2 * ones > input.size) gamma++ else epsilon++
}
return epsilon * gamma
}
}
fun part2(input: List<List<Int>>): Int {
fun part2(input: List<List<Int>>): Int {
fun part2Helper(predicate: (Int, Int) -> Boolean): Int {
var wanted = input.indices.toList()
var i = 0
@ -49,9 +41,9 @@ fun main() {
val co2ScrubberRating = part2Helper { ones, length -> 2 * ones < length }
return oxygenGeneratorRating * co2ScrubberRating
}
}
fun main() {
val testInput = toBits(readInput(3, "test_input"))
val input = toBits(readInput(3, "input"))