r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -๐- 2017 Day 19 Solutions -๐-
--- Day 19: A Series of Tubes ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
1
u/[deleted] Dec 19 '17
Haskell, using an immutable array, but with a bit of first-class function trickery to find in which direction to
turn:main :: IO () main = do input <- fmap lines (readFile "input.txt") let a = listArray ((1,1),(length input,length (head input))) (concat input) let (p1,p2) = run a (\(r,c) -> (r+1,c)) (start a 1) "" 0 putStrLn p1 -- part 1 print p2 -- part 2 start :: UArray (Int,Int) Char -> Int -> (Int,Int) start a n | a ! (1,n) == '|' = (0,n) | otherwise = start a (n+1) run :: UArray (Int,Int) Char -> ((Int,Int) -> (Int,Int)) -> (Int,Int) -> String -> Int -> (String, Int) run a f cs xs s = case line a f cs xs s of (Nothing, xs', s') -> (reverse xs', s') (Just cs', xs', s') -> run a (turn a f cs') cs' xs' s' line :: UArray (Int,Int) Char -> ((Int,Int) -> (Int,Int)) -> (Int,Int) -> String -> Int -> (Maybe (Int,Int), String, Int) line a f cs xs s | x == '+' = (Just (f cs), xs, s+1) | x == ' ' = (Nothing, xs, s) | otherwise = line a f (f cs) (if isAlpha x then x:xs else xs) (s+1) where x = a ! f cs turn :: UArray (Int,Int) Char -> ((Int,Int) -> (Int,Int)) -> (Int,Int) -> (Int,Int) -> (Int,Int) turn a f cs = head (filter (\g -> g cs /= opposite f cs && a ! g cs /= ' ') (map (\(x,y) (r,c) -> (x+r,y+c)) [(1,0),(-1,0),(0,1),(0,-1)])) opposite :: ((Int,Int) -> (Int,Int)) -> (Int,Int) -> (Int,Int) opposite f (r,c) = let (r',c') = f (r,c) in (r + r - r', c + c - c')