part2.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import argparse
  2. from collections import Counter
  3. from typing import List
  4. import copy
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument("ifile", type=argparse.FileType('r'))
  7. args = parser.parse_args()
  8. lines = [line.strip() for line in args.ifile.readlines()]
  9. counters: List[Counter] = [Counter() for i in range(0, len(lines[0]))]
  10. winning_char_lookup = ['0', '1']
  11. def bit_filter(one_wins: bool, ilist: List[str]) -> int:
  12. working_list = copy.deepcopy(ilist)
  13. working_index = 0
  14. while(len(working_list) > 1):
  15. counter: Counter[str] = Counter()
  16. for line in working_list:
  17. counter.update(line[working_index])
  18. winning_char = winning_char_lookup[int(one_wins == (counter['1'] < counter['0']))]
  19. working_list = list(filter(lambda line: line[working_index] == winning_char, working_list))
  20. working_index += 1
  21. return int(working_list[0], 2)
  22. oxy_rating = bit_filter(True, lines)
  23. co2_rating = bit_filter(False, lines)
  24. print(f"oxy * co2 = x, {oxy_rating} * {co2_rating} = {oxy_rating * co2_rating}")