2021/day03.scala

28 lines
653 B
Scala
Raw Normal View History

2021-12-09 11:07:49 +00:00
object day03 {
def main(args: Array[String]): Unit = {
val numbers = io.Source.stdin
.getLines()
.map(ln => ln.chars.map(_ - '0').toArray)
.toArray
var occurences = Array.fill(numbers(0).length)(0)
for (number <- numbers) {
for ((bit, i) <- number.zipWithIndex)
occurences(i) += bit
}
var gamma = 0
var epsilon = 0
for ((freq, i) <- occurences.zipWithIndex) {
val shifted = 1 << occurences.length - 1 >> i
if (freq > (numbers.length / 2))
gamma |= shifted
else
epsilon |= shifted
}
val part1 = gamma * epsilon
println(s"part1: $part1")
}
}