-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.hs
41 lines (38 loc) · 1.03 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Data.List (isPrefixOf)
import Debug.Trace
import Lib.Directions
import Lib.Inputs
import Lib.Lists
import Lib.Strings
import System.IO
main :: IO ()
main = do
handle <- openFile "input.txt" ReadMode
contents <- hGetContents handle
let linesList = lines contents
let res = fn1 (newPosition 0 0) linesList
print res
let res = fn2 0 (newPosition 0 0) linesList
print res
fn1 :: Position -> [String] -> Int
fn1 pos [] = (row pos) * (col pos)
fn1 pos (x:xs) =
case action of
"forward" -> fn1 (move pos DirRight n) xs
"down" -> fn1 (move pos DirDown n) xs
"up" -> fn1 (move pos DirUp n) xs
where
del = newDelimiter x " "
action = getString del 0
n = getInt del 1
fn2 :: Int -> Position -> [String] -> Int
fn2 _ pos [] = (row pos) * (col pos)
fn2 aim pos (x:xs) =
case action of
"forward" -> fn2 aim (move (move pos DirRight n) DirDown (aim * n)) xs
"down" -> fn2 (aim + n) pos xs
"up" -> fn2 (aim - n) pos xs
where
del = newDelimiter x " "
action = getString del 0
n = getInt del 1