Last active
December 9, 2020 10:06
-
-
Save redspider/dee185d3677edd1f0abcbd860d348df3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
from itertools import accumulate, combinations, product | |
INPUT="""...""" | |
PREAMBLE = 25 | |
values = [int(n) for n in INPUT.split("\n")] | |
# Part 1 | |
def is_valid(values, offset): | |
return any(sum(x) == values[offset] for x in combinations(values[offset - PREAMBLE:offset], 2)) | |
for i, n in enumerate(values[PREAMBLE:]): | |
if not is_valid(values, i + PREAMBLE): | |
print(n) | |
break | |
# Part 2 | |
target = 177777905 | |
for (start, start_value), (end, end_value) in product(enumerate(accumulate(values, operator.add)), repeat=2): | |
if end_value - start_value == target and end - start >= 2: | |
print(min(values[start:end]) + max(values[start:end])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment