this repo has no description
1module Lib
2 ( processMoves
3 , distanceFromZero
4 , furthestPointInJourney
5 ) where
6
7type Pos = (Int, Int)
8
9processMoves :: [String] -> Pos
10processMoves = foldl move (0, 0)
11
12furthestPointInJourney :: [String] -> Pos
13furthestPointInJourney = go (0, 0) (0, 0)
14 where go :: Pos -> Pos -> [String] -> Pos
15 go _ furthest [] = furthest
16 go cur furthest (m:ms) =
17 let next = move cur m
18 newMax = if distanceFromZero furthest < distanceFromZero next
19 then next else furthest
20 in go next newMax ms
21
22move :: Pos -> String -> Pos
23move (x, y) dir = case dir of
24 "nw" -> (x - 1, y)
25 "n" -> (x, y + 1)
26 "ne" -> (x + 1, y + 1)
27 "se" -> (x + 1, y)
28 "s" -> (x, y - 1)
29 "sw" -> (x - 1, y - 1)
30 _ -> (x, y)
31
32distanceFromZero :: Pos -> Int
33distanceFromZero (x, y) = abs x + abs y