Created
December 5, 2022 07:51
-
-
Save evinism/aba73cbf27e425b18b8a482af372710a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.Text as T | |
main = do | |
input <- readFile "input.txt" | |
print $ countpredranges oneissubset input | |
print $ countpredranges theyoverlap input | |
type Range = (Int, Int) | |
countpredranges :: (Range -> Range -> Bool) -> String -> Int | |
countpredranges predicate = | |
length . (filter (uncurry predicate)) . (fmap parseline) . lines | |
parseline :: String -> (Range, Range) | |
parseline line = case nums of { | |
((w:x:[]) : (y:z:[]) : []) -> ((w, x), (y, z)); | |
_ -> undefined | |
} where | |
listoflists = fmap (split "-") $ split "," line | |
nums = fmap (fmap read) listoflists | |
oneissubset :: Range -> Range -> Bool | |
oneissubset a b = (acontainsb a b) || (acontainsb b a) | |
acontainsb :: Range -> Range -> Bool | |
acontainsb (w, x) (y, z) = | |
(y >= w) | |
&& (y <= x) | |
&& (z >= w) | |
&& (z <= x) | |
theyoverlap :: Range -> Range -> Bool | |
theyoverlap (w, x) (y, z) = | |
(y >= w) && (y <= x) | |
|| (z >= w) && (z <= x) | |
|| (w >= y) && (w <= z) | |
|| (x >= y) && (x <= z) | |
split :: String -> String -> [String] | |
split delim str = fmap T.unpack $ T.splitOn (T.pack delim) (T.pack str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment