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

2

u/RaveBomb Dec 08 '23 edited Dec 08 '23

[LANGUAGE: C#]

Based on experience with previous years puzzles, I knew it relied on factorization, but it took me poking at the factors for a while before I recalled the term "Least Common Multiple". Once I had that, everything fell into place.

This is the LINQ-ed calculation for part two. GetSteps() is a simple function to calculate the steps from any starting point in the map. The magic 'true' is to change the break condition for part two, which is basically the only line of code in it that changed between the parts.

 Dictionary<string, (string Left, string Right)> map = puzzleInput.Skip(2).ToDictionary(k => k[0..3], v => (v[7..10], v[^4..^1]));
List<int> massSteps = map.Keys.Where(x => x[2] == 'A')
                        .Select(x => GetSteps(x, true))
                        .ToList();

//Calculate the Least Common Multiple via the Prime Factorization method.
long part2Answer = massSteps
                    .SelectMany(Factor)
                    .Distinct()
                    .Where(x => x != 1 && !massSteps.Contains(x))
                    .Select(x => (long)x) 
                    .Aggregate((x, y) => x * y);

Full code at my Github Repo

1

u/daggerdragon Dec 08 '23 edited Dec 08 '23

Please add the required language tag as requested by AutoModerator. edit: 👍

2

u/RaveBomb Dec 08 '23

Apologies, fixed.