File tree Expand file tree Collapse file tree 3 files changed +27
-5
lines changed Expand file tree Collapse file tree 3 files changed +27
-5
lines changed Original file line number Diff line number Diff line change 8
8
| [ Day 4: Camp Cleanup] ( https://adventofcode.com/2022/day/4 ) | [ py] ( /day04/main.py ) |
9
9
| [ Day 5: Supply Stacks] ( https://adventofcode.com/2022/day/5 ) | [ py] ( /day05/main.py ) |
10
10
| [ Day 6: Tuning Trouble] ( https://adventofcode.com/2022/day/6 ) | [ py] ( /day06/main.py ) , [ alt] ( /day06/alt.py ) |
11
- | [ Day 7: No Space Left On Device] ( https://adventofcode.com/2022/day/7 ) | [ py] ( /day07/main.py ) |
11
+ | [ Day 7: No Space Left On Device] ( https://adventofcode.com/2022/day/7 ) | [ py] ( /day07/main.py ) , [ alt ] ( /day07/alt.py ) |
12
12
13
13
My solutions from previous years:
14
14
* [ r0f1/adventofcode2020] ( https://github.com/r0f1/adventofcode2020 )
Original file line number Diff line number Diff line change
1
+ # hugues_hoppe on Reddit
2
+ from itertools import accumulate
3
+
4
+ with open ("input.txt" ) as f :
5
+ lines = [x .strip () for x in f ]
6
+
7
+ stack = []
8
+ sizes = []
9
+ for line in lines :
10
+ t = line .split ()
11
+ if line == "$ cd .." :
12
+ s = stack .pop ()
13
+ sizes .append (s )
14
+ stack [- 1 ] += s
15
+ elif line .startswith ("$ cd " ):
16
+ stack .append (0 )
17
+ elif t [0 ].isdigit ():
18
+ stack [- 1 ] += int (t [0 ])
19
+ sizes .extend (accumulate (stack [::- 1 ]))
20
+
21
+ print (sum (s for s in sizes if s <= 100_000 ))
22
+ print (min (s for s in sizes if s >= sizes [- 1 ] - 40_000_000 ))
Original file line number Diff line number Diff line change 1
1
with open ("input.txt" ) as f :
2
2
lines = [x .strip ().split () for x in f ]
3
3
4
- root = {".name" : "/" , ". type" : "d" }
4
+ root = {".type" : "d" }
5
5
cwd = None
6
6
path = []
7
7
for t in lines :
10
10
if t [2 ] == "/" : cwd = root
11
11
elif t [2 ] == ".." : cwd = path .pop ()
12
12
else : path .append (cwd ); cwd = cwd [t [2 ]]
13
- elif t [0 ] == "dir" : cwd [t [1 ]] = {".name" : t [ 1 ], ". type" : "d" }
14
- else : cwd [t [1 ]] = {".name" : t [ 1 ], ". type" : "f" , ".size" : int (t [0 ])}
13
+ elif t [0 ] == "dir" : cwd [t [1 ]] = {".type" : "d" }
14
+ else : cwd [t [1 ]] = {".type" : "f" , ".size" : int (t [0 ])}
15
15
16
16
def get_size (n ):
17
17
if n [".type" ] == "f" : return n [".size" ]
@@ -25,7 +25,7 @@ def create_size_list(cwd, all_sizes):
25
25
26
26
all_sizes = []
27
27
create_size_list (root , all_sizes )
28
- print (sum ([ s for s in all_sizes if s < 100_000 ] ))
28
+ print (sum (s for s in all_sizes if s < 100_000 ))
29
29
30
30
size_free = 70_000_000 - all_sizes [0 ]
31
31
for d in sorted (all_sizes ):
You can’t perform that action at this time.
0 commit comments