aboutsummaryrefslogtreecommitdiff
path: root/2024/01/solve.py
blob: a624499ea2271f6d8db0cbaced5470946e2289ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/env python

from collections import Counter

L = [
    [int(i) for i in l.strip().split("   ")] for l in open("input").readlines()
]


def part1():
    res = 0
    left = sorted(l[0] for l in L)
    right = sorted(l[1] for l in L)
    for i in range(len(left)):
        res += abs(left[i] - right[i])
    print(res)


def part2():
    res = 0
    left = [l[0] for l in L]
    right = [l[1] for l in L]
    c = Counter(right)
    print(sum(l * c[l] for l in left))


part1()
part2()