Day 4
This commit is contained in:
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
|
||||
) where
|
||||
|
||||
import Day3
|
||||
import Day4
|
||||
|
||||
someFunc :: IO ()
|
||||
someFunc = day3
|
||||
someFunc = day4
|
||||
|
||||
|
||||
Reference in New Issue
Block a user