aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-05 05:48:34 +0000
committerAkshay <[email protected]>2020-12-05 05:48:34 +0000
commit4c0b08c857afb6f308e01c19e5df68b994617311 (patch)
tree84c840220268b7f2717677122dd8a4fc80fd7757 /execs
parentb2a0507e6418c7853651e4aa0759752eb67808f2 (diff)
tidy up
Diffstat (limited to 'execs')
-rw-r--r--execs/Day05.hs21
1 files changed, 7 insertions, 14 deletions
diff --git a/execs/Day05.hs b/execs/Day05.hs
index 4251cd6..c018203 100644
--- a/execs/Day05.hs
+++ b/execs/Day05.hs
@@ -1,23 +1,16 @@
1module Main where 1module Main where
2 2
3import Text.ParserCombinators.Parsec
4import Data.List (sort)
5import Control.Monad
6
7data Direction = Upper | Lower 3data Direction = Upper | Lower
8 4
9doHalving :: [Direction] -> (Int, Int) -> (Int, Int) 5doHalf :: [Direction] -> (Int, Int) -> Int
10doHalving [] p = p 6doHalf ls start = fst $ foldl fn start ls
11doHalving (Lower:rest) (l, u) = doHalving rest (l, (u+l) `div` 2) 7 where fn (l, u) Lower = (l, (u+l) `div` 2)
12doHalving (Upper:rest) (l, u) = doHalving rest ((u+l) `div` 2 + 1, u) 8 fn (l, u) Upper = ((u+l) `div` 2 + 1, u)
13
14findId :: (Int, Int) -> Int
15findId (row, col) = row * 8 + col
16 9
17doValidate :: String -> Int 10doValidate :: String -> Int
18doValidate s = findId (row, col) 11doValidate s = row * 8 + col
19 where row = fst $ flip doHalving (0, 127) $ map readDir (take 7 s) 12 where row = flip doHalf (0, 127) $ map readDir (take 7 s)
20 col = fst $ flip doHalving (0, 7) $ map readDir (drop 7 s) 13 col = flip doHalf (0, 7) $ map readDir (drop 7 s)
21 14
22readDir :: Char -> Direction 15readDir :: Char -> Direction
23readDir c 16readDir c