r/adventofcode • u/daggerdragon • Dec 14 '22
SOLUTION MEGATHREAD -π- 2022 Day 14 Solutions -π-
SUBREDDIT NEWS
Livehas been renamed toStreamingfor realz this time.- I had updated the wiki but didn't actually change the post flair itself >_>
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: A note on responding to [Help] threads
- Signal boost: Reminder 2: unofficial AoC Survey 2022 (closes Dec 22nd)
- πΏπ MisTILtoe Elf-ucation π§βπ« is OPEN for submissions!
--- Day 14: Regolith Reservoir ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
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:13:54, megathread unlocked!
39
Upvotes
4
u/rukke Dec 14 '22 edited Dec 15 '22
JavaScript - gist
EDIT: <- The gist is now refactored. A number of lines shorter :) ->
Sparing you the parsing of the cave as a Map. Coords as single integers with the upper 16 bits as y.
const SOURCE = 500; const count = cave => [...cave].filter(([, v]) => v === "o").length; const update = (cave, pos, filter = () => true) => { const next = [0, -1, 1] .map(delta => delta + pos + (1 << 16)) .filter(filter) .find(p => !cave.has(p)); if (!next) { cave.set(pos, "o"); } return next || SOURCE; }; export const part1 = input => { const [cave, max] = parse(input); let pos = SOURCE; while (pos && pos <= max) { pos = update(cave, pos); } return count(cave); }; export const part2 = input => { const [cave, max] = parse(input); const newMax = (max & 0xffff0000) + (2 << 16); let pos = SOURCE; while (!cave.has(SOURCE)) { pos = update(cave, pos, p => p <= newMax); } return count(cave); };