{-# Language DeriveTraversable, Safe #-}
module Intcode.Opcode
(
Opcode(..), Mode(..),
decode,
) where
data Mode
= Abs
| Imm
| Rel
deriving (Mode -> Mode -> Bool
(Mode -> Mode -> Bool) -> (Mode -> Mode -> Bool) -> Eq Mode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Mode -> Mode -> Bool
== :: Mode -> Mode -> Bool
$c/= :: Mode -> Mode -> Bool
/= :: Mode -> Mode -> Bool
Eq, Eq Mode
Eq Mode =>
(Mode -> Mode -> Ordering)
-> (Mode -> Mode -> Bool)
-> (Mode -> Mode -> Bool)
-> (Mode -> Mode -> Bool)
-> (Mode -> Mode -> Bool)
-> (Mode -> Mode -> Mode)
-> (Mode -> Mode -> Mode)
-> Ord Mode
Mode -> Mode -> Bool
Mode -> Mode -> Ordering
Mode -> Mode -> Mode
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Mode -> Mode -> Ordering
compare :: Mode -> Mode -> Ordering
$c< :: Mode -> Mode -> Bool
< :: Mode -> Mode -> Bool
$c<= :: Mode -> Mode -> Bool
<= :: Mode -> Mode -> Bool
$c> :: Mode -> Mode -> Bool
> :: Mode -> Mode -> Bool
$c>= :: Mode -> Mode -> Bool
>= :: Mode -> Mode -> Bool
$cmax :: Mode -> Mode -> Mode
max :: Mode -> Mode -> Mode
$cmin :: Mode -> Mode -> Mode
min :: Mode -> Mode -> Mode
Ord, ReadPrec [Mode]
ReadPrec Mode
Int -> ReadS Mode
ReadS [Mode]
(Int -> ReadS Mode)
-> ReadS [Mode] -> ReadPrec Mode -> ReadPrec [Mode] -> Read Mode
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Mode
readsPrec :: Int -> ReadS Mode
$creadList :: ReadS [Mode]
readList :: ReadS [Mode]
$creadPrec :: ReadPrec Mode
readPrec :: ReadPrec Mode
$creadListPrec :: ReadPrec [Mode]
readListPrec :: ReadPrec [Mode]
Read, Int -> Mode -> ShowS
[Mode] -> ShowS
Mode -> String
(Int -> Mode -> ShowS)
-> (Mode -> String) -> ([Mode] -> ShowS) -> Show Mode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Mode -> ShowS
showsPrec :: Int -> Mode -> ShowS
$cshow :: Mode -> String
show :: Mode -> String
$cshowList :: [Mode] -> ShowS
showList :: [Mode] -> ShowS
Show)
data Opcode a
= Add !a !a !a
| Mul !a !a !a
| Inp !a
| Out !a
| Jnz !a !a
| Jz !a !a
| Lt !a !a !a
| Eq !a !a !a
| Arb !a
| Hlt
deriving (Opcode a -> Opcode a -> Bool
(Opcode a -> Opcode a -> Bool)
-> (Opcode a -> Opcode a -> Bool) -> Eq (Opcode a)
forall a. Eq a => Opcode a -> Opcode a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Opcode a -> Opcode a -> Bool
== :: Opcode a -> Opcode a -> Bool
$c/= :: forall a. Eq a => Opcode a -> Opcode a -> Bool
/= :: Opcode a -> Opcode a -> Bool
Eq, Eq (Opcode a)
Eq (Opcode a) =>
(Opcode a -> Opcode a -> Ordering)
-> (Opcode a -> Opcode a -> Bool)
-> (Opcode a -> Opcode a -> Bool)
-> (Opcode a -> Opcode a -> Bool)
-> (Opcode a -> Opcode a -> Bool)
-> (Opcode a -> Opcode a -> Opcode a)
-> (Opcode a -> Opcode a -> Opcode a)
-> Ord (Opcode a)
Opcode a -> Opcode a -> Bool
Opcode a -> Opcode a -> Ordering
Opcode a -> Opcode a -> Opcode a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Opcode a)
forall a. Ord a => Opcode a -> Opcode a -> Bool
forall a. Ord a => Opcode a -> Opcode a -> Ordering
forall a. Ord a => Opcode a -> Opcode a -> Opcode a
$ccompare :: forall a. Ord a => Opcode a -> Opcode a -> Ordering
compare :: Opcode a -> Opcode a -> Ordering
$c< :: forall a. Ord a => Opcode a -> Opcode a -> Bool
< :: Opcode a -> Opcode a -> Bool
$c<= :: forall a. Ord a => Opcode a -> Opcode a -> Bool
<= :: Opcode a -> Opcode a -> Bool
$c> :: forall a. Ord a => Opcode a -> Opcode a -> Bool
> :: Opcode a -> Opcode a -> Bool
$c>= :: forall a. Ord a => Opcode a -> Opcode a -> Bool
>= :: Opcode a -> Opcode a -> Bool
$cmax :: forall a. Ord a => Opcode a -> Opcode a -> Opcode a
max :: Opcode a -> Opcode a -> Opcode a
$cmin :: forall a. Ord a => Opcode a -> Opcode a -> Opcode a
min :: Opcode a -> Opcode a -> Opcode a
Ord, ReadPrec [Opcode a]
ReadPrec (Opcode a)
Int -> ReadS (Opcode a)
ReadS [Opcode a]
(Int -> ReadS (Opcode a))
-> ReadS [Opcode a]
-> ReadPrec (Opcode a)
-> ReadPrec [Opcode a]
-> Read (Opcode a)
forall a. Read a => ReadPrec [Opcode a]
forall a. Read a => ReadPrec (Opcode a)
forall a. Read a => Int -> ReadS (Opcode a)
forall a. Read a => ReadS [Opcode a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (Opcode a)
readsPrec :: Int -> ReadS (Opcode a)
$creadList :: forall a. Read a => ReadS [Opcode a]
readList :: ReadS [Opcode a]
$creadPrec :: forall a. Read a => ReadPrec (Opcode a)
readPrec :: ReadPrec (Opcode a)
$creadListPrec :: forall a. Read a => ReadPrec [Opcode a]
readListPrec :: ReadPrec [Opcode a]
Read, Int -> Opcode a -> ShowS
[Opcode a] -> ShowS
Opcode a -> String
(Int -> Opcode a -> ShowS)
-> (Opcode a -> String) -> ([Opcode a] -> ShowS) -> Show (Opcode a)
forall a. Show a => Int -> Opcode a -> ShowS
forall a. Show a => [Opcode a] -> ShowS
forall a. Show a => Opcode a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Opcode a -> ShowS
showsPrec :: Int -> Opcode a -> ShowS
$cshow :: forall a. Show a => Opcode a -> String
show :: Opcode a -> String
$cshowList :: forall a. Show a => [Opcode a] -> ShowS
showList :: [Opcode a] -> ShowS
Show, (forall a b. (a -> b) -> Opcode a -> Opcode b)
-> (forall a b. a -> Opcode b -> Opcode a) -> Functor Opcode
forall a b. a -> Opcode b -> Opcode a
forall a b. (a -> b) -> Opcode a -> Opcode b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Opcode a -> Opcode b
fmap :: forall a b. (a -> b) -> Opcode a -> Opcode b
$c<$ :: forall a b. a -> Opcode b -> Opcode a
<$ :: forall a b. a -> Opcode b -> Opcode a
Functor, (forall m. Monoid m => Opcode m -> m)
-> (forall m a. Monoid m => (a -> m) -> Opcode a -> m)
-> (forall m a. Monoid m => (a -> m) -> Opcode a -> m)
-> (forall a b. (a -> b -> b) -> b -> Opcode a -> b)
-> (forall a b. (a -> b -> b) -> b -> Opcode a -> b)
-> (forall b a. (b -> a -> b) -> b -> Opcode a -> b)
-> (forall b a. (b -> a -> b) -> b -> Opcode a -> b)
-> (forall a. (a -> a -> a) -> Opcode a -> a)
-> (forall a. (a -> a -> a) -> Opcode a -> a)
-> (forall a. Opcode a -> [a])
-> (forall a. Opcode a -> Bool)
-> (forall a. Opcode a -> Int)
-> (forall a. Eq a => a -> Opcode a -> Bool)
-> (forall a. Ord a => Opcode a -> a)
-> (forall a. Ord a => Opcode a -> a)
-> (forall a. Num a => Opcode a -> a)
-> (forall a. Num a => Opcode a -> a)
-> Foldable Opcode
forall a. Eq a => a -> Opcode a -> Bool
forall a. Num a => Opcode a -> a
forall a. Ord a => Opcode a -> a
forall m. Monoid m => Opcode m -> m
forall a. Opcode a -> Bool
forall a. Opcode a -> Int
forall a. Opcode a -> [a]
forall a. (a -> a -> a) -> Opcode a -> a
forall m a. Monoid m => (a -> m) -> Opcode a -> m
forall b a. (b -> a -> b) -> b -> Opcode a -> b
forall a b. (a -> b -> b) -> b -> Opcode a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => Opcode m -> m
fold :: forall m. Monoid m => Opcode m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Opcode a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Opcode a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Opcode a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Opcode a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Opcode a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Opcode a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Opcode a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Opcode a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Opcode a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Opcode a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Opcode a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Opcode a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Opcode a -> a
foldr1 :: forall a. (a -> a -> a) -> Opcode a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Opcode a -> a
foldl1 :: forall a. (a -> a -> a) -> Opcode a -> a
$ctoList :: forall a. Opcode a -> [a]
toList :: forall a. Opcode a -> [a]
$cnull :: forall a. Opcode a -> Bool
null :: forall a. Opcode a -> Bool
$clength :: forall a. Opcode a -> Int
length :: forall a. Opcode a -> Int
$celem :: forall a. Eq a => a -> Opcode a -> Bool
elem :: forall a. Eq a => a -> Opcode a -> Bool
$cmaximum :: forall a. Ord a => Opcode a -> a
maximum :: forall a. Ord a => Opcode a -> a
$cminimum :: forall a. Ord a => Opcode a -> a
minimum :: forall a. Ord a => Opcode a -> a
$csum :: forall a. Num a => Opcode a -> a
sum :: forall a. Num a => Opcode a -> a
$cproduct :: forall a. Num a => Opcode a -> a
product :: forall a. Num a => Opcode a -> a
Foldable)
decode :: Int -> Maybe (Opcode Mode)
decode :: Int -> Maybe (Opcode Mode)
decode Int
n =
case Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
100 of
Int
1 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Int -> Int -> Opcode Int
forall a. a -> a -> a -> Opcode a
Add Int
1 Int
2 Int
3)
Int
2 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Int -> Int -> Opcode Int
forall a. a -> a -> a -> Opcode a
Mul Int
1 Int
2 Int
3)
Int
3 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Opcode Int
forall a. a -> Opcode a
Inp Int
1 )
Int
4 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Opcode Int
forall a. a -> Opcode a
Out Int
1 )
Int
5 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Int -> Opcode Int
forall a. a -> a -> Opcode a
Jnz Int
1 Int
2 )
Int
6 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Int -> Opcode Int
forall a. a -> a -> Opcode a
Jz Int
1 Int
2 )
Int
7 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Int -> Int -> Opcode Int
forall a. a -> a -> a -> Opcode a
Lt Int
1 Int
2 Int
3)
Int
8 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Int -> Int -> Opcode Int
forall a. a -> a -> a -> Opcode a
Eq Int
1 Int
2 Int
3)
Int
9 -> Opcode Int -> Maybe (Opcode Mode)
fill (Int -> Opcode Int
forall a. a -> Opcode a
Arb Int
1 )
Int
99 -> Opcode Int -> Maybe (Opcode Mode)
fill Opcode Int
forall a. Opcode a
Hlt
Int
_ -> Maybe (Opcode Mode)
forall a. Maybe a
Nothing
where
fill :: Opcode Int -> Maybe (Opcode Mode)
fill = (Int -> Maybe Mode) -> Opcode Int -> Maybe (Opcode Mode)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Opcode a -> f (Opcode b)
traverse (Int -> Int -> Maybe Mode
parameter Int
n)
{-# INLINABLE decode #-}
parameter ::
Int ->
Int ->
Maybe Mode
parameter :: Int -> Int -> Maybe Mode
parameter Int
n Int
i =
case Int -> Int -> Int
digit (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
n of
Int
0 -> Mode -> Maybe Mode
forall a. a -> Maybe a
Just Mode
Abs
Int
1 -> Mode -> Maybe Mode
forall a. a -> Maybe a
Just Mode
Imm
Int
2 -> Mode -> Maybe Mode
forall a. a -> Maybe a
Just Mode
Rel
Int
_ -> Maybe Mode
forall a. Maybe a
Nothing
instance Traversable Opcode where
{-# INLINE traverse #-}
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Opcode a -> f (Opcode b)
traverse a -> f b
f Opcode a
o =
case Opcode a
o of
Add a
x a
y a
z -> b -> b -> b -> Opcode b
forall a. a -> a -> a -> Opcode a
Add (b -> b -> b -> Opcode b) -> f b -> f (b -> b -> Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (b -> b -> Opcode b) -> f b -> f (b -> Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
y f (b -> Opcode b) -> f b -> f (Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
z
Mul a
x a
y a
z -> b -> b -> b -> Opcode b
forall a. a -> a -> a -> Opcode a
Mul (b -> b -> b -> Opcode b) -> f b -> f (b -> b -> Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (b -> b -> Opcode b) -> f b -> f (b -> Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
y f (b -> Opcode b) -> f b -> f (Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
z
Inp a
x -> b -> Opcode b
forall a. a -> Opcode a
Inp (b -> Opcode b) -> f b -> f (Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
Out a
x -> b -> Opcode b
forall a. a -> Opcode a
Out (b -> Opcode b) -> f b -> f (Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
Jnz a
x a
y -> b -> b -> Opcode b
forall a. a -> a -> Opcode a
Jnz (b -> b -> Opcode b) -> f b -> f (b -> Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (b -> Opcode b) -> f b -> f (Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
y
Jz a
x a
y -> b -> b -> Opcode b
forall a. a -> a -> Opcode a
Jz (b -> b -> Opcode b) -> f b -> f (b -> Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (b -> Opcode b) -> f b -> f (Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
y
Lt a
x a
y a
z -> b -> b -> b -> Opcode b
forall a. a -> a -> a -> Opcode a
Lt (b -> b -> b -> Opcode b) -> f b -> f (b -> b -> Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (b -> b -> Opcode b) -> f b -> f (b -> Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
y f (b -> Opcode b) -> f b -> f (Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
z
Eq a
x a
y a
z -> b -> b -> b -> Opcode b
forall a. a -> a -> a -> Opcode a
Eq (b -> b -> b -> Opcode b) -> f b -> f (b -> b -> Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (b -> b -> Opcode b) -> f b -> f (b -> Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
y f (b -> Opcode b) -> f b -> f (Opcode b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
z
Arb a
x -> b -> Opcode b
forall a. a -> Opcode a
Arb (b -> Opcode b) -> f b -> f (Opcode b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
Opcode a
Hlt -> Opcode b -> f (Opcode b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Opcode b
forall a. Opcode a
Hlt
digit :: Int -> Int -> Int
digit :: Int -> Int -> Int
digit Int
i Int
x = Int
x Int -> Int -> Int
forall a. Integral a => a -> a -> a
`quot` (Int
10Int -> Int -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^Int
i) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
10