advent-of-code/2021/d03/ex1/ex1.py
Bruno BELANYI acfc841539 treewide: fix 'ruff check' errors
This is mostly about unused imports.

A couple errors remain, but are fine in my book (using `l` as a variable
name, assigning a lambda to a variable).
2024-12-03 10:28:53 +00:00

30 lines
550 B
Python
Executable file

#!/usr/bin/env python
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()