day 2 haskell
This commit is contained in:
parent
531a880e0f
commit
4a331229a2
24
hs/02.1.hs
Normal file
24
hs/02.1.hs
Normal file
|
@ -0,0 +1,24 @@
|
|||
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)
|
24
hs/02.2.hs
Normal file
24
hs/02.2.hs
Normal file
|
@ -0,0 +1,24 @@
|
|||
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)
|
Loading…
Reference in a new issue