25 lines
578 B
Haskell
25 lines
578 B
Haskell
import System.IO
|
|
|
|
main = do
|
|
file <- readFile "../input/02"
|
|
let (a, x, y) = foldl step (0, 0, 0) $ map parse $ lines file
|
|
print (x, y, x * y)
|
|
|
|
parse s =
|
|
case name of
|
|
"up" -> Up n
|
|
"down" -> Down n
|
|
"forward" -> Forward n
|
|
where
|
|
[name, x] = words s
|
|
n = read $ x
|
|
|
|
parse :: String -> Action
|
|
|
|
data Action = Up Int | Down Int | Forward Int
|
|
|
|
step (a, x, y) (Up n) = (a - n, x, y)
|
|
step (a, x, y) (Down n) = (a + n, x, y)
|
|
step (a, x, y) (Forward n) = (a, x + n, y + a * n)
|
|
step :: (Int, Int, Int) -> Action -> (Int, Int, Int)
|