-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
48 lines (40 loc) · 1.26 KB
/
day1.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
40
41
42
43
44
45
46
47
48
from collections import defaultdict
import heapq
import time
def parse_input(filename: str) -> list[str]:
with open(filename) as f:
rows = [line.split() for line in f.readlines()]
return rows
def part_one(strings: list[str]) -> None:
left, right = [], []
for row in strings:
heapq.heappush(left, int(row[0]))
heapq.heappush(right, int(row[1]))
res = 0
while len(left) > 0:
a, b = heapq.heappop(left), heapq.heappop(right)
res += (abs(a-b))
print(res)
def part_two(strings: list[str]) -> None:
left = []
right_counts = defaultdict(int)
for row in strings:
left_num, right_num = int(row[0]), int(row[1])
left.append(left_num)
right_counts[right_num] += 1
res = 0
for left_num in left:
res += (left_num * right_counts[left_num])
print(res)
def main(input_filename: str):
inp = parse_input(input_filename)
start_part_one = time.time()
part_one(inp)
start_part_two = time.time()
part_two(inp)
end_time = time.time()
print(f"Part one took {start_part_two - start_part_one:.2f} seconds")
print(f"Part two took {end_time - start_part_two:.2f} seconds")
if __name__ == "__main__":
# main("sample.txt")
main("input.txt")