From b2a0507e6418c7853651e4aa0759752eb67808f2 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 5 Dec 2020 11:04:09 +0530 Subject: add initial day5 solution --- execs/Day05.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 execs/Day05.hs (limited to 'execs') diff --git a/execs/Day05.hs b/execs/Day05.hs new file mode 100644 index 0000000..4251cd6 --- /dev/null +++ b/execs/Day05.hs @@ -0,0 +1,33 @@ +module Main where + +import Text.ParserCombinators.Parsec +import Data.List (sort) +import Control.Monad + +data Direction = Upper | Lower + +doHalving :: [Direction] -> (Int, Int) -> (Int, Int) +doHalving [] p = p +doHalving (Lower:rest) (l, u) = doHalving rest (l, (u+l) `div` 2) +doHalving (Upper:rest) (l, u) = doHalving rest ((u+l) `div` 2 + 1, u) + +findId :: (Int, Int) -> Int +findId (row, col) = row * 8 + col + +doValidate :: String -> Int +doValidate s = findId (row, col) + where row = fst $ flip doHalving (0, 127) $ map readDir (take 7 s) + col = fst $ flip doHalving (0, 7) $ map readDir (drop 7 s) + +readDir :: Char -> Direction +readDir c + | c `elem` "FL" = Lower + | otherwise = Upper + +main :: IO () +main = do + n <- lines <$> readFile "input/05" + let valids = map doValidate n + ans1 = maximum valids + print ans1 + print $ sum [minimum valids .. ans1] - sum valids -- cgit v1.2.3