diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-03 07:07:50 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-03 07:07:50 +0100 |
commit | 8b8e8e053ef8031cbba8a8409a714bfa3196651f (patch) | |
tree | 406a6e7f2868ae2862abc154cda3c61e900e0408 /2021/03/puzzle-2.py | |
parent | 4bc4482a1a2c2e5b652d1fd6163ef5d0b26365e7 (diff) |
Add day 3 solutions
Diffstat (limited to '2021/03/puzzle-2.py')
-rwxr-xr-x | 2021/03/puzzle-2.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/2021/03/puzzle-2.py b/2021/03/puzzle-2.py new file mode 100755 index 0000000..c5ae894 --- /dev/null +++ b/2021/03/puzzle-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from copy import deepcopy +from typing import Callable + + +def solve(lines: list[str], comp: Callable[[int, int], bool]) -> int: + while True: + for i in range(len(lines[0])): + if len(lines) == 1: + return int(lines[0], 2) + + ones = len([line for line in lines if line[i] == "1"]) + zeros = len([line for line in lines if line[i] == "0"]) + + if comp(zeros, ones): + lines = [line for line in lines if line[i] == "0"] + else: + lines = [line for line in lines if line[i] == "1"] + + +def main() -> None: + with open("input", "r", encoding="utf-8") as f: + data = [l.strip() for l in f.readlines()] + + print(solve(deepcopy(data), lambda x, y: x > y) * solve(data, lambda x, y: x <= y)) + + +if __name__ == "__main__": + main() |