diff options
-rw-r--r-- | execs/Day05.hs | 21 |
1 files changed, 6 insertions, 15 deletions
diff --git a/execs/Day05.hs b/execs/Day05.hs index c018203..aae8f49 100644 --- a/execs/Day05.hs +++ b/execs/Day05.hs | |||
@@ -1,21 +1,12 @@ | |||
1 | module Main where | 1 | module Main where |
2 | 2 | ||
3 | data Direction = Upper | Lower | 3 | import Data.Char (digitToInt) |
4 | 4 | ||
5 | doHalf :: [Direction] -> (Int, Int) -> Int | 5 | doValidate = toInt . map readBin |
6 | doHalf ls start = fst $ foldl fn start ls | 6 | toInt = foldl (\a x -> a * 2 + digitToInt x) 0 |
7 | where fn (l, u) Lower = (l, (u+l) `div` 2) | 7 | readBin 'F' = '0' |
8 | fn (l, u) Upper = ((u+l) `div` 2 + 1, u) | 8 | readBin 'L' = '0' |
9 | 9 | readBin _ = '1' | |
10 | doValidate :: String -> Int | ||
11 | doValidate s = row * 8 + col | ||
12 | where row = flip doHalf (0, 127) $ map readDir (take 7 s) | ||
13 | col = flip doHalf (0, 7) $ map readDir (drop 7 s) | ||
14 | |||
15 | readDir :: Char -> Direction | ||
16 | readDir c | ||
17 | | c `elem` "FL" = Lower | ||
18 | | otherwise = Upper | ||
19 | 10 | ||
20 | main :: IO () | 11 | main :: IO () |
21 | main = do | 12 | main = do |