Compare commits
No commits in common. "71c6797d8145d03b60d8791430a13b3616a4682b" and "cb83e4247d296dcc424691a43da62d39b0384a40" have entirely different histories.
71c6797d81
...
cb83e4247d
33
day2/Main.hs
33
day2/Main.hs
|
@ -2,8 +2,6 @@ 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)
|
||||||
|
@ -25,28 +23,19 @@ pairUp :: [a] -> [(a, a)]
|
||||||
pairUp (a:b:tl) = (a, b) : pairUp (b:tl)
|
pairUp (a:b:tl) = (a, b) : pairUp (b:tl)
|
||||||
pairUp _ = []
|
pairUp _ = []
|
||||||
|
|
||||||
isSafe :: Report -> Bool
|
allIncOrDec :: Report -> Bool
|
||||||
isSafe (a:b:tl) = a /= b && (==) 0 (
|
allIncOrDec (a:b:tl)
|
||||||
length $ filter (not . (\(l, r) -> let
|
| a /= b = all (uncurry (if a < b then (<) else (>))) $ pairUp $ b:tl
|
||||||
dif = abs (l - r)
|
| otherwise = False
|
||||||
cmp = if a < b then (<) else assert (b < a) (>)
|
allIncOrDec _ = True
|
||||||
in (1 <= dif) && (dif <= 3) && l `cmp` r))
|
|
||||||
$ pairUp
|
|
||||||
$ a:b:tl)
|
|
||||||
isSafe _ = True
|
|
||||||
|
|
||||||
with1Removed :: Report -> [Report]
|
notTooDifferent :: Report -> Bool
|
||||||
with1Removed (h:tl) = tl : map (h :) (with1Removed tl)
|
notTooDifferent (a:b:tl) = let dif = abs (a - b)
|
||||||
with1Removed [] = []
|
in (1 <= dif) && (dif <= 3) && notTooDifferent (b:tl)
|
||||||
|
notTooDifferent _ = True
|
||||||
isKindaSafe :: Report -> Bool
|
|
||||||
isKindaSafe = any isSafe . with1Removed
|
|
||||||
|
|
||||||
part1 :: [Report] -> Int
|
part1 :: [Report] -> Int
|
||||||
part1 = length . filter isSafe
|
part1 = length . filter allIncOrDec . filter notTooDifferent
|
||||||
|
|
||||||
part2 :: [Report] -> Int
|
|
||||||
part2 = length . filter isKindaSafe
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
@ -57,4 +46,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