Skip to content

Instantly share code, notes, and snippets.

@dozed

dozed/day5.hs Secret

Created December 9, 2022 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dozed/2bd3f4321c012727f9587d59450d098c to your computer and use it in GitHub Desktop.
Save dozed/2bd3f4321c012727f9587d59450d098c to your computer and use it in GitHub Desktop.
{-# LANGUAGE QuasiQuotes #-}
module Day5 where
import Control.Monad (void)
import Data.List (transpose)
import Data.Maybe (catMaybes)
import Text.Parsec
import Text.Parsec.String
import Text.RawString.QQ
import Util (regularParse, replaceAtIndex)
testInput1 :: String
testInput1 = [r|
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
|]
type Item = Maybe Char
data MoveSpec = MoveSpec {
num :: Int,
from :: Int,
to :: Int
} deriving Show
emptyItemParser :: Parser Item
emptyItemParser = Nothing <$ try (string " ")
filledItemParser :: Parser Item
filledItemParser = do
void $ char '['
c <- anyChar
void $ char ']'
return $ Just c
itemParser :: Parser Item
itemParser = emptyItemParser <|> filledItemParser
itemsParser :: Parser [Item]
itemsParser = sepBy1 itemParser $ char ' '
stackIndexParser :: Parser [Char]
stackIndexParser = sepBy1 (char ' ' *> digit <* char ' ') (char ' ') <* endOfLine
moveSpecParser :: Parser MoveSpec
moveSpecParser = do
void $ string "move "
num <- digit
void $ string " from "
from <- digit
void $ string " to "
to <- digit
return MoveSpec { num = read [num], from = read [from], to = read [to] }
inputParser :: Parser ([[Item]], [Char], [MoveSpec])
inputParser = do
void endOfLine
items <- endBy1 itemsParser endOfLine
idxs <- stackIndexParser
void $ endOfLine
specs <- endBy1 moveSpecParser endOfLine
return (items, idxs, specs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment