diff --git a/13.hs b/13.hs new file mode 100644 index 0000000..1044061 --- /dev/null +++ b/13.hs @@ -0,0 +1,29 @@ +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 <- fmap (map parseLine . lines) $ readFile "13.txt" + print $ severity firewalls + print $ findIndex not $ map (anyCaught firewalls) [0..] \ No newline at end of file diff --git a/13.txt b/13.txt new file mode 100644 index 0000000..4926844 --- /dev/null +++ b/13.txt @@ -0,0 +1,43 @@ +0: 4 +1: 2 +2: 3 +4: 5 +6: 8 +8: 6 +10: 4 +12: 6 +14: 6 +16: 8 +18: 8 +20: 6 +22: 8 +24: 8 +26: 8 +28: 12 +30: 12 +32: 9 +34: 14 +36: 12 +38: 12 +40: 12 +42: 12 +44: 10 +46: 12 +48: 12 +50: 10 +52: 14 +56: 12 +58: 14 +62: 14 +64: 14 +66: 12 +68: 14 +70: 14 +72: 17 +74: 14 +76: 14 +80: 20 +82: 14 +90: 24 +92: 14 +98: 14 \ No newline at end of file