File tree Expand file tree Collapse file tree 4 files changed +53
-2
lines changed Expand file tree Collapse file tree 4 files changed +53
-2
lines changed Original file line number Diff line number Diff line change
1
+ 2333133121414131402
Original file line number Diff line number Diff line change
1
+ from GhostyUtils import aoc
2
+
3
+
4
+ def expand_diskmap (diskmap : str ) -> list :
5
+ filesystem = []
6
+ for i , c in enumerate (diskmap ):
7
+ if i % 2 == 0 : # even, file
8
+ filesystem .extend ([i // 2 ]* int (c ))
9
+ else : # odd, free space
10
+ filesystem .extend (['.' ]* int (c ))
11
+ return filesystem
12
+
13
+
14
+ def print_filesystem (filesystem : list ):
15
+ print ('' .join (str (c ) for c in filesystem ))
16
+
17
+
18
+ def defrag (filesystem : list ) -> list :
19
+ while '.' in filesystem :
20
+ f = filesystem .pop ()
21
+ if f == '.' :
22
+ continue
23
+ filesystem [filesystem .index ('.' )] = f
24
+
25
+ if aoc .args ().verbose :
26
+ print_filesystem (filesystem )
27
+ return filesystem
28
+
29
+
30
+ def checksum (filesystem : list ) -> int :
31
+ return sum (pos * id for pos , id in enumerate (filesystem ))
32
+
33
+
34
+ def main ():
35
+ diskmap = aoc .read ()
36
+
37
+ filesystem = expand_diskmap (diskmap )
38
+
39
+ if aoc .args ().verbose :
40
+ print_filesystem (filesystem )
41
+
42
+ fs = defrag (filesystem )
43
+ print (f"p1: { checksum (fs )} " )
44
+
45
+
46
+ if __name__ == "__main__" :
47
+ main ()
Original file line number Diff line number Diff line change @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
3
3
4
4
<!-- AOC TILES BEGIN -->
5
5
<h1 align =" center " >
6
- Advent of Code - 177/466 ⭐
6
+ Advent of Code - 178/468 ⭐
7
7
</h1 >
8
8
<h1 align =" center " >
9
- 2024 - 16 ⭐ - Python
9
+ 2024 - 17 ⭐ - Python
10
10
</h1 >
11
11
<a href =" 2024/1/script.py " >
12
12
<img src =" .aoc_tiles/tiles/2024/01.png " width =" 161px " >
@@ -32,6 +32,9 @@ My solutions to the yearly Advents of Code
32
32
<a href =" 2024/8/script.py " >
33
33
<img src =" .aoc_tiles/tiles/2024/08.png " width =" 161px " >
34
34
</a >
35
+ <a href =" 2024/9/script.py " >
36
+ <img src =" .aoc_tiles/tiles/2024/09.png " width =" 161px " >
37
+ </a >
35
38
<h1 align =" center " >
36
39
2023 - 47 ⭐ - Python
37
40
</h1 >
You can’t perform that action at this time.
0 commit comments