Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_puzzle_input(a_file_name: str) -> list[str]:
- with open(a_file_name) as INFILE:
- return [line.rstrip() for line in INFILE if not line.startswith('#')]
- def get_masked_decimal(a_mask: str, a_decimal_in: int) -> int:
- masked_decimal: int
- mask_length = len(a_mask)
- in_value_binary = f'{a_decimal_in:0{mask_length}b}'
- out_value_binary = ''.join([v if m == 'X' else m for v, m in zip(in_value_binary, a_mask)])
- masked_decimal = int(out_value_binary, 2)
- return masked_decimal
- def main(a_program: list[str], a_result_map: dict[int, int]) -> None:
- mask: str
- memory_location: int
- value_decimal: int
- for program_line in program:
- program_line = program_line.split(' = ')
- if program_line[0] == 'mask':
- mask = program_line[1]
- # print(f"{mask=}")
- else:
- memory_location = int(program_line[0].strip('mem[]'))
- value_decimal = int(program_line[1])
- # print(f"{memory_location=}, {value_decimal=}")
- result: int = get_masked_decimal(mask, value_decimal)
- # print(f"{result=}")
- a_result_map[memory_location] = result
- program = get_puzzle_input('aoc_2020/aoc_2020_day_14.txt')
- # print(f"{program=}")
- result_map: dict = {}
- main(program, result_map)
- # print(f"{result_map=}")
- print(f"p1={sum(v for v in result_map.values())}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement