Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Created December 9, 2020 05:10
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 bluepichu/9ba663904749ab6658fd3ae7b688b01d to your computer and use it in GitHub Desktop.
Save bluepichu/9ba663904749ab6658fd3ae7b688b01d to your computer and use it in GitHub Desktop.
from typing import Dict, List, Set, Tuple
from advent import Input
import re
from collections import defaultdict
input = (
Input(day = 9)
# .all()
.ints()
# .lines()
# .line_tokens()
# .line_tokens(sep = "\n", line_sep = "\n\n")
# .program()
)
magic = 0
for i in range(25, len(input)):
ok =False
for j in range(i - 25, i):
for k in range(i - 25, i):
if input[j] != input[k] and input[j] + input[k] == input[i]:
ok = True
break
if ok: break
if not ok:
magic = input[i]
break
print("pt1:", magic)
for i in range(len(input)):
for j in range(i+1, len(input)):
if sum(input[i:j]) == magic:
print("pt2:", min(input[i:j]) + max(input[i:j]))
@bluepichu
Copy link
Author

Part 2 prints two answers. The second one is just 2*magic and comes from just selecting the range [i, i+1) if i is the index with the magic number and can be ignored.

@cj81499
Copy link

cj81499 commented Dec 9, 2020

To fix part 2 printing two answers, couldn't you just start j at i+2? That way, all slices would start with at least two numbers in them.

It's advent, and as long as you get the right answer, it doesn't really matter how you got there, but 🤷

@bluepichu
Copy link
Author

Yeah, that would absolutely work; I just didn't think of it at the time 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment