blob: 34c076304360c73b09ba5ef67d844c444df44933 (
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
48
49
50
51
|
module Day2
import Data.String
parseRow : String -> List Int
parseRow str = do
num <- words str
maybe [] pure (parseInteger num)
parseInp : String -> List (List Int)
parseInp inp = do
numrow <- lines inp
pure $ parseRow numrow
isSafe : List Int -> Bool
isSafe [] = False
isSafe (x :: xs) = fst up || fst down where
up = foldl (\(b, y) => \n => (b && y - n <= 3 && y - n > 0, n)) (True, x) xs
down = foldl (\(b, y) => \n => (b && n - y <= 3 && n - y > 0, n)) (True, x) xs
btoi : Bool -> Int
btoi False = 0
btoi True = 1
part1 : List (List Int) -> Int
part1 iss = sum $ map (btoi . isSafe) iss
export sol1 : String -> String
sol1 = show . part1 . parseInp
joinLists : (List a, List a) -> List a
joinLists (x, []) = []
joinLists (x, (y :: xs)) = x ++ xs
subLists : List a -> List (List a)
subLists xs = do
splitNum <- [0.. (length xs)]
pure $ joinLists $ splitAt splitNum xs
isSafe' : List Int -> Bool
isSafe' xs = isSafe xs || (any isSafe $ subLists xs)
part2 : List (List Int) -> Int
part2 iss = sum $ map (btoi . isSafe') iss
export sol2 : String -> String
sol2 = show . part2 . parseInp
export out : String -> String
out = show . parseInp
|