Skip to content

Instantly share code, notes, and snippets.

@magthe
Created Dec 18, 2017
Embed
What would you like to do?
AoC 2017, day 16
import Control.Applicative
import Data.Monoid
import Data.Vector as V
import Prelude as P
import Text.ParserCombinators.ReadP
spinList :: Int -> Vector Char -> Vector Char
spinList n xs = let (h, t) = V.splitAt (l - n) xs
l = V.length xs
in t <> h
exchangeElementsAt :: Int -> Int -> Vector Char -> Vector Char
exchangeElementsAt i j xs = let m = xs ! i
n = xs ! j
in xs // [(i, n), (j, m)]
swapElements :: Char -> Char -> Vector Char -> Vector Char
swapElements m n xs = let (Just i) = elemIndex m xs
(Just j) = elemIndex n xs
in xs // [(i, n), (j, m)]
readMoves = sepBy readMove (char ',')
readMove = choice [readSpin, readExchange, readPartner]
readSpin = spinList <$> (char 's' *> readS_to_P reads)
readExchange = exchangeElementsAt <$> (char 'x' *> readS_to_P reads) <*> (char '/' *> readS_to_P reads)
readPartner = swapElements <$> (char 'p' *> get) <*> (char '/' *> get)
runMoves ps ms = P.foldr ($) (fromList ps) . P.reverse . fst . P.last $ readP_to_S readMoves ms
-- example use
-- progs = "abcde"
-- moves = "s1,x3/4,pe/b"
-- runMoves progs moves
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment