-
-
Save gnyman/418d7a7492559eecb75134e214799a3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# Read the input from the command line | |
filename = sys.argv[1] | |
with open(filename) as file: | |
input_lines = file.readlines() | |
# Parse the input | |
directories = {} | |
current_directory = "/" | |
for line in input_lines: | |
if line.startswith("$ cd"): | |
# Change directory | |
if line[5:].strip() == "/": | |
current_directory = "/" | |
elif line[5:].strip() == "..": | |
current_directory = "/".join(current_directory.split("/")[:-1]) | |
else: | |
current_directory += "/" + line[5:].strip() | |
elif line.startswith("$ ls"): | |
# List directory | |
entries = line[5:].strip().split(" ") | |
for entry in entries: | |
if entry.startswith("dir"): | |
# Directory | |
directory_name = entry[4:] | |
directories[current_directory + "/" + directory_name] = 0 | |
else: | |
# File | |
file_name, file_size = entry.split(".") | |
file_size = int(file_size) | |
directories[current_directory] += file_size | |
# Find all directories with a total size of at most 100000 | |
total_size = 0 | |
for directory, size in directories.items(): | |
if size <= 100000: | |
total_size += size | |
# Print the result | |
print(total_size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment