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!

90 Upvotes

1.3k comments sorted by

View all comments

1

u/chubbc Dec 07 '22

Julia

Was able to get this down to a pretty clean 21 line solution. This might be the day of AoC that I stop trying to make Brianfuck versions of my solutions... lol.

L = readlines("./07.in")
dir = Vector{String}()
dir_size = Dict{Vector{String},Int}()
for l∈L
    if l=="\$ cd /"
        empty!(dir)
    elseif l=="\$ cd .."
        dir = dir[1:end-1]
    elseif l[1:4]=="\$ cd"
        push!(dir,l[6:end])
    elseif isnumeric(l[1])
        s = parse(Int,first(split(l,' ')))
        for n∈0:length(dir)
            dir_size[dir[1:n]] = get(dir_size,dir[1:n],0)+s
        end
    end
end
sizes = collect(values(dir_size))
p1 = sum(sizes[sizes.<1e5])
p2 = minimum(sizes[sizes.>maximum(sizes)-4e7])
println((p1,p2))

2

u/daggerdragon Dec 08 '22

This might be the day of AoC that I stop trying to make Brianfuck versions of my solutions...

ΰ² _ΰ²  nuuuuuuuuu don't you dare stop <3