summaryrefslogtreecommitdiff
path: root/2024/aoc2024-d01.py
blob: 034e17097890f631cd5b755d634346513be619e4 (plain)
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
#advent of code 2024
#day 01 p1 & p2
#heapq was slower than manually sorting the lists
import time
t1 = time.time();

part1 = 0;
part2 = 0;
ListA = [];
ListB = [];
DictA = {};

f = open("01.in");
for l in f:
	n1,n2 = l.split("   ");
	ListA.append(int(n1));
	ListB.append(int(n2));
	DictA[int(n1)] = 0;
f.close();

ListA = sorted(ListA);
ListB = sorted(ListB);

for i,x1 in enumerate(ListA):
	x2 = ListB[i];
	d = abs(x1-x2);
	part1 += d;
	if x2 in DictA:
		DictA[x2] = DictA[x2] + 1;

for x in DictA:
	part2 += x*DictA[x];

t2 = time.time();
print("part 1 = ", part1);
print("part 2 = ", part2);
print("time ", t2-t1);