30 lines
715 B
Python
30 lines
715 B
Python
import shared
|
|
from collections import Counter
|
|
|
|
|
|
def part1(rows):
|
|
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))))
|
|
|
|
|
|
def part2(rows):
|
|
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():
|
|
rows = [row for row in shared.load_rows(1)]
|
|
with shared.elapsed_timer() as elapsed:
|
|
part1(rows)
|
|
print("🕒", elapsed())
|
|
|
|
rows = [row for row in shared.load_rows(1, True)]
|
|
with shared.elapsed_timer() as elapsed:
|
|
part2(rows)
|
|
print("🕒", elapsed())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|