Advertisement
illuminati229

AoC 2023 Day 15

Dec 15th, 2023
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. from time import time
  2. import re
  3.  
  4.  
  5. def timer_func(func):
  6.     # This function shows the execution time of
  7.     # the function object passed
  8.     def wrap_func(*args, **kwargs):
  9.         t1 = time()
  10.         result = func(*args, **kwargs)
  11.         t2 = time()
  12.         print(f'Function {func.__name__!r} executed in {(t2 - t1):.4f}s')
  13.         return result
  14.  
  15.     return wrap_func
  16.  
  17.  
  18. @timer_func
  19. def day15(filepath, part2=False):
  20.     with open(filepath) as fin:
  21.         lines = [line.strip() for line in fin.readlines()]
  22.  
  23.     nums = lines[0].split(',')
  24.     if not part2:
  25.         v_sum = 0
  26.         for n in nums:
  27.             v = 0
  28.             for c in n:
  29.                 v += ord(c)
  30.                 v *= 17
  31.                 v %= 256
  32.             v_sum += v
  33.         return v_sum
  34.     else:
  35.         lens_dict = {}
  36.         for n in nums:
  37.             # split out the label and lens
  38.             label, lens = re.split('[-=]', n)
  39.             # find the box number
  40.             box = 0
  41.             for c in label:
  42.                 box += ord(c)
  43.                 box *= 17
  44.                 box %= 256
  45.             # if there is a lens number, it is an addition '=' operator
  46.             if lens:
  47.                 lens = int(lens)
  48.                 # check to see if that box has lenses in it yet
  49.                 if box in lens_dict:
  50.                     # check to see if that label already exists in the box
  51.                     for i, l in enumerate(lens_dict[box]):
  52.                         if label in l:
  53.                             lens_dict[box][i][label] = lens
  54.                             break
  55.                     # add new label and lens to the end of the list
  56.                     else:
  57.                         lens_dict[box].append({label: lens})
  58.                 else:
  59.                     lens_dict[box] = [{label: lens}]
  60.             # subtraction operator
  61.             else:
  62.                 # check to see if lenses in that box exist yet
  63.                 if box in lens_dict:
  64.                     # loop over the lenses in the box
  65.                     for i, l in enumerate(lens_dict[box]):
  66.                         # if that label does exist
  67.                         if label in l:
  68.                             # remove the lens
  69.                             lens_dict[box].pop(i)
  70.                             break
  71.         lens_power_sum = 0
  72.         for bn, b in lens_dict.items():
  73.             if b:
  74.                 for i, l in enumerate(b, 1):
  75.                     lens_power_sum += (bn + 1) * i * [*l.values()][0]
  76.         return lens_power_sum
  77.  
  78.  
  79. def main():
  80.     assert day15('test15') == 1320
  81.     print(f"Part 1: {day15('input15')}")
  82.  
  83.     assert day15('test15', True) == 145
  84.     print(f"Part 2: {day15('input15', True)}")
  85.  
  86.  
  87. if __name__ == '__main__':
  88.     main()
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement