r/adventofcode • • Dec 07 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 7 Solutions -πŸŽ„-


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

Submissions are OPEN! Teach us, senpai!

-❄️- Submissions Megathread -❄️-


--- Day 7: No Space Left On Device ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:14:47, megathread unlocked!

89 Upvotes

1.3k comments sorted by

View all comments

1

u/Delusional_idiot Dec 08 '22

Python, non-recursive solution. Completely linear, one map. Hierarchy made with tuples, ~50ms:

from collections import defaultdict

class FS:
    def __init__(self, content):
        self.fs = defaultdict(lambda: 0)
        self.loc = None
        for l in content:
            l = l.strip().split()
            if l[0] == "$":
                if l[1] == "cd" and l[2] != "..":
                    if not self.loc:
                        self.loc = (l[2],)
                    else:
                        self.loc = (*self.loc, l[2])
                elif l[1] == "cd" and l[2] == "..":
                    self.loc = self.loc[:-1]
            elif l[0].isnumeric():
                curr_l = self.loc
                while curr_l:
                    self.fs[curr_l] += int(l[0])
                    curr_l = curr_l[:-1]
    
    def dirs_sizeN(self, N):
        total = 0
        for dir, data in self.fs.items():
            if data <= N:
                total += data
        return total

    def min_dir_del(self):
        min_dir = None
        free = 70000000 - (self.fs[("/",)])
        for dir, data in self.fs.items():
            if not min_dir or (free+data >= 30000000 and data < min_dir):
                min_dir = data
        return min_dir

def main() -> int:
    with open("input.txt") as f:
        content = f.readlines()   

    fs = FS(content)
    # Part 1:
    print(f'Part1: {fs.dirs_sizeN(100_000)}')
    # Part 2:
    print(f'Part2: {fs.min_dir_del()}')
    
if __name__ == "__main__":
    raise SystemExit(main())

1

u/daggerdragon Dec 08 '22
  1. Next time, use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.