File tree Expand file tree Collapse file tree 4 files changed +72
-1
lines changed Expand file tree Collapse file tree 4 files changed +72
-1
lines changed Original file line number Diff line number Diff line change
1
+ # [ Day 1: Historian Hysteria] ( https://adventofcode.com/2024/day/1 )
Original file line number Diff line number Diff line change
1
+ import sys
2
+ from collections import defaultdict
3
+
4
+
5
+ def parse (instr : str ) -> tuple [list [int ], list [int ]]:
6
+ a , b = [], []
7
+
8
+ for line in instr .splitlines ():
9
+ ai , bi = line .split (" " )
10
+ a .append (int (ai ))
11
+ b .append (int (bi ))
12
+
13
+ return a , b
14
+
15
+
16
+ def one (instr : str ) -> int :
17
+ a , b = parse (instr )
18
+
19
+ a = sorted (a )
20
+ b = sorted (b )
21
+
22
+ acc = 0
23
+ for (x , y ) in zip (a , b ):
24
+ acc += abs (y - x )
25
+
26
+ return acc
27
+
28
+
29
+ def two (instr : str ):
30
+ a , b = parse (instr )
31
+
32
+ counts = defaultdict (lambda : 0 )
33
+ for val in b :
34
+ counts [val ] = counts [val ] + 1
35
+
36
+ acc = 0
37
+ for val in a :
38
+ acc += counts [val ] * val
39
+
40
+ return acc
41
+
42
+
43
+ def _debug (* args , ** kwargs ):
44
+ kwargs ["file" ] = sys .stderr
45
+ print (* args , ** kwargs )
46
+
47
+
48
+ if __name__ == "__main__" :
49
+ if len (sys .argv ) < 2 or sys .argv [1 ] not in ["1" , "2" ]:
50
+ print ("Missing day argument" , file = sys .stderr )
51
+ sys .exit (1 )
52
+ inp = sys .stdin .read ().strip ()
53
+ if sys .argv [1 ] == "1" :
54
+ print (one (inp ))
55
+ else :
56
+ print (two (inp ))
Original file line number Diff line number Diff line change
1
+ {
2
+ "1" : [
3
+ {
4
+ "is" : " 11" ,
5
+ "input" : " 3 4\n 4 3\n 2 5\n 1 3\n 3 9\n 3 3\n\n "
6
+ }
7
+ ],
8
+ "2" : [
9
+ {
10
+ "is" : " 31" ,
11
+ "input" : " 3 4\n 4 3\n 2 5\n 1 3\n 3 9\n 3 3\n\n "
12
+ }
13
+ ]
14
+ }
Original file line number Diff line number Diff line change @@ -14,4 +14,4 @@ A day denoted with a star means it has a visualisation.
14
14
15
15
| Day | Status | Solutions | Notes |
16
16
| -------------------------------------| --------| ----------------------| -------|
17
- | 01 - Name | ✗ ✗ | | Nothing here... yet! |
17
+ | 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. |
You can’t perform that action at this time.
0 commit comments