advent-of-code/2022/python/day07.py

161 lines
3.8 KiB
Python
Raw Permalink Normal View History

2022-12-09 16:43:00 +00:00
from pprint import pprint as pp
from pathlib import Path
import json
from shared import load_rows
import re
from functools import reduce
import operator
PWD = []
2022-12-12 07:41:14 +00:00
FS = {"/": {".": {"size": 0, "files": []}}}
2022-12-09 16:43:00 +00:00
def get_keys(input_dict):
for key, value in input_dict.items():
if isinstance(value, dict):
for subkey in get_keys(value):
2022-12-12 07:41:14 +00:00
yield key + "," + subkey
2022-12-09 16:43:00 +00:00
else:
if key in ("files"):
continue
yield f"{value}"
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def getFromDict(mapList):
return reduce(operator.getitem, mapList, FS)
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def setInDict(mapList, value):
getFromDict(mapList[:-1])[mapList[-1]] = value
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def addInDict(mapList, filename, value):
try:
v = getFromDict(mapList[:-1])[mapList[-1]]
v += value
getFromDict(mapList[:-1])[mapList[-1]] = v
except KeyError:
2022-12-12 07:41:14 +00:00
getFromDict(mapList[:-1])["."] = {"size": 0, "files": []}
2022-12-09 16:43:00 +00:00
v = getFromDict(mapList[:-1])[mapList[-1]]
2022-12-12 07:41:14 +00:00
v["size"] += 0 # Value
2022-12-09 16:43:00 +00:00
v["files"].append(f"{filename}:{value}")
getFromDict(mapList[:-1])[mapList[-1]] = v
except TypeError:
v = getFromDict(mapList[:-1])["."]
2022-12-12 07:41:14 +00:00
v["size"] += 0 # value
2022-12-09 16:43:00 +00:00
v["files"].append(f"{filename}:{value}")
getFromDict(mapList[:-1])[mapList[-1]] = v
def part1(rows):
build_fs(rows)
2022-12-12 07:41:14 +00:00
# calculate_directories()
2022-12-09 16:43:00 +00:00
def calculate_directories():
keys = list(get_keys(FS))
new_FS = {}
for key in keys:
pwd = key.split(",")
2022-12-12 07:41:14 +00:00
size = int(pwd.pop()) # split out size
pwd.pop() # remove .
2022-12-09 16:43:00 +00:00
pwd_key = ",".join(pwd)
parent = ",".join(pwd[:-1])
2022-12-12 07:41:14 +00:00
new_FS[pwd_key] = {"size": size, "full_size": size, "parent": parent}
2022-12-09 16:43:00 +00:00
print("made")
2022-12-12 07:41:14 +00:00
# pp(new_FS)
# print("----")
# for pwd, directory in new_FS.items():
2022-12-09 16:43:00 +00:00
# print(pwd, "\t", directory['parent'])
print(new_FS.keys())
print("keys^")
for pwd, directory in new_FS.items():
2022-12-12 07:41:14 +00:00
parent = directory["parent"]
2022-12-09 16:43:00 +00:00
if parent:
print(parent)
2022-12-12 07:41:14 +00:00
# print(f"{pwd}:{parent} Adding {directory['size']} to fullsize {new_FS[parent]['full_size']}")
2022-12-09 16:43:00 +00:00
new_FS[parent]["size"] += directory["size"]
print("added sizes")
sizes = []
2022-12-12 07:41:14 +00:00
for k, v in new_FS.items():
sizes.append(v["size"])
2022-12-09 16:43:00 +00:00
total = 0
for size in sizes:
if size <= 100000:
print("+", size)
total += size
2022-12-12 07:41:14 +00:00
print("=", total)
2022-12-09 16:43:00 +00:00
def build_fs(rows):
LS_ING = False
for row in rows:
parts = row.split(" ")
if parts[0] == "$":
if LS_ING:
LS_ING = False
# Action
if parts[1] == "cd":
if parts[2] == "..":
PWD.pop()
else:
PWD.append(parts[2])
2022-12-12 07:41:14 +00:00
# print(PWD)
2022-12-09 16:43:00 +00:00
elif parts[1] == "ls":
LS_ING = True
continue
if LS_ING:
if parts[0] == "dir":
add_directory(parts[1])
else:
add_file(parts[1], int(parts[0]))
jp(FS)
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def jp(d):
output = json.dumps(FS, indent=4)
output2 = re.sub(r'": \[\s+', '": [', output)
output3 = re.sub(r'",\s+', '", ', output2)
output4 = re.sub(r'"\s+\]', '"]', output3)
print(output4)
def add_directory(dirname):
temp_new_path = PWD + [dirname]
2022-12-12 07:41:14 +00:00
setInDict(temp_new_path, {".": {"size": 0, "files": []}})
2022-12-09 16:43:00 +00:00
def add_file(filename, size):
2022-12-12 07:41:14 +00:00
# print(".", PWD, filename, size)
2022-12-09 16:43:00 +00:00
mapList = PWD + ["."]
2022-12-12 07:41:14 +00:00
addInDict(mapList, filename, size)
2022-12-09 16:43:00 +00:00
curr = getFromDict(mapList[:-1])
print(curr)
2022-12-12 07:41:14 +00:00
# print(curr)
2022-12-09 16:43:00 +00:00
s = curr["."]
s["size"] += size
2022-12-12 07:41:14 +00:00
print(size, s)
2022-12-09 16:43:00 +00:00
tmp = mapList + ["size"]
setInDict(mapList, s)
print("-----")
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def part2(row):
pass
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def main():
rows = load_rows(7)
part1(rows)
part2(rows)
if __name__ == "__main__":
main()