advent-of-code/2024/python/day01.py

30 lines
715 B
Python
Raw Normal View History

2024-12-01 13:46:43 +00:00
import shared
2024-12-01 21:13:48 +00:00
from collections import Counter
2024-12-01 13:46:43 +00:00
def part1(rows):
2024-12-01 21:13:48 +00:00
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))))
2024-12-01 13:46:43 +00:00
def part2(rows):
2024-12-01 21:13:48 +00:00
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))
2024-12-01 13:46:43 +00:00
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()