diff --git a/2024/python/day01.py b/2024/python/day01.py index f66d000..86ed58e 100644 --- a/2024/python/day01.py +++ b/2024/python/day01.py @@ -1,41 +1,16 @@ import shared -import itertools -import functools -from collections import defaultdict +from collections import Counter -# @shared.profile def part1(rows): - left = [] - right = [] - for row in rows: - r = row.split() - left.append(int(r[0])) - right.append(int(r[1])) - left = list(sorted(left)) - right = list(sorted(right)) - pairs = list(zip(left,right)) - diffs = [] - for l, r in pairs: - diffs.append(abs(r - l)) - print(sum(diffs)) + left, right = zip(*(map(int, row.split()) for row in rows)) + print(sum(abs(r - l) for l, r in zip(sorted(left), sorted(right)))) -# @shared.profile def part2(rows): - left = [] - right = [] - for row in rows: - r = row.split() - left.append(int(r[0])) - right.append(int(r[1])) - right_hist = defaultdict(int) - for r in right: - right_hist[r] += 1 - tot = 0 - for l in left: - tot += l * right_hist[l] - print(tot) + left, right = zip(*(map(int, row.split()) for row in rows)) + right_hist = Counter(right) + print(sum(l * right_hist[l] for l in left)) def main():