File tree Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Original file line number Diff line number Diff line change 1
1
lists = [tuple (map (int , line .split (" " ))) for line in open ("inputs/day01.txt" ).read ().split ("\n " )]
2
- lists = [sorted (pair [ i ] for pair in lists ) for i in range ( 2 )]
3
- print (f"part 1 = { sum (abs (x - y ) for x , y in zip (lists [ 0 ], lists [ 1 ] ))} " )
2
+ lists = [sorted (li ) for li in zip ( * lists )]
3
+ print (f"part 1 = { sum (abs (x - y ) for x , y in zip (* lists ))} " )
4
4
print (f"part 2 = { sum (sum (1 for x in lists [1 ] if x == value ) * value for value in lists [0 ])} " )
Original file line number Diff line number Diff line change
1
+ def is_valid (values ):
2
+ prev = values [0 ]
3
+ asc = values [1 ] > values [0 ]
4
+ for v in values [1 :]:
5
+ if asc != (v > prev ) or not (1 <= abs (v - prev ) <= 3 ):
6
+ return False
7
+ prev = v
8
+ return True
9
+
10
+ reports = [[int (n ) for n in line .split (" " )] for line in open ("inputs/day02.txt" ).read ().split ("\n " )]
11
+ print (f"part 1 = { sum (is_valid (r ) for r in reports )} " )
12
+ # Brute force: Just try out removing each element until we find a valid combination.
13
+ print (f"part 2 = { sum (any (is_valid (r [:i ] + r [i + 1 :]) for i in range (len (r ))) for r in reports )} " )
You can’t perform that action at this time.
0 commit comments