advent-of-code-2021/3/part1.go
2021-12-12 11:03:48 +01:00

39 lines
503 B
Go

package main
import (
"bufio"
"fmt"
"os"
)
//const entryBits = 12
//const entryBits = 5
func main() {
zeroes := [entryBits]int{}
ones := [entryBits]int{}
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
for i, v := range s.Bytes() {
if v == '0' {
zeroes[i]++
} else {
ones[i]++
}
}
}
gamma := 0
for i := range zeroes {
if ones[i] > zeroes[i] {
gamma |= 1 << (entryBits - i - 1)
}
}
epsilon := gamma ^ ((1 << entryBits) - 1)
fmt.Println(gamma * epsilon)
}