Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import combinations
- def get_puzzle_input(a_file_name: str) -> list[int]:
- with open(a_file_name) as INFILE:
- return [int(line) for line in INFILE]
- def solve_p1(a_numbers: list[int], a_rolling_window_length: int) -> int:
- current_index = a_rolling_window_length
- while True:
- sum_matches_number: bool = False
- for combo in combinations(numbers[current_index - a_rolling_window_length:current_index], 2):
- if combo[0] + combo[1] == numbers[current_index]:
- sum_matches_number = True
- break
- if sum_matches_number:
- current_index += 1
- else:
- return numbers[current_index]
- def solve_p2(a_numbers: list[int], a_target: int) -> tuple[int, int]:
- start_index: int = 0
- current_index: int = start_index
- candidate_numbers: list[int] = []
- while True:
- candidate_numbers.append(a_numbers[current_index])
- if sum(candidate_numbers) == a_target:
- break
- elif sum(candidate_numbers) > a_target:
- start_index += 1
- current_index = start_index
- candidate_numbers = []
- continue
- else:
- current_index += 1
- return min(candidate_numbers), max(candidate_numbers)
- numbers = get_puzzle_input('aoc_2020/aoc_2020_day_09.txt')
- # print(f"{numbers=}")
- p1: int = solve_p1(numbers, 25)
- print(f"{p1=}")
- p2_lo, p2_hi = solve_p2(numbers, p1)
- print(f"p2={p2_lo + p2_hi}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement