Skip to content

Instantly share code, notes, and snippets.

@chasemc
Created December 7, 2022 18:57
Show Gist options
  • Save chasemc/35fa7b58b6a3c8440f360cbae35642d9 to your computer and use it in GitHub Desktop.
Save chasemc/35fa7b58b6a3c8440f360cbae35642d9 to your computer and use it in GitHub Desktop.
from collections import defaultdict
from pathlib import Path
prompt_data = "/home/chase/Downloads/input.txt"
with open(prompt_data) as fp:
lines = fp.readlines()
cleaned=[]
for i in lines:
if i.startswith("$ cd"):
cleaned.append(i.strip().removeprefix("$ cd "))
elif i[0].isdigit():
cleaned.append(i.split())
cleaned = [i for i in cleaned if i]
files = defaultdict(int)
current_dir=Path("/")
for i in cleaned:
if i == "..":
if current_dir.parents:
current_dir = current_dir.parents[0]
elif isinstance(i, str):
current_dir = current_dir / i
elif isinstance(i, list):
files[ current_dir / i[1]] = int(i[0])
scores = defaultdict(int)
for k,v in files.items():
for i in k.parents:
scores[i] += v
# part 1
sum([v for v in scores.values() if v <= 100000])
min([v for v in scores.values() if v >= scores[Path('/')] - 40000000])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment