Advertisement
Guest User

AoC day 7 part 1

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