tzot

AoC 2020 Day 10

Dec 12th, 2020
1,397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import operator as op, functools as ft
  2. from collections import Counter
  3. from more_itertools import pairwise
  4.  
  5. def aoc_2020_10(inp):
  6.  
  7.     difs= Counter()
  8.     inp.append(max_el:= max(inp) + 3)
  9.     inp.append(0)
  10.     inp.sort()
  11.  
  12.     # part 1
  13.     for i, j in pairwise(inp):
  14.         difs[j-i]+= 1
  15.     result_a= ft.reduce(op.mul, difs.values(), 1)
  16.  
  17.     # part 2
  18.     found= 0
  19.     def process(n, inp):
  20.         nonlocal found
  21.         if n == inp[-1]:
  22.             found+= 1
  23.             return
  24.         for n1 in inp:
  25.             if n+1 <= n1 <= n+3:
  26.                 process(n1, inp)
  27.             if n1 > n+3:
  28.                 break
  29.  
  30.     factor= 1
  31.     while inp:
  32.         while len(inp) > 1 and inp[1] - inp[0] == 3:
  33.             del inp[0]
  34.         if len(inp) == 1: break
  35.         for i in range(len(inp)-1):
  36.             try:
  37.                 if inp[i+1] - inp[i] == 3:
  38.                     found= 0
  39.                     process(inp[0], inp[:i+1])
  40.                     factor*= found
  41.                     del inp[:i]
  42.                     break
  43.             except IndexError:
  44.                 del inp[0]
  45.                 break
  46.  
  47.     result_b= factor
  48.  
  49.     return result_a, result_b
Advertisement
Add Comment
Please, Sign In to add comment