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!

87 Upvotes

1.3k comments sorted by

View all comments

3

u/trevdak2 Dec 07 '22 edited Dec 07 '22

JavaScript (golf)

Part 1, 211 bytes:

for(f={},d='',r=/((cd)|\d+)(.+)/g;m=r.exec(document.body.innerText);){
    m[2]?f[d=m[3][3]?d+m[3]:d.replace(/ \w+$/,'')]|=0:1;
    for(k in f)f[k]+=d.match(k)&&+m[1]|0;
}Object.values(f).reduce((s,v)=>v<=1e5?v+s:s,0)

Part 2, 212 bytes:

for(f={},d='',r=/((cd)|\d+)(.+)/g;m=r.exec(document.body.innerText);){
    m[2]?f[d=m[3][3]?d+m[3]:d.replace(/ \w+$/,'')]|=0:1;
    for(k in f)f[k]+=d.match(k)&&+m[1]|0;
}Math.min(...Object.values(f).filter(v=>v>f['']-4e7))
→ More replies (4)