gone golfing

This commit is contained in:
Tyrel Souza 2024-12-01 16:13:48 -05:00
parent 48a0e08f9c
commit 0da4e5acf9

View File

@ -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():