2021: d03: ex1: add solution
This commit is contained in:
parent
f5a30ee2da
commit
8c90ac89b3
30
2021/d03/ex1/ex1.py
Executable file
30
2021/d03/ex1/ex1.py
Executable file
|
@ -0,0 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from typing import List
|
||||
|
||||
|
||||
def solve(input: List[int]) -> int:
|
||||
gamma, epsilon = 0, 0
|
||||
bit = 1
|
||||
|
||||
while any(map(bool, input)):
|
||||
num_bits = sum(n % 2 for n in input)
|
||||
if num_bits >= len(input) / 2:
|
||||
gamma += bit
|
||||
else:
|
||||
epsilon += bit
|
||||
input = [n // 2 for n in input]
|
||||
bit *= 2
|
||||
|
||||
return gamma * epsilon
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = [int(line, 2) for line in sys.stdin.readlines()]
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue