Day 4
This commit is contained in:
@@ -35,6 +35,10 @@ library
|
|||||||
Day3.Part1
|
Day3.Part1
|
||||||
Day3.Part2
|
Day3.Part2
|
||||||
Day3.Shared
|
Day3.Shared
|
||||||
|
Day4
|
||||||
|
Day4.Part1
|
||||||
|
Day4.Part2
|
||||||
|
Day4.Shared
|
||||||
Lib
|
Lib
|
||||||
other-modules:
|
other-modules:
|
||||||
Paths_aoc2022
|
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
|
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
|
||||||
build-depends:
|
build-depends:
|
||||||
QuickCheck
|
QuickCheck
|
||||||
|
, Ranged-sets
|
||||||
, base >=4.7 && <5
|
, base >=4.7 && <5
|
||||||
, containers
|
, containers
|
||||||
, heredoc
|
, 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
|
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:
|
build-depends:
|
||||||
QuickCheck
|
QuickCheck
|
||||||
|
, Ranged-sets
|
||||||
, aoc2022
|
, aoc2022
|
||||||
, base >=4.7 && <5
|
, base >=4.7 && <5
|
||||||
, containers
|
, containers
|
||||||
@@ -78,12 +84,14 @@ test-suite aoc2022-test
|
|||||||
Day1Spec
|
Day1Spec
|
||||||
Day2Spec
|
Day2Spec
|
||||||
Day3Spec
|
Day3Spec
|
||||||
|
Day4Spec
|
||||||
Paths_aoc2022
|
Paths_aoc2022
|
||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
test
|
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
|
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:
|
build-depends:
|
||||||
QuickCheck
|
QuickCheck
|
||||||
|
, Ranged-sets
|
||||||
, aoc2022
|
, aoc2022
|
||||||
, base >=4.7 && <5
|
, base >=4.7 && <5
|
||||||
, containers
|
, containers
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ dependencies:
|
|||||||
- containers
|
- containers
|
||||||
- split
|
- split
|
||||||
- lens
|
- lens
|
||||||
|
- Ranged-sets
|
||||||
|
|
||||||
ghc-options:
|
ghc-options:
|
||||||
- -Wall
|
- -Wall
|
||||||
|
|||||||
1000
ressources/day04-input
Normal file
1000
ressources/day04-input
Normal file
File diff suppressed because it is too large
Load Diff
14
src/Day4.hs
Normal file
14
src/Day4.hs
Normal 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
16
src/Day4/Part1.hs
Normal 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
16
src/Day4/Part2.hs
Normal 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
29
src/Day4/Shared.hs
Normal 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
|
||||||
@@ -2,8 +2,8 @@ module Lib
|
|||||||
( someFunc
|
( someFunc
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Day3
|
import Day4
|
||||||
|
|
||||||
someFunc :: IO ()
|
someFunc :: IO ()
|
||||||
someFunc = day3
|
someFunc = day4
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ inputPart2 = [str|vJrwpWtwJgWrhcsFMMfFFhFp
|
|||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec =
|
spec =
|
||||||
describe "Day2" $ do
|
describe "Day3" $ do
|
||||||
describe "Part1" $ do
|
describe "Part1" $ do
|
||||||
it "can determine Rucksack compartment contents" $ do
|
it "can determine Rucksack compartment contents" $ do
|
||||||
splitContents simpleContents `shouldBe` ("abc", "dec")
|
splitContents simpleContents `shouldBe` ("abc", "dec")
|
||||||
|
|||||||
35
test/Day4Spec.hs
Normal file
35
test/Day4Spec.hs
Normal 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
|
||||||
|
|
||||||
Reference in New Issue
Block a user