This commit is contained in:
Jonathan Chan 2017-12-12 22:59:14 -08:00
parent 492ccb0a5d
commit c2c01450c9
2 changed files with 72 additions and 0 deletions

29
13.hs Normal file
View File

@ -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..]

43
13.txt Normal file
View File

@ -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