advent-of-code/2021/python/day03.py

40 lines
850 B
Python
Raw Permalink Normal View History

2022-12-09 16:43:00 +00:00
import shared
from pprint import pprint as pp
from collections import Counter
def run(orig):
ox = grab(orig, 1, 0)
co = grab(orig, 0, 1)
return ox * co
def grab(orig, high, low):
idx = 0
while len(orig) != 1:
matrix = _rot(orig)
c = Counter(matrix[idx])
if c[1] >= c[0]:
orig = [o for o in orig if o[idx] == high]
else:
orig = [o for o in orig if o[idx] == low]
idx += 1
return int("".join(str(x) for x in orig[0]), 2)
def _rot(m):
x = list(zip(*m[::-1]))
return [list(reversed(y)) for y in x]
def main():
spl = lambda y: [int(w) for w in y]
with open(shared.get_fname(3), "r") as f:
rows = [x.rstrip() for x in f.readlines()]
matrix = [spl(x) for x in rows]
print(run(matrix))
if __name__ == "__main__":
main()