This commit is contained in:
Jens Kadenbach
2022-12-05 13:05:21 +01:00
parent 2023211beb
commit 1267be4d00
10 changed files with 1122 additions and 3 deletions

View File

@@ -35,6 +35,10 @@ library
Day3.Part1
Day3.Part2
Day3.Shared
Day4
Day4.Part1
Day4.Part2
Day4.Shared
Lib
other-modules:
Paths_aoc2022
@@ -43,6 +47,7 @@ library
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends:
QuickCheck
, Ranged-sets
, base >=4.7 && <5
, containers
, heredoc
@@ -61,6 +66,7 @@ executable aoc2022-exe
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
QuickCheck
, Ranged-sets
, aoc2022
, base >=4.7 && <5
, containers
@@ -78,12 +84,14 @@ test-suite aoc2022-test
Day1Spec
Day2Spec
Day3Spec
Day4Spec
Paths_aoc2022
hs-source-dirs:
test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
QuickCheck
, Ranged-sets
, aoc2022
, base >=4.7 && <5
, containers

View File

@@ -29,6 +29,7 @@ dependencies:
- containers
- split
- lens
- Ranged-sets
ghc-options:
- -Wall

1000
ressources/day04-input Normal file

File diff suppressed because it is too large Load Diff

14
src/Day4.hs Normal file
View File

@@ -0,0 +1,14 @@
module Day4 (day4) where
import Day4.Part1
import Day4.Part2
day4 :: IO ()
day4 = do
input <- readFile "ressources/day04-input"
putStrLn "Day4"
let score1 = countFullyOverlappingPairs input
putStrLn ("Number of fully overlapping section pairs " ++ show score1)
let score2 = countOverlappingPairs input
putStrLn ("Number of section pairs that overlap at all " ++ show score2)
return ()

16
src/Day4/Part1.hs Normal file
View File

@@ -0,0 +1,16 @@
module Day4.Part1 (
isFullyOverlapping,
countFullyOverlappingPairs
)
where
import Day4.Shared
import Data.Ranged
isFullyOverlapping :: ElfPair -> Bool
isFullyOverlapping (a, b) = a `rangeEncloses` b || b `rangeEncloses` a
countFullyOverlappingPairs :: String -> Int
countFullyOverlappingPairs input = length $ filter isFullyOverlapping pairs
where
pairs = map elfPair $ lines input

16
src/Day4/Part2.hs Normal file
View File

@@ -0,0 +1,16 @@
module Day4.Part2 (
countOverlappingPairs
) where
import Day4.Shared
import Data.Ranged
isOverlapping :: ElfPair -> Bool
isOverlapping (a, b) = a `rangeOverlap` b || b `rangeOverlap` a
countOverlappingPairs :: String -> Int
countOverlappingPairs input = length $ filter isOverlapping pairs
where
pairs = map elfPair $ lines input

29
src/Day4/Shared.hs Normal file
View File

@@ -0,0 +1,29 @@
module Day4.Shared(
section,
elfPair,
(+=+),
Section,
ElfPair
) where
import Data.Ranged
import Control.Lens
type Section = Range Int
type ElfPair = (Section, Section)
(+=+) :: Int -> Int -> Range Int
x +=+ y = Range (BoundaryBelow x) (BoundaryAbove y)
section :: String -> Section
section s = read start +=+ read end
where
(start, rest) = break ('-' ==) s
end = drop 1 rest
elfPair :: String -> ElfPair
elfPair s = over each section (first, drop 1 second)
where
(first, second) = break (',' ==) s

View File

@@ -2,8 +2,8 @@ module Lib
( someFunc
) where
import Day3
import Day4
someFunc :: IO ()
someFunc = day3
someFunc = day4

View File

@@ -26,7 +26,7 @@ inputPart2 = [str|vJrwpWtwJgWrhcsFMMfFFhFp
spec :: Spec
spec =
describe "Day2" $ do
describe "Day3" $ do
describe "Part1" $ do
it "can determine Rucksack compartment contents" $ do
splitContents simpleContents `shouldBe` ("abc", "dec")

35
test/Day4Spec.hs Normal file
View File

@@ -0,0 +1,35 @@
{-# LANGUAGE QuasiQuotes #-}
module Day4Spec (spec) where
import Test.Hspec
import Text.Heredoc
import Day4.Part1
import Day4.Part2
import Day4.Shared
inputPart1 :: String
inputPart1 = [str|2-4,6-8
|2-3,4-5
|5-7,7-9
|2-8,3-7
|6-6,4-6
|2-6,4-8
|]
spec :: Spec
spec =
describe "Day4" $ do
describe "Part 1" $ do
it "parses section" $ do
section "2-4" `shouldBe` 2 +=+ 4
it "parses section pair" $ do
elfPair "2-4,6-8" ` shouldBe` (2 +=+ 4, 6 +=+ 8)
it "finds fully overlapping sections" $ do
isFullyOverlapping (3 +=+ 6, 4 +=+ 6) `shouldBe` True
it "counts fully overlapping pairs" $ do
countFullyOverlappingPairs inputPart1 `shouldBe` 2
describe "Part 2" $ do
it "counts overlapping pairs" $ do
countOverlappingPairs inputPart1 `shouldBe` 4