diff options
Diffstat (limited to 'day05/day05.hs')
-rw-r--r-- | day05/day05.hs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/day05/day05.hs b/day05/day05.hs new file mode 100644 index 0000000..9118cd2 --- /dev/null +++ b/day05/day05.hs @@ -0,0 +1,43 @@ +import Data.List +import Data.List.Split + +type Rule = [Int] +type Update = [Int] + +relevant :: Update -> Rule -> Bool +relevant upd (b:a:[]) = (b `elem` upd) && (a `elem` upd) + +abides :: Update -> Rule -> Bool +abides [] _ = False +abides (h:t) r@(a:[]) + | h == a = True + | otherwise = abides t r +abides (h:t) r@(b:a:[]) + | h == b = abides t [a] + | otherwise = abides t r + +middle :: [Int] -> Int +middle arr = arr !! (length arr `div` 2) + +valid :: [Rule] -> Update -> Bool +valid rules update = + all (abides update) $ filter (relevant update) rules + +part11 :: Int -> [Rule] -> [Update] -> Int +part11 acc _ [] = acc +part11 acc rules (h:t) + | valid rules h = part11 (acc + (middle h)) rules t + | otherwise = part11 acc rules t + +part1 :: [Rule] -> [Update] -> Int +part1 rules updates = + part11 0 rules updates + +main = do + file <- readFile "day05.input" + let lns = lines file + rulesin:updatesin:[] = splitOn [""] lns + rules = map (map (read::String->Int)) $ map (splitOn "|") rulesin + updates = map (map (read::String -> Int)) $ map (splitOn ",") updatesin + putStrLn ("Part 1: " ++ (show $ part1 rules updates)) + --putStrLn ("Part 2: " ++ (show $ part2 fst sec)) |