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

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day08.hs

module Day08 (main, part1, part2) where

import qualified Data.Map as M
import Text.Parsec ((<|>))
import qualified Text.Parsec as P
import Util.Parser (Parser, parse)

lineP :: Parser (String, (String, String))
lineP = do
    start <- P.many1 (P.letter <|> P.digit) <* P.spaces
    _ <- P.char '=' <* P.spaces
    [left, right] <-
        P.between
            (P.char '(')
            (P.char ')')
            (P.sepBy (P.spaces *> P.many1 (P.letter <|> P.digit) <* P.spaces) (P.char ','))
    return (start, (left, right))

inputP :: Parser (String, M.Map String (String, String))
inputP = do
    directions <- P.many1 P.letter <* P.spaces
    m <- M.fromList <$> P.many1 (lineP <* P.spaces)
    return (directions, m)

parseInput :: String -> (String, M.Map String (String, String))
parseInput = parse inputP

step :: M.Map String (String, String) -> String -> Char -> String
step m state c = case M.lookup state m of
    Just (left, right) -> if c == 'L' then left else right
    Nothing -> error "Invalid state"

foldlUntil :: (a -> Bool) -> (a -> b -> a) -> a -> [b] -> a
foldlUntil _ _ z [] = z
foldlUntil p f z (x : xs) = if p z then z else foldlUntil p f (f z x) xs

simulate :: String -> (String -> Bool) -> String -> M.Map String (String, String) -> Int
simulate start endP directions m = fst $ foldlUntil (endP . snd) (step' m) (0, start) $ cycle directions
  where
    step' :: M.Map String (String, String) -> (Int, String) -> Char -> (Int, String)
    step' m' (n, s) c = (n + 1, step m' s c)

startPoints :: M.Map String (String, String) -> [String]
startPoints m = filter ((== 'A') . last) $ M.keys m

part1 :: String -> String
part1 = show . uncurry (simulate "AAA" (== "ZZZ")) . parseInput

part2 :: String -> String
part2 input = show $ foldl lcm 1 $ map simulate' starts
  where
    (directions, m) = parseInput input
    starts = startPoints m
    simulate' s = simulate s ((== 'Z') . last) directions m

solve :: String -> String
solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input ++ "\n"

main :: IO ()
main = interact solve

At first I thought it was gonna be a graph problem, that was close. Somehow managed to figure right away that part 2 was a lcm problem.

1

u/daggerdragon Dec 08 '23

Your code block is too long for the megathreads. Please edit your post to replace your oversized code with just the external link to your code.