1
0
Fork 0
adventofcode/13.hs

29 lines
788 B
Haskell
Raw Normal View History

2017-12-13 06:59:14 +00:00
import Data.List.Split (splitOn)
import Data.List (findIndex)
type Delay = Int
type Firewall = (Int, Int)
(%) = mod
parseLine :: String -> Firewall
parseLine str =
let depth : range : _ = splitOn ": " str
in (read depth, read range)
caught :: Delay -> Firewall -> Bool
caught delay (depth, range) =
(depth + delay) % (2 * (range - 1)) == 0
severity :: [Firewall] -> Int
severity firewalls =
sum . map (\firewall -> uncurry (*) firewall * fromEnum (caught 0 firewall)) $ firewalls
anyCaught :: [Firewall] -> Delay -> Bool
anyCaught firewalls delay =
or . map (caught delay) $ firewalls
main :: IO ()
main = do
firewalls <- map parseLine . lines <$> readFile "13.txt"
2017-12-13 06:59:14 +00:00
print $ severity firewalls
print $ findIndex not $ map (anyCaught firewalls) [0..]