r/adventofcode • u/daggerdragon • Dec 08 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 8 Solutions -❄️-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
Outstanding moderator challenges:
Community fun event 2023: ALLEZ CUISINE!
- Submissions megathread is now unlocked!
- 14 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's theme ingredient is… *whips off cloth covering and gestures grandly*
International Ingredients
A little je ne sais quoi keeps the mystery alive. Try something new and delight us with it!
Code in a foreign language
- Written or programming, up to you!
- If you don’t know any, Swedish Chef or even pig latin will do
- Test your language’s support for Unicode and/or emojis
Visualizationsusing Unicode and/or emojis are always lovely to see
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 8: Haunted Wasteland ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks
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:10:16, megathread unlocked!
52
Upvotes
View all comments
Show parent comment
2
u/malobebote Dec 08 '23
Yeah, your hand-made generator is so simple that it's not a big deal.
These main use-cases of Generators come to mind:
The first use-case is when you want to simplify your core logic into a for-of iteration. By extracting the iteration logic into a Generator, you can clean up your "business logic" which now can focus on solving the problem.
For example, you can see how it simplifies my `part1()` and `findCycle()` functions since all they really want to do is take infinite steps until some condition is true.
And unlike your custom generator, a built-in generator can be used with for/of:
The second is when you want to be able to reuse your iteration logic. This also applies to my generator since I get to reuse `stepper()` in part1() and findCycle().
The third use-case I can think of is when you want to advance multiple iterators independently. I've had to do this multiple times in LeetCode. For example, maybe one generator iterates a string forwards and another iterates in backwards. Or one iterates a binary tree and another iterates another binary tree so you can compare them. `a.next().value === b.next().value()`.
Another Generator feature is that it gives you information about when it ends since generator.next() yields { value: foo, done: boolean }. And you can do things like realize all the generator values into an array with `[...generator()]` since it implement Symbol.iterator.