Skip to content

Commit

Permalink
[2024/9] p1 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 9, 2024
1 parent acd5342 commit f4652d7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
Binary file added .aoc_tiles/tiles/2024/09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions 2024/9/example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2333133121414131402
47 changes: 47 additions & 0 deletions 2024/9/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from GhostyUtils import aoc


def expand_diskmap(diskmap: str) -> list:
filesystem = []
for i, c in enumerate(diskmap):
if i % 2 == 0: # even, file
filesystem.extend([i//2]*int(c))
else: # odd, free space
filesystem.extend(['.']*int(c))
return filesystem


def print_filesystem(filesystem: list):
print(''.join(str(c) for c in filesystem))


def defrag(filesystem: list) -> list:
while '.' in filesystem:
f = filesystem.pop()
if f == '.':
continue
filesystem[filesystem.index('.')] = f

if aoc.args().verbose:
print_filesystem(filesystem)
return filesystem


def checksum(filesystem: list) -> int:
return sum(pos * id for pos, id in enumerate(filesystem))


def main():
diskmap = aoc.read()

filesystem = expand_diskmap(diskmap)

if aoc.args().verbose:
print_filesystem(filesystem)

fs = defrag(filesystem)
print(f"p1: {checksum(fs)}")


if __name__ == "__main__":
main()
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 177/466
Advent of Code - 178/468
</h1>
<h1 align="center">
2024 - 16 ⭐ - Python
2024 - 17 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand All @@ -32,6 +32,9 @@ My solutions to the yearly Advents of Code
<a href="2024/8/script.py">
<img src=".aoc_tiles/tiles/2024/08.png" width="161px">
</a>
<a href="2024/9/script.py">
<img src=".aoc_tiles/tiles/2024/09.png" width="161px">
</a>
<h1 align="center">
2023 - 47 ⭐ - Python
</h1>
Expand Down

0 comments on commit f4652d7

Please sign in to comment.