trunk
HeNine 2 years ago
parent 7058b3ffd2
commit bfcf12a4a5

@ -132,3 +132,29 @@ test-suite day03aoctest
, split , split
ghc-options: -threaded -rtsopts -with-rtsopts=-N ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010 default-language: Haskell2010
-- Day 4
test-suite day04basictest
type: exitcode-stdio-1.0
hs-source-dirs: src/day04/test, src/day04
main-is: Basic.hs
other-modules:
Day4Lib
build-depends: base
, HUnit
, split
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
test-suite day04aoctest
type: exitcode-stdio-1.0
hs-source-dirs: src/day04/test, src/day04
main-is: AoCTest.hs
other-modules:
Day4Lib
build-depends: base
, HUnit
, split
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010

@ -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 ()

@ -17,8 +17,9 @@
# #
# resolver: ./custom-snapshot.yaml # resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml # resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: # resolver:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/2.yaml # url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/2.yaml
resolver: lts-19.33
# User packages to be built. # User packages to be built.
# Various formats can be used as shown in the example below. # Various formats can be used as shown in the example below.
@ -49,7 +50,7 @@ packages:
# extra-package-dbs: [] # extra-package-dbs: []
# Control whether we use the GHC we find on the path # Control whether we use the GHC we find on the path
system-ghc: true # system-ghc: true
# #
# Require a specific version of stack, using version ranges # Require a specific version of stack, using version ranges
# require-stack-version: -any # Default # require-stack-version: -any # Default

@ -6,8 +6,7 @@
packages: [] packages: []
snapshots: snapshots:
- completed: - completed:
sha256: fc39d8afc97531d53d87b10abdef593bce503c0c1e46c2e9a84ebcbc78bf8470 sha256: 6d1532d40621957a25bad5195bfca7938e8a06d923c91bc52aa0f3c41181f2d4
size: 648432 size: 619204
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/2.yaml url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/19/33.yaml
original: original: lts-19.33
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/2.yaml

Loading…
Cancel
Save