blob: b263574c4a930d210ea47adc3c0d703ad0622ce6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import Data.List
import Data.List.Split
import Data.Maybe
data Dir = Up | Down
safe :: [Int] -> Int -> Dir -> Bool
safe [] _ _ =
True
safe (x:xs) last Up
| x - last < 1 = False
| x - last > 3 = False
| otherwise = safe xs x Up
safe (x:xs) last Down
| last - x < 1 = False
| last - x > 3 = False
| otherwise = safe xs x Down
isSafe (x:y:rest)
| y > x = safe (y:rest) x Up
| x > y = safe (y:rest) x Down
| otherwise = False
deleteAt x list =
let (h, t) = splitAt x list in
h ++ (drop 1 t)
isSafeDamped Nothing rep
| isSafe rep = True
| otherwise = isSafeDamped (Just 0) rep
isSafeDamped (Just x) rep
| x >= length rep = False
| isSafe $ deleteAt x rep = True
| otherwise = isSafeDamped (Just (x + 1)) rep
part1 reps =
length $ filter (isSafe) reps
part2 reps =
length $ filter (isSafeDamped Nothing) reps
main = do
file <- readFile "day02.input"
let lns = map (splitOn " ") $ lines file
let reps = map (map (read::String->Int)) $ lns
putStrLn ("Part 1: " ++ (show $ part1 reps))
putStrLn ("Part 2: " ++ (show $ part2 reps))
|