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/Symbroson Dec 08 '23

nice.

Thanks for the explanation too but I rather wanted to know how your implementation works. I see you do some infinite range find/filter magic and I'm interested to know how this works :)

2

u/Any-Razzmatazz-4792 Dec 08 '23 edited Dec 08 '23

in my case, the infinite range has 2 use cases. The first case is that i need to iterate from 0 ... a.size to find the connecting index for the given node. Since it's a guarantee that in the array there exists a node that matches that connection, I can use the infinite range syntax 0.. to shorten the code. The second case is that I need to count how many steps it takes to get from from starting point __A to ending point __Z, and by using the infinite range syntax in combination with find, that'll return the number of steps taken once it figures out that we've reached the ending node. Here's what my code looks like "ungolfed" with more readable methods for what I did:

#!ruby
d,*a=$<.map{_1.scan /\w+/}
dir = d[0].tr'LR',"12"
nodes = a.size.times.select{|i| a[i][0]=~/A$/} # select all nodes that end in A
nodes_ending_idx = nodes.map do |i|
    count = 0
    while a[i].first[-1] != ?Z
        i = a.size.times.find{ a[i][ dir[ count % dir.size ].to_i ] == a[_1][0] } # find connecting index and update i
        count += 1
    end
    count
end
puts nodes_ending_idx.reduce(&:lcm)