From b00dd9290d19d4e670b2fff3d9f1b29cdd5aa59e Mon Sep 17 00:00:00 2001 From: blenovo Date: Tue, 3 Dec 2024 16:16:21 +0100 Subject: added 2024 day 01 --- 2024/aoc2024-d01.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2024/aoc2024-d01.py diff --git a/2024/aoc2024-d01.py b/2024/aoc2024-d01.py new file mode 100644 index 0000000..034e170 --- /dev/null +++ b/2024/aoc2024-d01.py @@ -0,0 +1,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); -- cgit v1.2.3