Skip to content

Instantly share code, notes, and snippets.

@redspider
Last active December 9, 2020 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redspider/dee185d3677edd1f0abcbd860d348df3 to your computer and use it in GitHub Desktop.
Save redspider/dee185d3677edd1f0abcbd860d348df3 to your computer and use it in GitHub Desktop.
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