|
| 1 | +import Text.ParserCombinators.Parsec |
| 2 | +import Text.ParserCombinators.Parsec.Number (int) |
| 3 | +import qualified Data.List as List |
| 4 | +import qualified Data.Map.Strict as Map |
| 5 | + |
| 6 | +newtype Register = Register Char deriving Show |
| 7 | +newtype Value = Value Int deriving Show |
| 8 | + |
| 9 | +addV :: Value -> Int -> Value |
| 10 | +addV (Value v) x = Value (v + x) |
| 11 | + |
| 12 | +data ValueOrRegister = V Value | R Register deriving Show |
| 13 | + |
| 14 | +data Instruction = Cpy ValueOrRegister Register |
| 15 | + | Inc Register |
| 16 | + | Dec Register |
| 17 | + | Jnz ValueOrRegister Value |
| 18 | + deriving Show |
| 19 | + |
| 20 | +data Registers = Registers Value Value Value Value deriving Show |
| 21 | + |
| 22 | +defaultR :: Registers |
| 23 | +defaultR = Registers (Value 0) (Value 0) (Value 0) (Value 0) |
| 24 | + |
| 25 | +getR :: Registers -> Register -> Value |
| 26 | +getR (Registers a _ _ _) (Register 'a') = a |
| 27 | +getR (Registers _ b _ _) (Register 'b') = b |
| 28 | +getR (Registers _ _ c _) (Register 'c') = c |
| 29 | +getR (Registers _ _ _ d) (Register 'd') = d |
| 30 | + |
| 31 | +setR :: Registers -> Register -> Value -> Registers |
| 32 | +setR (Registers _ b c d) (Register 'a') a = Registers a b c d |
| 33 | +setR (Registers a _ c d) (Register 'b') b = Registers a b c d |
| 34 | +setR (Registers a b _ d) (Register 'c') c = Registers a b c d |
| 35 | +setR (Registers a b c _) (Register 'd') d = Registers a b c d |
| 36 | + |
| 37 | +incR :: Registers -> Register -> Registers |
| 38 | +incR rs r = setR rs r (addV (getR rs r) 1) |
| 39 | + |
| 40 | +decR :: Registers -> Register -> Registers |
| 41 | +decR rs r = setR rs r (addV (getR rs r) (-1)) |
| 42 | + |
| 43 | +getValueOrRegister :: Registers -> ValueOrRegister -> Value |
| 44 | +getValueOrRegister _ (V val) = val |
| 45 | +getValueOrRegister regs (R reg) = getR regs reg |
| 46 | + |
| 47 | +data Context = Context { program :: [Instruction] |
| 48 | + , registers :: Registers |
| 49 | + , iptr :: Int |
| 50 | + } deriving Show |
| 51 | + |
| 52 | +defaultContext :: [Instruction] -> Context |
| 53 | +defaultContext is = Context is defaultR 0 |
| 54 | + |
| 55 | +execute :: Context -> Context |
| 56 | +execute c@(Context is _ iptr) = if iptr < (List.length is) then execute (execute_ c (is !! iptr)) else c |
| 57 | + |
| 58 | +execute_ :: Context -> Instruction -> Context |
| 59 | +execute_ (Context is regs iptr) (Cpy vor reg) = Context is (setR regs reg (getValueOrRegister regs vor)) (iptr + 1) |
| 60 | +execute_ (Context is regs iptr) (Inc reg) = Context is (incR regs reg) (iptr + 1) |
| 61 | +execute_ (Context is regs iptr) (Dec reg) = Context is (decR regs reg) (iptr + 1) |
| 62 | +execute_ (Context is regs iptr) (Jnz vor (Value offset)) = Context is regs (case getValueOrRegister regs vor of |
| 63 | + Value 0 -> iptr + 1 |
| 64 | + _ -> iptr + offset) |
| 65 | + |
| 66 | + |
| 67 | +-- Parser |
| 68 | +register :: Parser Register |
| 69 | +register = Register <$> oneOf "abcd" |
| 70 | + |
| 71 | +value :: Parser Value |
| 72 | +value = Value <$> int |
| 73 | + |
| 74 | +valueOrRegister :: Parser ValueOrRegister |
| 75 | +valueOrRegister = V <$> value <|> R <$> register |
| 76 | + |
| 77 | +cpy :: Parser Instruction |
| 78 | +cpy = do |
| 79 | + string "cpy " |
| 80 | + vor <- valueOrRegister |
| 81 | + char ' ' |
| 82 | + reg <- register |
| 83 | + pure (Cpy vor reg) |
| 84 | + |
| 85 | +inc :: Parser Instruction |
| 86 | +inc = Inc <$> (string "inc " >> register) |
| 87 | + |
| 88 | +dec :: Parser Instruction |
| 89 | +dec = Dec <$> (string "dec " >> register) |
| 90 | + |
| 91 | +jnz :: Parser Instruction |
| 92 | +jnz = do |
| 93 | + string "jnz " |
| 94 | + vor <- valueOrRegister |
| 95 | + char ' ' |
| 96 | + val <- value |
| 97 | + pure (Jnz vor val) |
| 98 | + |
| 99 | +instruction :: Parser Instruction |
| 100 | +instruction = choice [cpy, inc, dec, jnz] |
| 101 | + |
| 102 | +instructions :: Parser [Instruction] |
| 103 | +instructions = sepEndBy instruction newline |
| 104 | + |
| 105 | +-- Main |
| 106 | +main :: IO () |
| 107 | +main = do |
| 108 | + contents <- getContents |
| 109 | + let is = parse instructions "" contents |
| 110 | + print $ execute . defaultContext<$> is |
| 111 | + let regs = setR defaultR (Register 'c') (Value 1) |
| 112 | + print $ execute . (\is -> Context is regs 0) <$> is |
0 commit comments