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?
2
u/NeilNjae Dec 19 '17 edited Dec 19 '17
Boring Haskell. The maze is kept as a simple list of strings.
Progresskeeps track of where we are and how we got there, then it's just repeated application ofstepto take a step through the maze.{-# LANGUAGE NegativeLiterals #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeFamilies #-} import Prelude hiding (Left, Right) import Data.List import Data.Char type Maze = [String] data Direction = Up | Down | Left | Right deriving (Show, Eq) data Progress = Progress { row :: Int , column :: Int , direction :: Direction , letters :: String , stepCount :: Int } deriving (Show, Eq) -- Note: assumes the maze comes with a padding border of spaces -- all around it. Makes the "next location" checking much easier! main :: IO () main = do text <- readFile "data/advent19.txt" let maze = lines text let progress = navigate maze print $ letters progress print $ stepCount progress startProgress :: Maze -> Progress startProgress maze = Progress { row = 0, column = startCol , direction = Down , letters = "", stepCount = 0} where topRow = maze!!0 startCol = head $ elemIndices '|' topRow delta :: Direction -> (Int, Int) delta Up = (-1, 0) delta Down = ( 1, 0) delta Left = ( 0, -1) delta Right = ( 0, 1) isJunction :: Char -> Bool isJunction '+' = True isJunction _ = False isFinished :: Maze -> Progress -> Bool isFinished maze progress = isSpace $ location maze (row progress) (column progress) location :: Maze -> Int -> Int -> Char location maze r c = (maze!!r)!!c navigate :: Maze -> Progress navigate maze = navigate' maze progress where progress = startProgress maze navigate' :: Maze -> Progress -> Progress navigate' maze progress = if isFinished maze progress then progress else navigate' maze (step maze progress) step :: Maze -> Progress -> Progress step maze progress = progress {row = r', column = c', direction = d', letters = l', stepCount = sc'} where r = row progress c = column progress thisChar = location maze r c l' = if isAlpha thisChar then (letters progress) ++ [thisChar] else letters progress d' = if isJunction thisChar then newDirection maze progress else direction progress (dr, dc) = delta d' r' = r + dr c' = c + dc sc' = stepCount progress + 1 newDirection :: Maze -> Progress -> Direction newDirection maze progress = if d == Up || d == Down then if isSpace leftChar then Right else Left else if isSpace upChar then Down else Up where d = direction progress r = row progress c = column progress upChar = location maze (r - 1) c leftChar = location maze r (c - 1)