Advertisement
illuminati229

AoC 2022 Day 13 V2

Dec 13th, 2022
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. from time import time
  2. from json import loads
  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. def insertion_sort(A):
  19.     for k in range(1, len(A)):
  20.         cur = A[k]
  21.         j = k
  22.         while j > 0 and compare_packet(cur, A[j-1]):
  23.             A[j] = A[j-1]
  24.             j -= 1
  25.             A[j] = cur
  26.     return A
  27.  
  28.  
  29. def compare_packet(left: list, right: list):
  30.     for left_val, right_val in zip(left, right):
  31.         if isinstance(left_val, int) and isinstance(right_val, int):
  32.             if left_val < right_val:
  33.                 return True
  34.             elif left_val > right_val:
  35.                 return False
  36.             else:
  37.                 continue
  38.         else:
  39.             if isinstance(left_val, int):
  40.                 left_val = [left_val]
  41.             elif isinstance(right_val, int):
  42.                 right_val = [right_val]
  43.             temp = compare_packet(left_val, right_val)
  44.             if temp is None:
  45.                 continue
  46.             else:
  47.                 return temp
  48.     if len(left) < len(right):
  49.         return True
  50.     elif len(left) > len(right):
  51.         return False
  52.     return None
  53.  
  54.  
  55. @timer_func
  56. def day13(filepath, sort=False):
  57.     with open(filepath) as fin:
  58.         packet_pairs = fin.read().split('\n\n')
  59.  
  60.     if not sort:
  61.         correct_packets = []
  62.         for i, pair in enumerate(packet_pairs):
  63.             left_p, right_p = pair.split('\n')
  64.             if compare_packet(loads(left_p), loads(right_p)):
  65.                 correct_packets.append(i)
  66.         return sum(correct_packets) + len(correct_packets)
  67.  
  68.     else:
  69.         packets = [loads(packet) for pair in packet_pairs for packet in pair.split('\n')] + [[[2]], [[6]]]
  70.         packets_sorted = insertion_sort(packets)
  71.         return (packets_sorted.index([[2]]) + 1) * (packets_sorted.index([[6]]) + 1)
  72.  
  73.  
  74. def main():
  75.     assert day13('test13') == 13
  76.     print(f"Part 1: {day13('input13')}")
  77.  
  78.     assert day13('test13', True) == 140
  79.     print(f"Part 2: {day13('input13', True)}")
  80.  
  81.  
  82. if __name__ == '__main__':
  83.     main()
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement