-
-
Save 9helix/f427b6b10afecf4dd25dd8ba873381ae to your computer and use it in GitHub Desktop.
[2022 Day 7 (Part 1)] [PYTHON]
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
with open("7.txt", 'r') as l: | |
li = l.readlines() | |
suma = 0 | |
commands = [] | |
dirs = [] | |
dirval = {} | |
for i in range(len(li)): | |
li[i] = li[i].replace("\n", "") | |
if "$ cd" in li[i]: | |
commands.append(li[i][5:]) | |
ok = [] | |
cur_dir = "" | |
for i in range(len(li)): | |
if "cd" in li[i]: | |
if ".." not in li[i]: | |
cur_dir += li[i][5:]+"\\" | |
else: | |
cur_dir = cur_dir[:cur_dir.rfind("\\", 0, -2)+1] | |
print(li[i], cur_dir) | |
elif li[i] == "$ ls": | |
dirc = 0 | |
for j in range(i+1, len(li)): | |
li[j] = li[j].replace("\n", "") | |
if li[j][0] == "$": | |
# print(ok) | |
for z in cur_dir.split("\\")[:-1]: | |
x = dirc | |
for t in range(len(ok)): | |
# print(len(ok)) | |
y = ok[t] | |
print(z, y.split("\\")[-2], z == y.split("\\")[-2]) | |
if z == y.split("\\")[-2]: | |
dirc += x | |
break | |
i = j | |
break | |
else: | |
if li[j][:3] != "dir": | |
a = int(li[j].split()[0]) | |
dirc += a | |
if dirc <= 100000 and dirc > 0: | |
ok.append(cur_dir) | |
dirs.append(dirc) | |
print(sum(dirs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment