gone golfing
This commit is contained in:
parent
48a0e08f9c
commit
0da4e5acf9
@ -1,41 +1,16 @@
|
|||||||
import shared
|
import shared
|
||||||
import itertools
|
from collections import Counter
|
||||||
import functools
|
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
|
||||||
def part1(rows):
|
def part1(rows):
|
||||||
left = []
|
left, right = zip(*(map(int, row.split()) for row in rows))
|
||||||
right = []
|
print(sum(abs(r - l) for l, r in zip(sorted(left), sorted(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))
|
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
|
||||||
def part2(rows):
|
def part2(rows):
|
||||||
left = []
|
left, right = zip(*(map(int, row.split()) for row in rows))
|
||||||
right = []
|
right_hist = Counter(right)
|
||||||
for row in rows:
|
print(sum(l * right_hist[l] for l in left))
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
Loading…
Reference in New Issue
Block a user