day 4
parent
7058b3ffd2
commit
bfcf12a4a5
@ -0,0 +1,6 @@
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,49 @@
|
||||
module Day4Lib
|
||||
( Range (..),
|
||||
parseRange,
|
||||
parseRow,
|
||||
--
|
||||
processInput1,
|
||||
processInput2,
|
||||
)
|
||||
where
|
||||
|
||||
import Data.List.Split (splitOn)
|
||||
|
||||
-- import Debug.Trace ()
|
||||
|
||||
newtype Range = Range (Int, Int) deriving (Show, Eq)
|
||||
|
||||
parseRange :: String -> Range
|
||||
parseRange rs = let [s, e] = map read (splitOn "-" rs) in Range (s, e)
|
||||
|
||||
parseRow :: String -> [Range]
|
||||
parseRow = map parseRange . splitOn ","
|
||||
|
||||
isInside :: Range -> Range -> Bool
|
||||
isInside (Range (s1, e1)) (Range (s2, e2)) = s1 >= s2 && e1 <= e2
|
||||
|
||||
isOverlapping :: Range -> Range -> Bool
|
||||
isOverlapping r1@(Range (s1, e1)) r2@(Range (s2, e2)) =
|
||||
Range (s1, s1)
|
||||
`isInside` r2
|
||||
|| Range (e1, e1)
|
||||
`isInside` r2
|
||||
|| Range (s2, s2)
|
||||
`isInside` r1
|
||||
|| Range (e2, e2)
|
||||
`isInside` r1
|
||||
|
||||
-- ####################
|
||||
|
||||
processInput1 :: String -> Integer
|
||||
processInput1 = sum . map processRow1 . lines
|
||||
|
||||
processRow1 :: String -> Integer
|
||||
processRow1 = (\[r1, r2] -> if r1 `isInside` r2 || r2 `isInside` r1 then 1 else 0) . parseRow
|
||||
|
||||
processInput2 :: String -> Integer
|
||||
processInput2 = sum . map processRow2 . lines
|
||||
|
||||
processRow2 :: String -> Integer
|
||||
processRow2 = (\[r1, r2] -> if r1 `isOverlapping` r2 then 1 else 0) . parseRow
|
@ -0,0 +1,41 @@
|
||||
import Day4Lib (processInput1, processInput2)
|
||||
import System.IO
|
||||
import Test.HUnit
|
||||
|
||||
testCases1 =
|
||||
[ ("data/input040.txt", 2),
|
||||
("data/input041.txt", 444)
|
||||
]
|
||||
|
||||
testCase1 (file, result) = do
|
||||
withFile
|
||||
file
|
||||
ReadMode
|
||||
( \handle -> do
|
||||
contents <- hGetContents handle
|
||||
assertEqual "input test" result $ processInput1 contents
|
||||
)
|
||||
|
||||
testCases2 =
|
||||
[ ("data/input040.txt", 4),
|
||||
("data/input041.txt", 801)
|
||||
]
|
||||
|
||||
testCase2 (file, result) = do
|
||||
withFile
|
||||
file
|
||||
ReadMode
|
||||
( \handle -> do
|
||||
contents <- hGetContents handle
|
||||
assertEqual "input test" result $ processInput2 contents
|
||||
)
|
||||
|
||||
tests =
|
||||
TestList $
|
||||
[TestCase (testCase1 c) | c <- testCases1]
|
||||
++ [TestCase (testCase2 c) | c <- testCases2]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
runTestTT tests
|
||||
return ()
|
@ -0,0 +1,17 @@
|
||||
import Day4Lib
|
||||
import Test.HUnit
|
||||
|
||||
trange = TestCase (assertEqual "1-2" (Range (1,2)) $ parseRange "1-2")
|
||||
trangel = TestCase (assertEqual "131231-215562" (Range (131231,215562)) $ parseRange "1-2")
|
||||
|
||||
trow = TestCase (assertEqual "1-2,3-4" [Range (1,2), Range(3,4)] $ parseRow "1-2,3-4")
|
||||
trowl = TestCase (assertEqual "1-2,3-4" [Range (1,2), Range(34524,62345)] $ parseRow "1-2,34524-62345")
|
||||
|
||||
tests =
|
||||
TestList $
|
||||
[trange, trow]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
runTestTT tests
|
||||
return ()
|
Loading…
Reference in New Issue