Skip to content

Instantly share code, notes, and snippets.

@pjhoberman
Created December 9, 2020 17:34
Show Gist options
  • Save pjhoberman/945e00aeae9e53d733d48b719e52d003 to your computer and use it in GitHub Desktop.
Save pjhoberman/945e00aeae9e53d733d48b719e52d003 to your computer and use it in GitHub Desktop.
from itertools import permutations
with open('scratch_23.txt') as file:
nums = file.read().splitlines()
nums = [int(nums) for nums in nums]
def check_number(numbers, preamble=25, start=0):
pre = numbers[start:preamble+start]
test = numbers[preamble+start]
perms = permutations(pre, 2)
try:
while perms:
if sum(next(perms)) == test:
return True
except StopIteration:
return False
return False
bad_number = None
for i, n in enumerate(nums[25:]):
if not check_number(nums, start=i):
bad_number = n
break
print(bad_number)
# part 2
for i, n in enumerate(nums):
for j in range(i):
if sum(nums[j:i]) == bad_number:
print(sorted(nums[j:i])[0] + sorted(nums[j:i])[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment