aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aoc.cabal2
-rw-r--r--execs/Day05.hs21
2 files changed, 8 insertions, 15 deletions
diff --git a/aoc.cabal b/aoc.cabal
index 7b25a88..a5be71d 100644
--- a/aoc.cabal
+++ b/aoc.cabal
@@ -42,6 +42,6 @@ executable Day04
42 42
43executable Day05 43executable Day05
44 main-is: Day05.hs 44 main-is: Day05.hs
45 build-depends: base, parsec 45 build-depends: base
46 default-language: Haskell2010 46 default-language: Haskell2010
47 hs-source-dirs: execs 47 hs-source-dirs: execs
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