Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-08 13:33:36 +00:00
parent 4e62cdd4aa
commit ceedd40662
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
6 changed files with 147 additions and 1 deletions

View file

@ -0,0 +1 @@
# [Day 7: No Space Left On Device](https://adventofcode.com/2022/day/7)

View file

@ -0,0 +1,15 @@
{
"day": 7,
"dir": "challenges/2022/07-noSpaceLeftOnDevice",
"implementations": {
"Python": {
"part.1.avg": 0.007820404291152954,
"part.1.max": 0.027557849884033203,
"part.1.min": 0.00545811653137207,
"part.2.avg": 0.00781301736831665,
"part.2.max": 0.030576467514038086,
"part.2.min": 0.005479097366333008
}
},
"numRuns": 1000
}

View file

@ -0,0 +1,20 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{
"input": "$ cd /\n$ ls\ndir a\n14848514 b.txt\n8504156 c.dat\ndir d\n$ cd a\n$ ls\ndir e\n29116 f\n2557 g\n62596 h.lst\n$ cd e\n$ ls\n584 i\n$ cd ..\n$ cd ..\n$ cd d\n$ ls\n4060174 j\n8033020 d.log\n5626152 d.ext\n7214296 k\n",
"expected": "95437"
}
],
"two": [
{
"input": "$ cd /\n$ ls\ndir a\n14848514 b.txt\n8504156 c.dat\ndir d\n$ cd a\n$ ls\ndir e\n29116 f\n2557 g\n62596 h.lst\n$ cd e\n$ ls\n584 i\n$ cd ..\n$ cd ..\n$ cd d\n$ ls\n4060174 j\n8033020 d.log\n5626152 d.ext\n7214296 k\n",
"expected": "24933642"
}
]
},
"not": {
"one": ["1366030"]
}
}

View file

@ -0,0 +1,110 @@
from typing import *
from dataclasses import dataclass
from aocpy import BaseChallenge
class Path:
parts: List[str]
def __init__(self):
self.parts = []
def cd(self, arg: str):
if arg == "..":
_ = self.parts.pop()
elif arg != ".":
self.parts.append(arg)
def cwd(self) -> str:
x = "/".join(self.parts)
return x if len(x) != 0 else "/"
def abs_in_cwd(self, path: str) -> str:
cwd = self.cwd()
if cwd == "/":
return "/" + path
return cwd + "/" + path
@dataclass(init=False)
class Directories:
directory_names: List[str]
files: Dict[str, int]
def __init__(self):
self.directory_names = []
self.files = {}
def add_dir(self, path: str):
if path not in self.directory_names:
self.directory_names.append(path)
def add_file(self, path: str, size: int):
self.files[path] = size
def parse(instr: str) -> Directories:
dirs = Directories()
fp = Path()
i = 0
lines = instr.strip().splitlines()
while i < len(lines):
line = lines[i]
if line.startswith("$ cd"):
fp.cd(line.removeprefix("$ cd "))
dirs.add_dir(fp.cwd())
elif line.startswith("$ ls"):
while i < len(lines) - 1 and not lines[i + 1].startswith("$"):
i += 1
line = lines[i]
sp = line.split(" ")
if sp[0] == "dir":
dirs.add_dir(fp.abs_in_cwd(sp[1]))
else:
dirs.add_file(fp.abs_in_cwd(sp[1]), int(sp[0]))
i += 1
return dirs
def calculate_directory_size(dirs: Directories, target_dir_name: str) -> int:
total = 0
for fname in dirs.files:
if fname.startswith(target_dir_name + "/"):
total += dirs.files[fname]
return total
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
dirs = parse(instr)
total = 0
for dir_name in dirs.directory_names:
sz = calculate_directory_size(dirs, dir_name)
if sz <= 100000:
total += sz
return total
@staticmethod
def two(instr: str) -> int:
dirs = parse(instr)
used = 70000000 - calculate_directory_size(dirs, "")
amount_to_delete = 30000000 - used
opts = []
for dir_name in dirs.directory_names:
sz = calculate_directory_size(dirs, dir_name)
if sz >= amount_to_delete:
opts.append(sz)
return min(opts)

View file

@ -16,7 +16,7 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
| 04 - Camp Cleanup | ★ ★ | [Python](04-campCleanup/py), [Nim](04-campCleanup/nim) | More sets and more intersections! |
| 05 - Supply Stacks | ★ ★ | [Python](05-supplyStacks/py), [Nim](05-supplyStacks/nim) | Believe it or not, this one involved stacks. |
| 06 - Tuning Trouble | ★ ★ | [Python](06-tuningTrouble/py), [Nim](06-tuningTrouble/nim) | This is the first year I've not repeatedly forgotten about the existence of sets, and it's coming in quite handy. |
| 07 - No Space Left On Device | ☆ ☆ | | Fake file systems are much more difficult to implement than one might expect. |
| 07 - No Space Left On Device | ★ ★ | [Python](07-noSpaceLeftOnDevice/py) | Turns out that fake file systems are prone to very subtle and infuriating bugs. |
| 08 - Treetop Tree House | ★ ★ | [Python](08-treetopTreeHouse/py) | Magical coordinate dictionary tuple things do be magical. |
<!-- PARSE END -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Before After
Before After