aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-05 05:34:09 +0000
committerAkshay <[email protected]>2020-12-05 05:34:09 +0000
commitb2a0507e6418c7853651e4aa0759752eb67808f2 (patch)
treecb28a4905e83fef2ae5b343c7b1ddea6a4230710 /execs
parent33549e923debfa5c586e2b27938ff9574c9de5bf (diff)
add initial day5 solution
Diffstat (limited to 'execs')
-rw-r--r--execs/Day05.hs33
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 @@
1module Main where
2
3import Text.ParserCombinators.Parsec
4import Data.List (sort)
5import Control.Monad
6
7data Direction = Upper | Lower
8
9doHalving :: [Direction] -> (Int, Int) -> (Int, Int)
10doHalving [] p = p
11doHalving (Lower:rest) (l, u) = doHalving rest (l, (u+l) `div` 2)
12doHalving (Upper:rest) (l, u) = doHalving rest ((u+l) `div` 2 + 1, u)
13
14findId :: (Int, Int) -> Int
15findId (row, col) = row * 8 + col
16
17doValidate :: String -> Int
18doValidate 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
22readDir :: Char -> Direction
23readDir c
24 | c `elem` "FL" = Lower
25 | otherwise = Upper
26
27main :: IO ()
28main = 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