-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.py
39 lines (34 loc) · 1.12 KB
/
day01.py
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
29
30
31
32
33
34
35
36
37
38
39
from typing import Tuple, List
from collections import Counter
INPUTFILE = "inputs/day01.txt"
def get_left_and_right_from_file(filename: str) -> Tuple[List[int],List[int]]:
left: List[int] = []
right: List[int] = []
with open(filename, mode='r') as f_in:
for line in f_in:
items = [int(x) for x in line.rstrip().split(' ')]
left.append(items[0])
right.append(items[1])
return left, right
def part01(debug: bool = False) -> int:
dist_total = 0
if debug:
filename = INPUTFILE.replace(".txt", "_test.txt")
else:
filename = INPUTFILE
left, right = get_left_and_right_from_file(filename)
# TODO
left.sort()
right.sort()
for idx in range(len(left)):
dist_total = sum([abs(x-y) for x, y in zip(left, right)])
return dist_total
def part02(debug: bool = False) -> int:
if debug:
filename = INPUTFILE.replace(".txt", "_test.txt")
else:
filename = INPUTFILE
left, right = get_left_and_right_from_file(filename)
cnt = Counter(right)
dist = sum([l_item*cnt[l_item] for l_item in left])
return dist