-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2.hs
More file actions
32 lines (26 loc) · 1.03 KB
/
Task2.hs
File metadata and controls
32 lines (26 loc) · 1.03 KB
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
module Task2 where
import System.IO
import Data.List
import Data.List.Split
main :: IO ()
main = do
--handle <- openFile "test1.txt" ReadMode
handle <- openFile "task2.txt" ReadMode
contents <- hGetContents handle
--print $ lines contents
let listOfPairs = map splitNumbers $ lines contents
print $ "Part 1: " ++ (show $ length $ filter isSafe listOfPairs)
print $ "Part 2: " ++ (show $ length $ filter isSafeWithRemoval listOfPairs)
hClose handle
splitNumbers :: String -> [Int]
splitNumbers str = map read splitted where
splitted = splitOn " " str
isSafe :: [Int] -> Bool
isSafe ns = isDiffSafe && (isMonotoneUp || isMonotoneDown) where
pairs = zip ns (tail ns)
isDiffSafe = all (\(a,b) -> a/=b && abs(a-b) <= 3) pairs
isMonotoneUp = all (\(a,b) -> a<b) pairs
isMonotoneDown = all (\(a,b) -> a>b) pairs
isSafeWithRemoval :: [Int] -> Bool
isSafeWithRemoval ns = (isSafe ns) || (any isSafe tryRemovals) where
tryRemovals = map (\i -> take i ns ++ drop (i+1) ns) [0..(length ns - 1)]