diff options
Diffstat (limited to '2024/aoc2024-d01.py')
-rw-r--r-- | 2024/aoc2024-d01.py | 37 |
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); |