From 0da4e5acf9c54811c09032fc1f247075e951df43 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sun, 1 Dec 2024 16:13:48 -0500 Subject: [PATCH] gone golfing --- 2024/python/day01.py | 37 ++++++------------------------------- 1 file changed, 6 insertions(+), 31 deletions(-) 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():