aboutsummaryrefslogtreecommitdiff
path: root/2021/03/puzzle-2.py
blob: 55513dd6f5783bca6f70bef1f89a8debeae5ad51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)

			if comp(
				len([line for line in lines if line[i] == "0"]),
				len([line for line in lines if line[i] == "1"]),
			):
				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()