2 changed files with 1238 additions and 0 deletions
@ -0,0 +1,45 @@ |
|||||||
|
module Main where |
||||||
|
|
||||||
|
import System.Environment (getArgs) |
||||||
|
|
||||||
|
main :: IO () |
||||||
|
main = do |
||||||
|
input <- readFile . head =<< getArgs |
||||||
|
putStrLn $ unwords ["Part 1:", show $ part1 input] |
||||||
|
putStrLn $ unwords ["Part 2:", show $ part2 input] |
||||||
|
|
||||||
|
part1 :: String -> Int |
||||||
|
part1 = length . uncurry (filter . isFresh) . parse |
||||||
|
where |
||||||
|
isFresh :: [(Int, Int)] -> Int -> Bool |
||||||
|
isFresh fresh ing = any (contained ing) fresh |
||||||
|
|
||||||
|
contained :: Int -> (Int, Int) -> Bool |
||||||
|
contained a (x, y) = x <= a && a <= y |
||||||
|
|
||||||
|
parse :: String -> ([(Int, Int)], [Int]) |
||||||
|
parse = (\(ranges, ingredients) -> ((map parseRange ranges), map read (drop 1 ingredients))) . span (/= "") . lines |
||||||
|
where |
||||||
|
parseRange :: String -> (Int, Int) |
||||||
|
parseRange s = case span (/= '-') s of |
||||||
|
(start, '-' : end) -> (read start, read end) |
||||||
|
_ -> error $ "parseRange: expected a string on the form \"NUM-NUM\", got \"" <> s <> "\"" |
||||||
|
|
||||||
|
part2 :: String -> Int |
||||||
|
part2 = error "Not implemented" |
||||||
|
|
||||||
|
testInput :: String |
||||||
|
testInput = |
||||||
|
unlines |
||||||
|
[ "3-5", |
||||||
|
"10-14", |
||||||
|
"16-20", |
||||||
|
"12-18", |
||||||
|
"", |
||||||
|
"1", |
||||||
|
"5", |
||||||
|
"8", |
||||||
|
"11", |
||||||
|
"17", |
||||||
|
"32" |
||||||
|
] |
||||||
Loading…
Reference in new issue