diff options
| author | blenovo <bk@gmail.com> | 2025-07-16 18:55:48 +0200 |
|---|---|---|
| committer | blenovo <bk@gmail.com> | 2025-07-16 18:55:48 +0200 |
| commit | 99a7d62c30069a5ffe2210a72c7cf81e76a1f241 (patch) | |
| tree | 71c5c529b7f2fda9b5b5897a56a4ef3199400709 /2020/aoc2020-d09.py | |
| parent | 15662865f0886209d871a7225bfc62cffd2e0783 (diff) | |
summertime warmup session with 2020 event
Diffstat (limited to '2020/aoc2020-d09.py')
| -rw-r--r-- | 2020/aoc2020-d09.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/2020/aoc2020-d09.py b/2020/aoc2020-d09.py new file mode 100644 index 0000000..26c1c61 --- /dev/null +++ b/2020/aoc2020-d09.py @@ -0,0 +1,55 @@ +#advent of code 2020 +#day 09 +#genuinely surprised the simple solution worked +#shamelessly googled the code for combinations, +#didn't feel like implementing it +#if i reversed the "visited" list the runtime could be faster but w/e + +def PossibleCombs(n,k): + combinations = []; + if k==1: return n; + for i in range(len(n)): + head = n[i:i+1] + tail = PossibleCombs(n[i+1:],k-1); + for j in range(len(tail)): + if type(tail[j]) == int: + comb = head + [tail[j]] + else: + comb = head + tail[j]; + combinations.append(comb); + return combinations; + +p1 = None; +limit = 25; +numbers = []; +PuzzleInput = open("09.in","r"); +for line in PuzzleInput: numbers.append(int(line)); +PuzzleInput.close(); +previous = numbers[:limit]; +counter = limit -1; + +for n in numbers[limit:]: + counter += 1; + found = False; + for combo in PossibleCombs(previous,2): + if n == sum(combo): + previous = previous[1:] + [n]; + found = True; + break; + if not found: + p1 = n; + break; + +visited = numbers[:counter]; +for i,n in enumerate(visited): + subsum = 0 + n; + subcounter = 0 + i; + while subsum < p1: + subcounter += 1; + subsum += visited[subcounter]; + if subsum == p1: + p2 = min(visited[i:subcounter]) + max(visited[i:subcounter]) ; + break; + +print("part 1 =",p1); +print("part 2 =",p2); |
