X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=adventofcode16%2Fapp%2Fadvent23.hs;h=2903967e8b37f64624dd7cc3eea301717050bc0d;hb=HEAD;hp=20c4a15154df24f540eb67119acb9cd1b4c6e51c;hpb=58f2c7df3af645c9121ed507e55c9f7205b7012a;p=advent-of-code-16.git diff --git a/adventofcode16/app/advent23.hs b/adventofcode16/app/advent23.hs index 20c4a15..2903967 100644 --- a/adventofcode16/app/advent23.hs +++ b/adventofcode16/app/advent23.hs @@ -21,7 +21,6 @@ data Machine = Machine { a :: Int , instructions :: [Instruction]} deriving (Show, Eq) - testInstructions = "cpy 2 a\n\ \tgl a\n\ \tgl a\n\ @@ -30,7 +29,6 @@ testInstructions = "cpy 2 a\n\ \dec a\n\ \dec a" - emptyMachine :: Machine emptyMachine = Machine {a=0, b=0, c=0, d=0, pc=0, instructions=[]} @@ -162,15 +160,14 @@ writeValue m (Register r) v = instructionFile = instructionLine `sepEndBy` newline instructionLine = incL <|> decL <|> cpyL <|> jnzL <|> tglL -incL = (Inc . Register) <$> (string "inc" *> spaces *> (oneOf "abcd")) -decL = (Dec . Register) <$> (string "dec" *> spaces *> (oneOf "abcd")) -cpyL = cpyify <$> (string "cpy" *> spaces *> ((Literal <$> int) <|> ((Register . head) <$> (many1 letter)))) - <*> (spaces *> (oneOf "abcd")) - where cpyify s r = Cpy s (Register r) -jnzL = jnzify <$> (string "jnz" *> spaces *> ((Literal <$> int) <|> ((Register . head) <$> (many1 letter)))) - <*> (spaces *> ((Literal <$> int) <|> ((Register . head) <$> (many1 letter)))) - where jnzify r o = Jnz r o -tglL = Tgl <$> (string "tgl" *> spaces *> ((Literal <$> int) <|> ((Register . head) <$> (many1 letter)))) +incL = Inc <$> (string "inc" *> spaces *> register) +decL = Dec <$> (string "dec" *> spaces *> register) +cpyL = Cpy <$> (string "cpy" *> spaces *> location) <*> (spaces *> register) +jnzL = Jnz <$> (string "jnz" *> spaces *> location) <*> (spaces *> location) +tglL = Tgl <$> (string "tgl" *> spaces *> location) + +location = (Literal <$> int) <|> register +register = Register <$> (oneOf "abcd") parseIfile :: String -> Either ParseError [Instruction] parseIfile input = parse instructionFile "(unknown)" input @@ -180,4 +177,4 @@ parseIline input = parse instructionLine "(unknown)" input successfulParse :: Either ParseError [a] -> [a] successfulParse (Left _) = [] -successfulParse (Right a) = a \ No newline at end of file +successfulParse (Right a) = a