Skip to content

Commit 0e49e87

Browse files
committed
2024-12-09: part one: solution code and sample data
1 parent bcf87a5 commit 0e49e87

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

2024/09/09.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
3+
disk_map = list(sys.stdin.read().strip())
4+
files = list(enumerate(map(int, disk_map[::2])))
5+
spaces = list(map(int, disk_map[1::2]))
6+
filesystem = []
7+
8+
def filesystem_representation(filesystem):
9+
return str().join('.' if block is None else str(block) for block in filesystem)
10+
11+
def filesystem_checksum(filesystem):
12+
return sum(idx * block for idx, block in enumerate(filesystem) if block is not None)
13+
14+
while files or spaces:
15+
if files:
16+
(file_id, file_size), files = files[0], files[1:]
17+
filesystem += [file_id] * file_size
18+
if spaces:
19+
space, spaces = spaces[0], spaces[1:]
20+
filesystem += [None] * space
21+
22+
print(filesystem_representation(filesystem))
23+
24+
while any(block is None for block in filesystem):
25+
block = filesystem.pop()
26+
empty = filesystem.index(None)
27+
filesystem = filesystem[:empty] + [block] + filesystem[empty + 1:]
28+
29+
print(filesystem_representation(filesystem))
30+
print(filesystem_checksum(filesystem))

2024/09/09.txt

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy