Skip to content

Instantly share code, notes, and snippets.

@aseelye
Created December 9, 2020 05:31
Show Gist options
  • Save aseelye/5401dbbe8c3d5f0977d312023abc8507 to your computer and use it in GitHub Desktop.
Save aseelye/5401dbbe8c3d5f0977d312023abc8507 to your computer and use it in GitHub Desktop.
AoC day 09
from itertools import combinations
def validate_xmas(validate_list: list, preamble_len: int = 5) -> int:
"""
takes list, returns the first item that doesn't fit, otherwise returns -1 for a valid list
:param validate_list:
:param preamble_len:
:return:
"""
for i in range(preamble_len, len(validate_list)):
if validate_list[i] not in [x + y for x, y in combinations(validate_list[i - preamble_len:i], 2)]:
return i
return -1
raw = '''35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576'''
with open('09.input.txt') as f:
raw = f.read()
xmas_list = raw.split('\n')
xmas_list = [int(i) for i in xmas_list]
item_number = validate_xmas(xmas_list, preamble_len=25)
print(item_number, xmas_list[item_number]) # 3199139634
for a in range(2, len(xmas_list)):
for b in range(len(xmas_list) - a):
if sum(xmas_list[b:a + b]) == xmas_list[item_number]:
big = max(xmas_list[b:a + b])
small = min(xmas_list[b:a + b])
print(a, b, big + small)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment