{-# Language QuasiQuotes, TemplateHaskell #-}
module Main (main) where
import Advent (format, stageTH)
import Data.Foldable (foldMap')
data C = Cforward | Cdown | Cup
stageTH
main :: IO ()
IO ()
main =
do [(C, Int)]
inp <- [format|2021 2 (@C %u%n)*|]
case ((C, Int) -> S) -> [(C, Int)] -> S
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap' (C, Int) -> S
toS [(C, Int)]
inp of
S Int
dx Int
dy1 Int
dy2 ->
do Int -> IO ()
forall a. Show a => a -> IO ()
print (Int
dxInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
dy1)
Int -> IO ()
forall a. Show a => a -> IO ()
print (Int
dxInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
dy2)
toS :: (C, Int) -> S
toS :: (C, Int) -> S
toS (C
c,Int
n) =
case C
c of
C
Cup -> Int -> Int -> Int -> S
S Int
0 (-Int
n) Int
0
C
Cdown -> Int -> Int -> Int -> S
S Int
0 Int
n Int
0
C
Cforward -> Int -> Int -> Int -> S
S Int
n Int
0 Int
0
data S = S !Int !Int !Int
deriving Int -> S -> ShowS
[S] -> ShowS
S -> String
(Int -> S -> ShowS) -> (S -> String) -> ([S] -> ShowS) -> Show S
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> S -> ShowS
showsPrec :: Int -> S -> ShowS
$cshow :: S -> String
show :: S -> String
$cshowList :: [S] -> ShowS
showList :: [S] -> ShowS
Show
instance Monoid S where mempty :: S
mempty = Int -> Int -> Int -> S
S Int
0 Int
0 Int
0
instance Semigroup S where S Int
x1 Int
y1 Int
z1 <> :: S -> S -> S
<> S Int
x2 Int
y2 Int
z2 = Int -> Int -> Int -> S
S (Int
x1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
x2) (Int
y1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
y2) (Int
z1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
z2Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
y1Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
x2)