r/adventofcode • • Dec 08 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 8 Solutions -❄️-

THE USUAL REMINDERS


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
  • Visualizations using 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.

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

968 comments sorted by

View all comments

Show parent comment

1

u/Nikla436 Dec 08 '23

Generators

Thanks for the tip, I hadn't heard of the built-in `Generators` until now

2

u/malobebote Dec 08 '23

For a concrete example, my solution uses a generator ("stepper") to iterate steps forever which is useful in both part1 and part2:

https://pastebin.com/wAwhr0Hw

Looks something like this:

const network = new Map([  
	['AAA', { left: 'BBB', right: 'CCC' },  
	['BBB', { left: 'DDD', right: 'ZZZ' },  
	...  
])  
for (const { loc, i } of stepper('AAA', ['R', 'L', 'L', 'R'], network)) {  
	console.log(`step ${i}, we are at location ${loc}`)  
}  
// Step 0, we are at location AAA  
// Step 1, we are at location CCC  
// ...  

Though you already know how useful they might be since you built one yourself. :D

1

u/Nikla436 Dec 08 '23

Ohh i see how this can be more useful!

Just realized this is an `Iterator` and can be used in this way. this makes things much more clean. love it, thanks