Unable to load image

Advent of Code 2022: Day 7

https://adventofcode.com
with open('AdventOfCode2022/input.txt', 'r') as input:
    commands = input.read().split('\n')

    def new_dict(name, parent=None):
        return {
        'name': name,
        'parent':parent,
        'files': 0,
        'dirs': {}
    }

    root_dict = new_dict(name='/', parent=None)
    curr_dict = root_dict

    #Build tree of dicts
    for i in range(len(commands)):
        command_parts = commands[i].split(' ')
        if command_parts[0] == '$':
            if command_parts[1] == 'cd':
                if command_parts[2] == '..':
                    curr_dict = curr_dict['parent']
                elif command_parts[2] == '/':
                    pass
                else:
                    curr_dict = curr_dict['dirs'][command_parts[2]]
        elif command_parts[0] == 'dir':
            if command_parts[1] not in curr_dict:
                curr_dict['dirs'][command_parts[1]] = new_dict(name=command_parts[1], parent=curr_dict)
        elif command_parts[0] == '':
            pass
        else:
            curr_dict['files'] += int(command_parts[0])

    #PART ONE

    MAX_THRESHOLD = 100000
    FINAL_COUNTER = 0
    dict_list = []
    
    def parse_dict(t_dict):
        folder_total = t_dict['files']

        for c_dict in t_dict['dirs'].keys():
            folder_total += parse_dict(t_dict['dirs'][c_dict])

        global dict_list
        dict_list.append(folder_total)

        if folder_total < MAX_THRESHOLD:
            global FINAL_COUNTER # this is the most embarrassing thing I've ever done
            FINAL_COUNTER += folder_total
        return folder_total

    total_used = parse_dict(root_dict)
    print(FINAL_COUNTER)

    #PART TWO
    TOTAL_SPACE = 70000000
    WANTED_SPACE = 30000000
    curr_wanted = WANTED_SPACE - (TOTAL_SPACE - total_used)
    
    larger_than_wanted = []
    for i in dict_list:
        if i > curr_wanted:
            larger_than_wanted.append(i)

    print(min(larger_than_wanted))
32
Jump in the discussion.

No email address required.

:tayhyperdab:

r←⊢⎕FIO[49]'7' ⋄ d←'' ⋄ f←''
∇p i
→({⍵≡'$ cd ..'} i)⍴ Lu
→({⍵≡'$ ls'} i)⍴ Ls
→({('$ cd '≡5↑⍵)∧(⍵≢'$ cd ..')} i)⍴ Ln
→({'dir '≡4↑⍵} i)⍴ Ld
→({'0123456789'∊⍨1⌷⍵} i)⍴ Lf
Lu: d←⌽1↓⌽d ⋄ →0
Ls: →0
Ln: d←d,⊂5↓i ⋄ →0
Ld: x←d,⊂4↓i ⋄ f←f,(⊂d x) ⋄ →0
Lf: x←{⍎⊃1⌷⍵⊂⍨' '≠⍵}i ⋄ f←f,(⊂d x) ⋄ →0 ∇
p¨r ⋄ ul←↑¨∪{1⌷⍵}¨f
li←(ul⍳{⊃1⌷⍵}¨f)⊂↑¨{2⌷⍵}¨f
∇z←eo v
→(0≡↑⍴v)⍴ Ln
→(1≡ul∊⍨(⊂v))⍴ Lu
→(~((0≡↑⍴v)∨1≡ul∊⍨(⊂v)))⍴ Le
Ln: z←v ⋄ →0
Lu: z←+/ex ((ul⍳⊂v)⌷li) ⋄ →0
Le: z←ex v ⋄ →0 ∇
∇z←ex c
z←+/eo¨c ∇
s←ex¨li
↑+/⊃,/(s≤100000)⊂s
⌊/⊃,/(s≥((⌈/s)+¯40000000))⊂s
Jump in the discussion.

No email address required.

Is this (picrel) the same language? If yes that's quite the mogging

Why are your solutions so lengthy? Am I missing something?

![](/images/16704604139879818.webp)

Jump in the discussion.

No email address required.

They're using dyalog APL (still being developed today), I'm stuck with GNU APL (was cutting edge in the 1980s). If you have a choice definitely go for dyalog.

Jump in the discussion.

No email address required.

do you own an apl keyboard?

also i checked out https://www.dyalog.com/uploads/documents/MasteringDyalogAPL.pdf and had a hearty chuckle at this considering what most apl looks like.

You can see that APL behaves like any hand-held calculator with, however, a small difference; multiplication is represented by the multiplication symbol ( × ) which is used in schools in many countries; likewise for division ( ÷ ).

In most other computer languages, a star * is used for Multiply and / for Divide. This is a legacy of the early days of computers, when the character set was limited to the one available on a typewriter. At the time it was decided to use * and / in place of × and ÷. But it is now possible to display any type of symbol on a screen and on a printer, and this transposition is no longer justifyable. The use of common symbols, which are taught all over the world, aids the understanding of APL by non programmers.

Jump in the discussion.

No email address required.

What language?

Ah, APL?

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.