1234567891011121314151617181920212223242526272829303132333435 |
- import argparse
- from collections import Counter
- from typing import List
- import copy
- parser = argparse.ArgumentParser()
- parser.add_argument("ifile", type=argparse.FileType('r'))
- args = parser.parse_args()
- lines = [line.strip() for line in args.ifile.readlines()]
- counters: List[Counter] = [Counter() for i in range(0, len(lines[0]))]
- winning_char_lookup = ['0', '1']
- def bit_filter(one_wins: bool, ilist: List[str]) -> int:
- working_list = copy.deepcopy(ilist)
- working_index = 0
- while(len(working_list) > 1):
- counter: Counter[str] = Counter()
- for line in working_list:
- counter.update(line[working_index])
- winning_char = winning_char_lookup[int(one_wins == (counter['1'] < counter['0']))]
- working_list = list(filter(lambda line: line[working_index] == winning_char, working_list))
- working_index += 1
- return int(working_list[0], 2)
- oxy_rating = bit_filter(True, lines)
- co2_rating = bit_filter(False, lines)
- print(f"oxy * co2 = x, {oxy_rating} * {co2_rating} = {oxy_rating * co2_rating}")
|