Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import operator as op, functools as ft
- from collections import Counter
- from more_itertools import pairwise
- def aoc_2020_10(inp):
- difs= Counter()
- inp.append(max_el:= max(inp) + 3)
- inp.append(0)
- inp.sort()
- # part 1
- for i, j in pairwise(inp):
- difs[j-i]+= 1
- result_a= ft.reduce(op.mul, difs.values(), 1)
- # part 2
- found= 0
- def process(n, inp):
- nonlocal found
- if n == inp[-1]:
- found+= 1
- return
- for n1 in inp:
- if n+1 <= n1 <= n+3:
- process(n1, inp)
- if n1 > n+3:
- break
- factor= 1
- while inp:
- while len(inp) > 1 and inp[1] - inp[0] == 3:
- del inp[0]
- if len(inp) == 1: break
- for i in range(len(inp)-1):
- try:
- if inp[i+1] - inp[i] == 3:
- found= 0
- process(inp[0], inp[:i+1])
- factor*= found
- del inp[:i]
- break
- except IndexError:
- del inp[0]
- break
- result_b= factor
- return result_a, result_b
Advertisement
Add Comment
Please, Sign In to add comment