advent-of-code-2021/3/part1.go

40 lines
503 B
Go
Raw Normal View History

2021-12-03 08:58:04 +00:00
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)
}