diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-12-06 10:50:02 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-12-06 10:50:02 +0000 |
commit | ea4dfa2548c5aefe45e4f0ce0b7c7fc2e35fb745 (patch) | |
tree | 9fb8e2b26d3979b60e40e7c2e18c51688462d8ad | |
parent | 4f7acf6f0e4c9c87c31f3512d33f65b7d286f5a1 (diff) |
[Day 05] Part 2: Oh yeah this is simple was overthinking it
-rw-r--r-- | day05/day05.hs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/day05/day05.hs b/day05/day05.hs index 9118cd2..5c589e7 100644 --- a/day05/day05.hs +++ b/day05/day05.hs @@ -23,6 +23,12 @@ valid :: [Rule] -> Update -> Bool valid rules update = all (abides update) $ filter (relevant update) rules +apply :: [Rule] -> Int -> Int -> Ordering +apply rules a b + | [a,b] `elem` rules = LT + | [b,a] `elem` rules = GT + | otherwise = EQ + part11 :: Int -> [Rule] -> [Update] -> Int part11 acc _ [] = acc part11 acc rules (h:t) @@ -33,6 +39,20 @@ part1 :: [Rule] -> [Update] -> Int part1 rules updates = part11 0 rules updates +part21 :: Int -> [Rule] -> [Update] -> Int +part21 acc _ [] = acc +part21 acc rules (h:t) + | valid rules h = part21 acc rules t + | otherwise = + let rrules = filter (relevant h) rules + sfun = apply rrules + inc = middle $ sortBy (sfun) h in + part21 (acc + inc) rules t + +part2 :: [Rule] -> [Update] -> Int +part2 rules updates = + part21 0 rules updates + main = do file <- readFile "day05.input" let lns = lines file @@ -40,4 +60,4 @@ main = do 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)) + putStrLn ("Part 2: " ++ (show $ part2 rules updates)) |