Skip to content

Commit f4652d7

Browse files
committed
[2024/9] p1 solved
1 parent acd5342 commit f4652d7

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

.aoc_tiles/tiles/2024/09.png

6.13 KB
Loading

2024/9/example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2333133121414131402

2024/9/script.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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()

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
33

44
<!-- AOC TILES BEGIN -->
55
<h1 align="center">
6-
Advent of Code - 177/466
6+
Advent of Code - 178/468
77
</h1>
88
<h1 align="center">
9-
2024 - 16 ⭐ - Python
9+
2024 - 17 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@@ -32,6 +32,9 @@ My solutions to the yearly Advents of Code
3232
<a href="2024/8/script.py">
3333
<img src=".aoc_tiles/tiles/2024/08.png" width="161px">
3434
</a>
35+
<a href="2024/9/script.py">
36+
<img src=".aoc_tiles/tiles/2024/09.png" width="161px">
37+
</a>
3538
<h1 align="center">
3639
2023 - 47 ⭐ - Python
3740
</h1>

0 commit comments

Comments
 (0)