Advertisement
rpsjab

AoC 2020 Day 16

Dec 16th, 2020 (edited)
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. goodtickets = []
  2. yourticket = []
  3. types = {}
  4. possibilities = {}
  5. notpossible = {}
  6.  
  7.  
  8. def buildtickets():
  9.     nums = set()
  10.     global goodtickets
  11.     global yourticket
  12.     global types
  13.     global possibilities, notpossible
  14.  
  15.     with open("day16.txt", "r") as f:
  16.         for line in f:
  17.  
  18.             if line[0:4] == "your":
  19.                 yourticket = [int(x) for x in f.readline().split(',')]
  20.                 continue
  21.  
  22.             if ":" in line and line[-2] != ':':
  23.                 newset = set()
  24.                 type, ranges = line.split(': ')
  25.                 possibilities[type] = set()
  26.                 notpossible[type] = set()
  27.                 r1, r2 = ranges.split(' or ')
  28.                 r11, r12 = r1.split('-')
  29.                 r21, r22 = r2.split('-')
  30.                 for i in range(int(r11),int(r12) + 1):
  31.                     nums.add(i)
  32.                     newset.add(i)
  33.                 for i in range(int(r21),int(r22) + 1):
  34.                     nums.add(i)
  35.                     newset.add(i)
  36.                 types[type] = newset
  37.  
  38.             if "," in line:
  39.                 fields = [int(x) for x in line.split(",")]
  40.                 bad = False
  41.                 for i in fields:
  42.                     if i not in nums:
  43.                         bad = True
  44.                         break
  45.                 if bad == False:
  46.                     goodtickets.append(fields)
  47.  
  48. buildtickets()
  49. # print(goodtickets)
  50. # print(yourticket)
  51. # print(types)
  52.  
  53.  
  54. # make a list of all possibilities and all notpossibles
  55. for t in goodtickets:
  56.     for num, field in enumerate(t):
  57.         # print(num, field)
  58.         for type, range in types.items():
  59.             if field in range:
  60.                 possibilities[type].add(num)
  61.             else:
  62.                 notpossible[type].add(num)
  63.  
  64. # print(possibilities)
  65. # print(notpossible)
  66.  
  67. # remove notpossibles from the possibilities
  68. for k,v in notpossible.items():
  69.     for n in v:
  70.         possibilities[k].discard(n)
  71.  
  72. # print(possibilities)
  73.  
  74.  
  75. # keep removing the ones that have only 1 possibility from the other sets
  76. dones = {}
  77. while (len(dones) < len(possibilities)):
  78.     todo = []
  79.     for k, v in possibilities.items():
  80.         if k not in dones and len(v) == 1:
  81.             todo.append(k)
  82.  
  83.     for t in todo:
  84.         dones[t] = possibilities[t].pop()
  85.         for k in possibilities.keys():
  86.             if k != t:
  87.                 possibilities[k].discard(dones[t])
  88.  
  89. print(dones)
  90.  
  91. # finally, calculate the product
  92. product = 1
  93. for k, v in dones.items():
  94.     if k[:9] == 'departure':
  95.         product *= yourticket[v]
  96.         # print(k, v)
  97.  
  98. print(product)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement