summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorblenovo <bk@gmail.com>2024-12-03 16:16:21 +0100
committerblenovo <bk@gmail.com>2024-12-03 16:16:21 +0100
commitb00dd9290d19d4e670b2fff3d9f1b29cdd5aa59e (patch)
treeee364f8f1ba0184a401924733d494e6419e40894
parent3d2b624cba351e8d10d407e9322e87d31a20f3be (diff)
added 2024 day 01
-rw-r--r--2024/aoc2024-d01.py37
1 files changed, 37 insertions, 0 deletions
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);