Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions 2024_haskell/05Manual.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Main where

import Data.List (isPrefixOf, isSuffixOf)
import Data.List.Split (splitOn)

-- import qualified Data.Map.Strict as Map

main :: IO ()
main = do
input <- readFile "05input"
let [rules, seqsInit] = map lines (splitOn "\n\n" input)
let seqs = map (splitOn ",") seqsInit
let validSeqs = filter (validate rules) seqs
let result = sum $ map getMiddle validSeqs
print result

readInt :: String -> Int
readInt = read

validate :: [String] -> [String] -> Bool
validate rules [] = True
validate rules (x : xs)
| all (anyRuleMatches (rulesForPage x rules)) xs = validate rules xs
| otherwise = False

rulesForPage :: String -> [String] -> [String]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update signature [String]->String->[String]

rulesForPage rules x = filter (x `isPrefixOf`) rules

anyRuleMatches :: [String] -> String -> Bool
anyRuleMatches rules p = any (p `isSuffixOf`) rules

getMiddle :: [String] -> Int
getMiddle seq = readInt (seq !! div (length seq) 2)