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

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