Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import collections
- infile = '../.input/day01.txt'
- regex = r"(?P<La>\d*)(?:\s*)(?P<Lb>\d*)"
- list_a = []
- list_b = []
- with open(infile) as f:
- for line in f.readlines():
- matches = re.match(regex, line)
- la, lb = matches.groups()
- list_a.append(int(la))
- list_b.append(int(lb))
- a_sorted = sorted(list_a)
- b_sorted = sorted(list_b)
- diffs = sum([abs(b - a) for a, b in zip(a_sorted, b_sorted)])
- print("Part 1: ", str(diffs))
- count_b = dict(collections.Counter(list_b))
- counts = [count_b.get(a,0)*a for a in list_a]
- print("Part 2: ", str(sum(counts)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement