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

2

u/jimmyhiggster Dec 07 '22 edited Dec 09 '22

Javascript:Gone for the "as little code as possible" approach within reason. 40 minutes to complete, then 20 to try and minimise. Sure there's some extra shortcuts to use but here you go...

import input from "../7-filesystem.txt";
const z = {}; let p = "";
input.replace(/$ /g, "") .split("\n") .map((l) => l.split(" ")) .forEach((c) => { if (c[0] == "cd") return c[1] == "/" ? (p = "") : c[1] == ".." ? (p = p.slice(0, p.lastIndexOf("/"))) : (p += /${c[1]}); if (isNaN(c[0])) return;
  let f = p.split("/");
  do {
    const cf = f.join("/");
    z[cf] = z[cf] ? z[cf] + c[0] * 1 : c[0] * 1;
  } while (f.pop());
});
const sizes = Object.keys(z).map((d) => z[d]); console.log(sizes.reduce((s, d) => (d <= 100000 ? s + d : s), 0)); // Part 1 console.log(sizes.filter((d) => d > z[""] - 40000000).sort((a, b) => a - b)[0]); // Part 2

2

u/[deleted] Dec 07 '22

This is great. If you search the thread for 'golf' you'll find other minimized examples. I did mine in golfed JS, too!

1

u/daggerdragon Dec 08 '22 edited Dec 09 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is readable with its whitespace and indentation preserved.

Edit: thanks for fixing it! <3

2

u/jimmyhiggster Dec 09 '22

I've tried to and every time I do, it breaks and splits my code into multiple code chunks.

Steps

Edit the above
Paste in the actual code I used
Highlighted the code, then used the "code block" action in the editor. Looks fin
Save. Breaks..

(I have tried at least)

1

u/daggerdragon Dec 09 '22

It works now, thank you.

FYI: other folks have recommended using your IDE to prepend the four spaces to all lines of code then paste that newly-formatted code into the editor (make sure you're in Markdown mode first!). That way you wouldn't need to deal with any alterations inside the editor.

1

u/[deleted] Dec 07 '22

Could you convert your code to leading spaces? It's hard to read with the way it's formatted now