Advertisement
Guest User

aoc19d22 short

a guest
Dec 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. def apply(a, b, k, n, position):
  2.     def combine(a1, b1, a2, b2):
  3.         b3 = (a1 * b2 + b1) % k
  4.         a3 = (a1 * a2) % k
  5.         return a3, b3
  6.  
  7.     finala, finalb = a, b
  8.     totalCount = 1
  9.     while totalCount < n:
  10.         aa, bb = a, b
  11.         count = 1
  12.  
  13.         while 2*count <= n - totalCount:
  14.             aa, bb = combine(aa, bb, aa, bb)
  15.             count *= 2
  16.  
  17.         finala, finalb = combine(aa, bb, finala, finalb)
  18.         totalCount += count
  19.  
  20.     return (finala * position + finalb) % k
  21.  
  22. def solution(inp, decksize, loops, position):
  23.     a, b = 1, 0
  24.  
  25.     for line in inp.splitlines():
  26.         move, n = line.split(' ')[-2:]
  27.         if move == "new":
  28.             a = (-a) % decksize
  29.             b = (-b - 1) % decksize
  30.         elif move == "cut":
  31.             b = (b - int(n)) % decksize
  32.         elif move == "increment":
  33.             a = (a * int(n)) % decksize
  34.             b = (b * int(n)) % decksize
  35.  
  36.     return apply(a, b, decksize, loops, position)
  37.  
  38. print("Part 1:", solution(open("input.txt").read(), 10007, 1, 2019))
  39. print("Part 2:", solution(open("input.txt").read(), 119315717514047, (119315717514047-1 - 101741582076661), 2020))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement