Advertisement
Guest User

AoC day 7 part 2

a guest
Dec 7th, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | Source Code | 0 0
  1. def possibilities(values, limit):
  2.     queue = [values[0]]
  3.  
  4.     for v in values[1:]:
  5.         new_queue = []
  6.  
  7.         for q in queue[:]:
  8.             summation = v + q
  9.             product = v * q
  10.             abso = int(f"{q}{v}")
  11.  
  12.             if summation <= limit:
  13.                 new_queue.append(summation)
  14.             if product <= limit:
  15.                 new_queue.append(product)
  16.             if abso <= limit:
  17.                 new_queue.append(abso)
  18.  
  19.         queue = new_queue
  20.  
  21.     output = []
  22.     for q in queue:
  23.         if q == limit:
  24.             output.append(q)
  25.  
  26.     return output
  27.  
  28.  
  29. def iterate(file):
  30.     for line in open(file):
  31.         line = line.strip()
  32.         limit, values = line.split(':')
  33.         limit = int(limit)
  34.  
  35.         values = [int(x) for x in values.strip().split()]
  36.  
  37.         yield limit, values
  38.  
  39.  
  40. def main(file):
  41.     total = 0
  42.     for limit, values in iterate(file):
  43.         combinations = possibilities(values, limit)
  44.  
  45.         if len(combinations) == 0:
  46.             continue
  47.  
  48.         total += limit
  49.  
  50.     return total
  51.  
  52.  
  53. if __name__ == '__main__':
  54.     print(main('input.txt'))
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement