Advertisement
Guest User

AoC2024_Day01.py

a guest
Dec 1st, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. import re
  2. import collections
  3.  
  4. infile = '../.input/day01.txt'
  5. regex = r"(?P<La>\d*)(?:\s*)(?P<Lb>\d*)"
  6.  
  7. list_a = []
  8. list_b = []
  9.  
  10. with open(infile) as f:
  11.     for line in f.readlines():
  12.         matches = re.match(regex, line)
  13.         la, lb = matches.groups()
  14.         list_a.append(int(la))
  15.         list_b.append(int(lb))
  16.    
  17. a_sorted = sorted(list_a)
  18. b_sorted = sorted(list_b)
  19.  
  20. diffs = sum([abs(b - a) for a, b in zip(a_sorted, b_sorted)])
  21. print("Part 1: ", str(diffs))
  22.  
  23. count_b = dict(collections.Counter(list_b))
  24. counts = [count_b.get(a,0)*a for a in list_a]
  25. print("Part 2: ", str(sum(counts)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement