Skip to content

Instantly share code, notes, and snippets.

@evinism
Created December 5, 2022 07:51
Show Gist options
  • Save evinism/aba73cbf27e425b18b8a482af372710a to your computer and use it in GitHub Desktop.
Save evinism/aba73cbf27e425b18b8a482af372710a to your computer and use it in GitHub Desktop.
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