From dbd5abfca3db17a859c5163199db4ba2524ec47e Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 3 Dec 2021 08:48:21 +0100 Subject: [PATCH] 2021: d03: ex1: add solution --- 2021/d03/ex1/ex1.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 2021/d03/ex1/ex1.py diff --git a/2021/d03/ex1/ex1.py b/2021/d03/ex1/ex1.py new file mode 100755 index 0000000..ed2eeb7 --- /dev/null +++ b/2021/d03/ex1/ex1.py @@ -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()