From de19878331f10e04c1f9599a482111bb3e7f2762 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 11 Dec 2017 10:04:19 +0100 Subject: Day 11 --- 2017/11.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2017/11.hs (limited to '2017/11.hs') diff --git a/2017/11.hs b/2017/11.hs new file mode 100644 index 0000000..19fe862 --- /dev/null +++ b/2017/11.hs @@ -0,0 +1,30 @@ +import Control.Monad +import Data.Char +import qualified Data.Map.Strict as Map +import Data.Map.Strict ((!)) + +splitOn :: Eq a => a -> [a] -> [[a]] +splitOn c xs = case break (== c) xs of + (pre, []) -> [pre] + (pre, _ : post) -> pre : splitOn c post + +strip :: String -> String +strip = dropWhile isSpace . reverse . dropWhile isSpace . reverse + +dirmap :: Map.Map String (Int, Int) +dirmap = Map.fromList [("n", (0,2)), ("ne", (1,1)), ("se", (1,-1)), ("s", (0,-2)), ("sw", (-1,-1)), ("nw", (-1,1))] + +add :: (Int, Int) -> (Int, Int) -> (Int, Int) +add (a,b) (c,d) = (a + c, b + d) + +distance :: (Int, Int) -> Int +distance (0,0) = 0 +distance (0,y) = abs y `div` 2 +distance (x,0) = abs x +distance (x,y) = let m = min (abs x) (abs y) in m + distance (x - m * signum x, y - m * signum y) + +main :: IO () +main = do + input <- liftM (map (dirmap !) . splitOn ',' . strip) (readFile "11.in") + print $ distance $ foldl1 add input + print $ maximum $ map distance $ scanl add (0,0) input -- cgit v1.2.3-70-g09d2