From a749bc7d940e23ec257927432784f1f330163d07 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 17:53:00 +0200 Subject: [PATCH 01/34] Create module for `Directory` class --- puzzles/solutions/2022/d07/directory.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 puzzles/solutions/2022/d07/directory.py diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py new file mode 100644 index 00000000..e69de29b From 4f5f0427fb79aed525fed8ce2c8eea0865145819 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 17:53:38 +0200 Subject: [PATCH 02/34] Create `Directory` class --- puzzles/solutions/2022/d07/directory.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py index e69de29b..22ff138c 100644 --- a/puzzles/solutions/2022/d07/directory.py +++ b/puzzles/solutions/2022/d07/directory.py @@ -0,0 +1,2 @@ +class Directory: + """A directory which can store files and subdirectories.""" From 940c6a6ebc0390ebcd8db719c425b8ef52fcd653 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 17:56:23 +0200 Subject: [PATCH 03/34] Add constructor to `Directory` class --- puzzles/solutions/2022/d07/directory.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py index 22ff138c..beb10f06 100644 --- a/puzzles/solutions/2022/d07/directory.py +++ b/puzzles/solutions/2022/d07/directory.py @@ -1,2 +1,16 @@ +from typing import Self + + class Directory: """A directory which can store files and subdirectories.""" + + def __init__(self, name: str, parent: Self = None): + """ + Instantiate a directory. + :param name: name of the directory + :param parent: parent directory, if exists + """ + self.name = name + self.parent = parent + self._files = [] + self._subdirectories = [] \ No newline at end of file From dbd8cf6c0a482cf8ee4a402da43f22dcaa1978fe Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 17:56:52 +0200 Subject: [PATCH 04/34] Create module for `File` class --- puzzles/solutions/2022/d07/file.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 puzzles/solutions/2022/d07/file.py diff --git a/puzzles/solutions/2022/d07/file.py b/puzzles/solutions/2022/d07/file.py new file mode 100644 index 00000000..e69de29b From 196273fb4515f16fdeb0285e222e35f42b6bfebb Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 17:57:21 +0200 Subject: [PATCH 05/34] Create `File` class --- puzzles/solutions/2022/d07/file.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/puzzles/solutions/2022/d07/file.py b/puzzles/solutions/2022/d07/file.py index e69de29b..e20d967c 100644 --- a/puzzles/solutions/2022/d07/file.py +++ b/puzzles/solutions/2022/d07/file.py @@ -0,0 +1,6 @@ +import dataclasses + + +@dataclasses.dataclass +class File: + """A file with a name and size.""" From 4ef518b2879e3dcf989513f284080de286fcaf1a Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 17:57:43 +0200 Subject: [PATCH 06/34] Add "name" field to `File` class --- puzzles/solutions/2022/d07/file.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/puzzles/solutions/2022/d07/file.py b/puzzles/solutions/2022/d07/file.py index e20d967c..fd1efa77 100644 --- a/puzzles/solutions/2022/d07/file.py +++ b/puzzles/solutions/2022/d07/file.py @@ -4,3 +4,5 @@ @dataclasses.dataclass class File: """A file with a name and size.""" + + name: str From 9545a4a5e5daec942505fadd671c8b030cac31a8 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 17:57:57 +0200 Subject: [PATCH 07/34] Add "size" field to `File` class --- puzzles/solutions/2022/d07/file.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/file.py b/puzzles/solutions/2022/d07/file.py index fd1efa77..c3a9dbcd 100644 --- a/puzzles/solutions/2022/d07/file.py +++ b/puzzles/solutions/2022/d07/file.py @@ -6,3 +6,4 @@ class File: """A file with a name and size.""" name: str + size: int From d459af9b290bdc96f83d54a521957325fd0c5588 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 18:05:18 +0200 Subject: [PATCH 08/34] Add method that adds a file to a directory --- puzzles/solutions/2022/d07/directory.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py index beb10f06..7b322658 100644 --- a/puzzles/solutions/2022/d07/directory.py +++ b/puzzles/solutions/2022/d07/directory.py @@ -1,5 +1,7 @@ from typing import Self +from file import File + class Directory: """A directory which can store files and subdirectories.""" @@ -13,4 +15,10 @@ def __init__(self, name: str, parent: Self = None): self.name = name self.parent = parent self._files = [] - self._subdirectories = [] \ No newline at end of file + self._subdirectories = [] + + def add_file(self, file: File) -> None: + """ + :param file: file to add to the directory + """ + self._files.append(file) From 07c9df38f39dc96b6fa4a4cc74d547a2f120509d Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Wed, 14 Dec 2022 18:06:32 +0200 Subject: [PATCH 09/34] Add method that adds a subdirectory to a directory --- puzzles/solutions/2022/d07/directory.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py index 7b322658..a67f1904 100644 --- a/puzzles/solutions/2022/d07/directory.py +++ b/puzzles/solutions/2022/d07/directory.py @@ -22,3 +22,9 @@ def add_file(self, file: File) -> None: :param file: file to add to the directory """ self._files.append(file) + + def add_subdirectory(self, subdirectory: Self) -> None: + """ + :param subdirectory: subdirectory to add to the directory + """ + self._subdirectories.append(subdirectory) From facb4cc0700abff1c7a6a15ad850ae59d3a2537e Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 00:10:51 +0200 Subject: [PATCH 10/34] Create solution file for part 1 --- puzzles/solutions/2022/d07/p1.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 puzzles/solutions/2022/d07/p1.py diff --git a/puzzles/solutions/2022/d07/p1.py b/puzzles/solutions/2022/d07/p1.py new file mode 100644 index 00000000..c3bbf496 --- /dev/null +++ b/puzzles/solutions/2022/d07/p1.py @@ -0,0 +1,12 @@ +import sys + + +def get_answer(input_text: str): + raise NotImplementedError + + +if __name__ == "__main__": + try: + print(get_answer(sys.argv[1])) + except IndexError: + pass # Don't crash if no input was passed through command line arguments. From 60b0e41f3f1394c3761edf4a4e27733f24ee9520 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 00:18:23 +0200 Subject: [PATCH 11/34] Add constants module --- puzzles/solutions/2022/d07/consts.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 puzzles/solutions/2022/d07/consts.py diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py new file mode 100644 index 00000000..e69de29b From 43b8b1f17a9c887df920cf3944831ee01f8daa4c Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 00:23:54 +0200 Subject: [PATCH 12/34] Add constant for commands seperator --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index e69de29b..98e8c05f 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -0,0 +1 @@ +COMMANDS_SEPARATOR = "$" From 5269b481888592cfd7a803151a615bccafedefef Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 00:54:18 +0200 Subject: [PATCH 13/34] Create module for input parsing functions --- puzzles/solutions/2022/d07/input_parsing.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 puzzles/solutions/2022/d07/input_parsing.py diff --git a/puzzles/solutions/2022/d07/input_parsing.py b/puzzles/solutions/2022/d07/input_parsing.py new file mode 100644 index 00000000..e69de29b From 05fd0f26eb8c5fda27d036310cd4f7ec2803e197 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 20:12:27 +0200 Subject: [PATCH 14/34] Add constant for parent directory symbol --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index 98e8c05f..0e185df3 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -1 +1,2 @@ COMMANDS_SEPARATOR = "$" +PARENT_DIRECTORY_SYMBOL = ".." From 70999b7f92b406fd6b4a2c98422a4a4a61cca9f7 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 00:55:45 +0200 Subject: [PATCH 15/34] Create function that returns next directory according to `cd` command --- puzzles/solutions/2022/d07/input_parsing.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/puzzles/solutions/2022/d07/input_parsing.py b/puzzles/solutions/2022/d07/input_parsing.py index e69de29b..898e17fc 100644 --- a/puzzles/solutions/2022/d07/input_parsing.py +++ b/puzzles/solutions/2022/d07/input_parsing.py @@ -0,0 +1,16 @@ +import consts +from directory import Directory + + +def get_next_directory(current_directory: Directory, command: str) -> Directory: + """ + :param current_directory: current directory + :param command: `cd` command to parse + :return: `cd` command object + """ + destination = command.split()[1] + if destination == consts.PARENT_DIRECTORY_SYMBOL: + return current_directory.parent + for subdirectory in current_directory.subdirectories: + if subdirectory.name == destination: + return subdirectory From 73d05d1421d5046024f7862ac43ec3e32b3285f7 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 00:58:10 +0200 Subject: [PATCH 16/34] Add constant for the directory symbol --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index 0e185df3..78b15a0b 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -1,2 +1,3 @@ COMMANDS_SEPARATOR = "$" +DIRECTORY_SYMBOL = "dir" PARENT_DIRECTORY_SYMBOL = ".." From 609b67136a332c01d210426a15eeb98e99642829 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 19:50:15 +0200 Subject: [PATCH 17/34] Add `property` of the files to `Directory` --- puzzles/solutions/2022/d07/directory.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py index a67f1904..245a7a16 100644 --- a/puzzles/solutions/2022/d07/directory.py +++ b/puzzles/solutions/2022/d07/directory.py @@ -28,3 +28,10 @@ def add_subdirectory(self, subdirectory: Self) -> None: :param subdirectory: subdirectory to add to the directory """ self._subdirectories.append(subdirectory) + + @property + def files(self) -> tuple[File, ...]: + """ + :return: files in the directory + """ + return tuple(self._files) From 41cc799bc73049df1b3b48a4cc078acd10c2e7d5 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 19:52:14 +0200 Subject: [PATCH 18/34] Add `property` of the subdirectories to `Directory` --- puzzles/solutions/2022/d07/directory.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py index 245a7a16..46f53e1c 100644 --- a/puzzles/solutions/2022/d07/directory.py +++ b/puzzles/solutions/2022/d07/directory.py @@ -35,3 +35,10 @@ def files(self) -> tuple[File, ...]: :return: files in the directory """ return tuple(self._files) + + @property + def subdirectories(self) -> tuple[Self, ...]: + """ + :return: subdirectories of the directory + """ + return tuple(self._subdirectories) From 35ef21040d58b1e5acaa5ce7055a21bb5bfeb470 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 20:10:51 +0200 Subject: [PATCH 19/34] Add function that adds contents to a directory based on `ls` output --- puzzles/solutions/2022/d07/input_parsing.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/puzzles/solutions/2022/d07/input_parsing.py b/puzzles/solutions/2022/d07/input_parsing.py index 898e17fc..abd206c6 100644 --- a/puzzles/solutions/2022/d07/input_parsing.py +++ b/puzzles/solutions/2022/d07/input_parsing.py @@ -1,5 +1,6 @@ import consts from directory import Directory +from file import File def get_next_directory(current_directory: Directory, command: str) -> Directory: @@ -14,3 +15,19 @@ def get_next_directory(current_directory: Directory, command: str) -> Directory: for subdirectory in current_directory.subdirectories: if subdirectory.name == destination: return subdirectory + + +def handle_ls_command(directory: Directory, command: str) -> None: + """ + Add contents to the given directory, according to the given `ls` command output. + :param command: `ls` command output + :param directory: directory to add contents to + """ + contents = command.splitlines()[1:] + for content in contents: + if content.startswith(consts.DIRECTORY_SYMBOL): + directory_name = content.split()[1] + directory.add_subdirectory(Directory(directory_name, directory)) + else: + size, filename = content.split() + directory.add_file(File(filename, int(size))) From 602fca0c28c8eaad3eb7103378b601f9fda4fe38 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 21:38:35 +0200 Subject: [PATCH 20/34] Add constant for `cd` command name --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index 78b15a0b..8ef1f10c 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -1,3 +1,4 @@ COMMANDS_SEPARATOR = "$" DIRECTORY_SYMBOL = "dir" PARENT_DIRECTORY_SYMBOL = ".." +CHANGE_DIRECTORY_COMMAND = "cd" From 7b258662f555f1cc4a4688b9808cbb669023e05a Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 21:38:56 +0200 Subject: [PATCH 21/34] Add constant for `ls` command name --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index 8ef1f10c..24dd2f9a 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -2,3 +2,4 @@ DIRECTORY_SYMBOL = "dir" PARENT_DIRECTORY_SYMBOL = ".." CHANGE_DIRECTORY_COMMAND = "cd" +LIST_CONTENTS_COMMAND = "ls" From 8e4b891b5d9700858be959cafcc91aca2413fb55 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Thu, 15 Dec 2022 21:42:24 +0200 Subject: [PATCH 22/34] Add constant for root directory symbol --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index 24dd2f9a..f4418125 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -1,5 +1,6 @@ COMMANDS_SEPARATOR = "$" DIRECTORY_SYMBOL = "dir" PARENT_DIRECTORY_SYMBOL = ".." +ROOT_DIRECTORY_SYMBOL = "/" CHANGE_DIRECTORY_COMMAND = "cd" LIST_CONTENTS_COMMAND = "ls" From ea0cd9e2fa475e3b01dde5482232773fafa3c99a Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 11:16:42 +0200 Subject: [PATCH 23/34] Add `property` of size to `Directory` --- puzzles/solutions/2022/d07/directory.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py index 46f53e1c..18f73b59 100644 --- a/puzzles/solutions/2022/d07/directory.py +++ b/puzzles/solutions/2022/d07/directory.py @@ -42,3 +42,12 @@ def subdirectories(self) -> tuple[Self, ...]: :return: subdirectories of the directory """ return tuple(self._subdirectories) + + @property + def size(self) -> int: + """ + :return: size of the files and subdirectories in the directory + """ + return sum(file.size for file in self.files) + sum( + subdirectory.size for subdirectory in self.subdirectories + ) From 22b2d731fb6b934cad91613017e9d4b02c80f330 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 11:19:13 +0200 Subject: [PATCH 24/34] Add `__str__` magic method to `Directory` --- puzzles/solutions/2022/d07/directory.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/puzzles/solutions/2022/d07/directory.py b/puzzles/solutions/2022/d07/directory.py index 18f73b59..4f8c3de0 100644 --- a/puzzles/solutions/2022/d07/directory.py +++ b/puzzles/solutions/2022/d07/directory.py @@ -17,6 +17,17 @@ def __init__(self, name: str, parent: Self = None): self._files = [] self._subdirectories = [] + def __str__(self): + """ + :return: string representation of the directory, including its name, size, files, and subdirectories + """ + files = ", ".join(file.name for file in self.files) or "no files" + subdirectories = ( + ", ".join(subdirectory.name for subdirectory in self.subdirectories) + or "no subdirs" + ) + return f"{self.name} ({self.size}): {files}; {subdirectories}" + def add_file(self, file: File) -> None: """ :param file: file to add to the directory From 31d0a69823f56869c974daaecd8c0e75ed71e998 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 11:23:29 +0200 Subject: [PATCH 25/34] Add function that returns root directory after parsing terminal output --- puzzles/solutions/2022/d07/p1.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/puzzles/solutions/2022/d07/p1.py b/puzzles/solutions/2022/d07/p1.py index c3bbf496..4e6a96bc 100644 --- a/puzzles/solutions/2022/d07/p1.py +++ b/puzzles/solutions/2022/d07/p1.py @@ -1,4 +1,28 @@ import sys +from typing import Sequence + +import consts +import input_parsing +from directory import Directory + + +def build_filesystem(commands: Sequence[str]) -> Directory: + """ + Build the filesystem according to the given commands and return the root directory. + :param commands: commands output + :return: root directory + """ + root = Directory(consts.ROOT_DIRECTORY_SYMBOL) + current_directory = root + for command in commands: + if command.startswith(consts.CHANGE_DIRECTORY_COMMAND): + current_directory = input_parsing.get_next_directory( + current_directory, command + ) + elif command.startswith(consts.LIST_CONTENTS_COMMAND): + input_parsing.handle_ls_command(current_directory, command) + + return root def get_answer(input_text: str): From 9bb09067c0b58f735acaab24d656bd8dbf7ac53b Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 11:27:24 +0200 Subject: [PATCH 26/34] Add function that returns commands from the terminal output --- puzzles/solutions/2022/d07/p1.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/puzzles/solutions/2022/d07/p1.py b/puzzles/solutions/2022/d07/p1.py index 4e6a96bc..0ef7fc2c 100644 --- a/puzzles/solutions/2022/d07/p1.py +++ b/puzzles/solutions/2022/d07/p1.py @@ -6,6 +6,18 @@ from directory import Directory +def get_commands(input_text: str) -> tuple[str, ...]: + """ + :param input_text: puzzle input + :return: tuple of commands from the terminal output + """ + # Remove the first `$` so we don't have an empty string after running `split()`. + input_text = input_text[1:] + # First command is `cd /` which we can skip. + commands = input_text.split(consts.COMMANDS_SEPARATOR)[1:] + return tuple(command.strip() for command in commands) + + def build_filesystem(commands: Sequence[str]) -> Directory: """ Build the filesystem according to the given commands and return the root directory. From ccdea66b4607a061e1195184f87781205b5dca0c Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 15:00:53 +0200 Subject: [PATCH 27/34] Add function that returns all subdirectories under a given directory --- puzzles/solutions/2022/d07/p1.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/puzzles/solutions/2022/d07/p1.py b/puzzles/solutions/2022/d07/p1.py index 0ef7fc2c..105b0f5e 100644 --- a/puzzles/solutions/2022/d07/p1.py +++ b/puzzles/solutions/2022/d07/p1.py @@ -37,6 +37,22 @@ def build_filesystem(commands: Sequence[str]) -> Directory: return root +def get_all_subdirectories( + directory: Directory, subdirectories=None +) -> list[Directory]: + """ + :param directory: directory to traverse + :param subdirectories: list to add subdirectories to + :return: subdirectories under the given directory + """ + if subdirectories is None: + subdirectories = list() + subdirectories.append(directory) + for subdirectory in directory.subdirectories: + subdirectories.extend(get_all_subdirectories(subdirectory)) + return subdirectories + + def get_answer(input_text: str): raise NotImplementedError From 09aac964170d80a4d866fe7ff669efc3482a1945 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 15:09:47 +0200 Subject: [PATCH 28/34] Add constant for small directory size threshold --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index f4418125..c42307af 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -4,3 +4,4 @@ ROOT_DIRECTORY_SYMBOL = "/" CHANGE_DIRECTORY_COMMAND = "cd" LIST_CONTENTS_COMMAND = "ls" +SMALL_DIRECTORY_SIZE_THRESHOLD = 100_000 From 9c944b10e49008710d1015c42fc22d0cf1178928 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 17 Dec 2022 13:11:30 +0200 Subject: [PATCH 29/34] Add wrapper function that returns filesystem based on terminal output --- puzzles/solutions/2022/d07/p1.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/puzzles/solutions/2022/d07/p1.py b/puzzles/solutions/2022/d07/p1.py index 105b0f5e..61a7f24b 100644 --- a/puzzles/solutions/2022/d07/p1.py +++ b/puzzles/solutions/2022/d07/p1.py @@ -53,6 +53,17 @@ def get_all_subdirectories( return subdirectories +def get_filesystem(terminal_output: str) -> list[Directory]: + """ + :param terminal_output: puzzle input + :return: filesystem built according to the terminal output + """ + commands = get_commands(terminal_output) + root = build_filesystem(commands) + filesystem = get_all_subdirectories(root) + return filesystem + + def get_answer(input_text: str): raise NotImplementedError From 37ccc50e77386f4bf94fd1de19b18b4eabe97846 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 15:11:10 +0200 Subject: [PATCH 30/34] Add function that returns sum of sizes of all small directories --- puzzles/solutions/2022/d07/p1.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/puzzles/solutions/2022/d07/p1.py b/puzzles/solutions/2022/d07/p1.py index 61a7f24b..3ca7a829 100644 --- a/puzzles/solutions/2022/d07/p1.py +++ b/puzzles/solutions/2022/d07/p1.py @@ -65,7 +65,13 @@ def get_filesystem(terminal_output: str) -> list[Directory]: def get_answer(input_text: str): - raise NotImplementedError + """Return the sum of the total sizes of all the directories with a total size of at most 100000.""" + filesystem = get_filesystem(input_text) + return sum( + directory.size + for directory in filesystem + if directory.size < consts.SMALL_DIRECTORY_SIZE_THRESHOLD + ) if __name__ == "__main__": From fd56428e5f73f33c97d7614c25e52d9d54343454 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 21:35:42 +0200 Subject: [PATCH 31/34] Create solution file for part 2 --- puzzles/solutions/2022/d07/p2.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 puzzles/solutions/2022/d07/p2.py diff --git a/puzzles/solutions/2022/d07/p2.py b/puzzles/solutions/2022/d07/p2.py new file mode 100644 index 00000000..c3bbf496 --- /dev/null +++ b/puzzles/solutions/2022/d07/p2.py @@ -0,0 +1,12 @@ +import sys + + +def get_answer(input_text: str): + raise NotImplementedError + + +if __name__ == "__main__": + try: + print(get_answer(sys.argv[1])) + except IndexError: + pass # Don't crash if no input was passed through command line arguments. From 4bdfcfd0e87e715b7862bf495eb3656db9e235e4 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 21:42:36 +0200 Subject: [PATCH 32/34] Add constant for total disk space --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index c42307af..dfa27337 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -5,3 +5,4 @@ CHANGE_DIRECTORY_COMMAND = "cd" LIST_CONTENTS_COMMAND = "ls" SMALL_DIRECTORY_SIZE_THRESHOLD = 100_000 +TOTAL_DISK_SPACE = 70_000_000 From f15e860db2e956c31a1a6f010c7b8fde4ff2794d Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 16 Dec 2022 21:40:54 +0200 Subject: [PATCH 33/34] Add constant for required unused space --- puzzles/solutions/2022/d07/consts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/puzzles/solutions/2022/d07/consts.py b/puzzles/solutions/2022/d07/consts.py index dfa27337..191b3401 100644 --- a/puzzles/solutions/2022/d07/consts.py +++ b/puzzles/solutions/2022/d07/consts.py @@ -6,3 +6,4 @@ LIST_CONTENTS_COMMAND = "ls" SMALL_DIRECTORY_SIZE_THRESHOLD = 100_000 TOTAL_DISK_SPACE = 70_000_000 +REQUIRED_UNUSED_SPACE = 30_000_000 From 43dfb1e6eed4cd8d6074f15811c36cb3f1b62b93 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 17 Dec 2022 13:19:02 +0200 Subject: [PATCH 34/34] Add function that size of the smallest directory that should be deleted --- puzzles/solutions/2022/d07/p2.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/puzzles/solutions/2022/d07/p2.py b/puzzles/solutions/2022/d07/p2.py index c3bbf496..18aa3959 100644 --- a/puzzles/solutions/2022/d07/p2.py +++ b/puzzles/solutions/2022/d07/p2.py @@ -1,8 +1,24 @@ import sys +import consts +import p1 + def get_answer(input_text: str): - raise NotImplementedError + """Return total size of the smallest directory that, if deleted, would free up enough space on the filesystem.""" + filesystem = p1.get_filesystem(input_text) + root = filesystem[0] + total_used_space = root.size + unused_space_needed = consts.REQUIRED_UNUSED_SPACE - ( + consts.TOTAL_DISK_SPACE - total_used_space + ) + candidate_directories = ( + directory for directory in filesystem if directory.size >= unused_space_needed + ) + directory_to_delete = min( + candidate_directories, key=lambda directory: directory.size + ) + return directory_to_delete.size if __name__ == "__main__":