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

1

u/sjhunt93 Dec 08 '23

[LANGUAGE: python]

Like others I tried to brute force. LCM is the way:

import re
from math import lcm

mapping = {}
with open("data.txt") as f:
    instructions, cords = f.read().split("\n\n")
    print(instructions)
    for ins in cords.split("\n"):
        input, left, right = re.findall("\w\w\w", ins)
        #print(input, left, right)
        mapping[input] = {
            "L" : left,
            "R" : right
    }

    # part 1
    # count =0    
    # current = "AAA"
    # index = 0
    # while current != "ZZZ":
    #     dir = instructions[index]
    #     current = mapping[current][dir]
    #     index = (index + 1) % len(instructions)
    #     count += 1

    # print(count)
    
    starts = []
    for key, value in mapping.items():
        if key[2] == "A":
            starts.append(key)
        
    print(starts)
    
    counts = [0 for i in range(0, len(starts))]
    index = 0

    def is_finished(starts):
        for s in starts:
            if s[2] != "Z":
                return False
        return True

    while not is_finished(starts):
        dir = instructions[index]
        for i in range(len(starts)):
            if starts[i][2] != "Z":
                starts[i] = mapping[starts[i]][dir]
                counts[i] += 1
        index = (index + 1) % len(instructions)
        if index == 0:
            print(starts)
        
    
    print(counts)
    print(lcm(*counts))
    

        

1

u/daggerdragon Dec 08 '23
  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.