Compare commits
2 commits
cb83e4247d
...
71c6797d81
Author | SHA1 | Date | |
---|---|---|---|
kale | 71c6797d81 | ||
kale | 45c9a200e2 |
33
day2/Main.hs
33
day2/Main.hs
|
@ -2,6 +2,8 @@ module Main where
|
||||||
|
|
||||||
import System.Environment (getArgs)
|
import System.Environment (getArgs)
|
||||||
|
|
||||||
|
import Control.Exception (assert)
|
||||||
|
|
||||||
import Text.Parsec (parse, many1, char, sepBy, endBy)
|
import Text.Parsec (parse, many1, char, sepBy, endBy)
|
||||||
import Text.Parsec.Char (endOfLine)
|
import Text.Parsec.Char (endOfLine)
|
||||||
import Text.ParserCombinators.Parsec (GenParser)
|
import Text.ParserCombinators.Parsec (GenParser)
|
||||||
|
@ -23,19 +25,28 @@ pairUp :: [a] -> [(a, a)]
|
||||||
pairUp (a:b:tl) = (a, b) : pairUp (b:tl)
|
pairUp (a:b:tl) = (a, b) : pairUp (b:tl)
|
||||||
pairUp _ = []
|
pairUp _ = []
|
||||||
|
|
||||||
allIncOrDec :: Report -> Bool
|
isSafe :: Report -> Bool
|
||||||
allIncOrDec (a:b:tl)
|
isSafe (a:b:tl) = a /= b && (==) 0 (
|
||||||
| a /= b = all (uncurry (if a < b then (<) else (>))) $ pairUp $ b:tl
|
length $ filter (not . (\(l, r) -> let
|
||||||
| otherwise = False
|
dif = abs (l - r)
|
||||||
allIncOrDec _ = True
|
cmp = if a < b then (<) else assert (b < a) (>)
|
||||||
|
in (1 <= dif) && (dif <= 3) && l `cmp` r))
|
||||||
|
$ pairUp
|
||||||
|
$ a:b:tl)
|
||||||
|
isSafe _ = True
|
||||||
|
|
||||||
notTooDifferent :: Report -> Bool
|
with1Removed :: Report -> [Report]
|
||||||
notTooDifferent (a:b:tl) = let dif = abs (a - b)
|
with1Removed (h:tl) = tl : map (h :) (with1Removed tl)
|
||||||
in (1 <= dif) && (dif <= 3) && notTooDifferent (b:tl)
|
with1Removed [] = []
|
||||||
notTooDifferent _ = True
|
|
||||||
|
isKindaSafe :: Report -> Bool
|
||||||
|
isKindaSafe = any isSafe . with1Removed
|
||||||
|
|
||||||
part1 :: [Report] -> Int
|
part1 :: [Report] -> Int
|
||||||
part1 = length . filter allIncOrDec . filter notTooDifferent
|
part1 = length . filter isSafe
|
||||||
|
|
||||||
|
part2 :: [Report] -> Int
|
||||||
|
part2 = length . filter isKindaSafe
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
@ -46,4 +57,4 @@ main = do
|
||||||
Left e -> putStrLn ("Input file is incorrectly formatted : " ++ show e)
|
Left e -> putStrLn ("Input file is incorrectly formatted : " ++ show e)
|
||||||
Right r -> do
|
Right r -> do
|
||||||
putStrLn ("Solution (Part 1) : " ++ show (part1 r))
|
putStrLn ("Solution (Part 1) : " ++ show (part1 r))
|
||||||
-- putStrLn ("Solution (Part 2) : " ++ show (part2 r))
|
putStrLn ("Solution (Part 2) : " ++ show (part2 r))
|
||||||
|
|
Loading…
Reference in a new issue