Advertisement
hhoppe

Advent of code 2020 day 10

Dec 10th, 2020 (edited)
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. def product_of_number_of_differences(s):
  2.   l = sorted(map(int, s.split()))
  3.   counter = collections.Counter(np.diff([0] + l + [l[-1] + 3]))
  4.   return counter[1] * counter[3]
  5.  
  6.  
  7. # Version 1; exotic.
  8. def count_combinations(s):
  9.   diff = np.diff([0] + sorted(map(int, s.split())))
  10.   lengths_of_ones = map(len, re.findall('1+', ''.join(map(str, diff))))
  11.  
  12.   def f(n):
  13.     """Number of combinations from a sequence of n consecutive one-diffs."""
  14.     return 0 if n < 0 else 1 if n < 2 else f(n - 1) + f(n - 2) + f(n - 3)
  15.  
  16.   return np.prod(list(map(f, lengths_of_ones)))
  17.  
  18.  
  19. # Version 2; inspired by Ray's solution.
  20. def count_combinations(s):
  21.   l = [0] + sorted(map(int, s.split()))
  22.   npaths = [1] + [0] * (len(l) - 1)
  23.   for i in range(len(l)):
  24.     for j in range(i + 1, min(i + 4, len(l))):
  25.       if l[j] - l[i] <= 3:
  26.         npaths[j] += npaths[i]
  27.   return npaths[-1]
  28.  
  29.  
  30. # Version 3; "gather/pull".
  31. def count_combinations(s):
  32.   l = [0] + sorted(map(int, s.split()))
  33.   npaths = [1]
  34.   for i in range(1, len(l)):
  35.     npaths.append(
  36.         sum(npaths[j]
  37.             for j in range(-4, 0)
  38.             if i + j >= 0 and l[i] - l[i + j] < 4))
  39.   return npaths[-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement