diff options
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day05.hs | 33 |
1 files changed, 33 insertions, 0 deletions
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 @@ | |||
1 | module Main where | ||
2 | |||
3 | import Text.ParserCombinators.Parsec | ||
4 | import Data.List (sort) | ||
5 | import Control.Monad | ||
6 | |||
7 | data Direction = Upper | Lower | ||
8 | |||
9 | doHalving :: [Direction] -> (Int, Int) -> (Int, Int) | ||
10 | doHalving [] p = p | ||
11 | doHalving (Lower:rest) (l, u) = doHalving rest (l, (u+l) `div` 2) | ||
12 | doHalving (Upper:rest) (l, u) = doHalving rest ((u+l) `div` 2 + 1, u) | ||
13 | |||
14 | findId :: (Int, Int) -> Int | ||
15 | findId (row, col) = row * 8 + col | ||
16 | |||
17 | doValidate :: String -> Int | ||
18 | doValidate s = findId (row, col) | ||
19 | where row = fst $ flip doHalving (0, 127) $ map readDir (take 7 s) | ||
20 | col = fst $ flip doHalving (0, 7) $ map readDir (drop 7 s) | ||
21 | |||
22 | readDir :: Char -> Direction | ||
23 | readDir c | ||
24 | | c `elem` "FL" = Lower | ||
25 | | otherwise = Upper | ||
26 | |||
27 | main :: IO () | ||
28 | main = do | ||
29 | n <- lines <$> readFile "input/05" | ||
30 | let valids = map doValidate n | ||
31 | ans1 = maximum valids | ||
32 | print ans1 | ||
33 | print $ sum [minimum valids .. ans1] - sum valids | ||