aoc2021/hs/02.1.hs
2021-12-03 01:50:49 +07:00

25 lines
536 B
Haskell

import System.IO
main = do
file <- readFile "../input/02"
let (x, y) = foldl step (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 (x, y) (Up n) = (x, y - n)
step (x, y) (Down n) = (x, y + n)
step (x, y) (Forward n) = (x + n, y)
step :: (Int, Int) -> Action -> (Int, Int)