From: Neil Smith Date: Sun, 4 Dec 2022 09:11:03 +0000 (+0000) Subject: Done day 4 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=b8fa5f9013026264b0509f0498f4b897c9b4c824;p=advent-of-code-22.git Done day 4 --- diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index f310294..a374b42 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -104,3 +104,8 @@ executable advent03 import: common-extensions, build-directives main-is: advent03/Main.hs build-depends: containers, split + +executable advent04 + import: common-extensions, build-directives + main-is: advent04/Main.hs + build-depends: text, attoparsec \ No newline at end of file diff --git a/advent04/Main.hs b/advent04/Main.hs new file mode 100644 index 0000000..456b157 --- /dev/null +++ b/advent04/Main.hs @@ -0,0 +1,66 @@ +-- Writeup at https://work.njae.me.uk/2022/12/04/advent-of-code-2022-day-4/ + +import System.Environment +import Data.Text (Text) +import qualified Data.Text.IO as TIO +import Data.Attoparsec.Text hiding (Result) +-- import Control.Applicative + +data Assignment = Assignment Int Int deriving (Show, Eq) +type Pair = (Assignment, Assignment) + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- TIO.readFile dataFileName + let pairs = successfulParse text + print $ part1 pairs + print $ part2 pairs + +getDataFileName :: IO String +getDataFileName = + do args <- getArgs + progName <- getProgName + let baseDataName = if null args + then progName + else head args + let dataFileName = "data/" ++ baseDataName ++ ".txt" + return dataFileName + +part1 :: [Pair] -> Int +part1 = length . (filter hasContainment) + +part2 :: [Pair] -> Int +part2 = length . (filter overlaps) + +hasContainment, disjoint, overlaps :: Pair -> Bool +hasContainment (assignment1, assignment2) = + (assignment1 `contains` assignment2) || (assignment2 `contains` assignment1) + +disjoint (assignment1, assignment2) = + (assignment1 `before` assignment2) || (assignment2 `before` assignment1) + +overlaps pair = not $ disjoint pair + +contains, before :: Assignment -> Assignment -> Bool +contains (Assignment lower1 upper1) (Assignment lower2 upper2) = + (lower1 <= lower2) && (upper1 >= upper2) + +before (Assignment _lower1 upper1) (Assignment lower2 _upper2) = (upper1 < lower2) + +-- Parse the input file + +pairsP :: Parser [Pair] +pairP :: Parser Pair +assignmentP :: Parser Assignment + +pairsP = pairP `sepBy` endOfLine +pairP = (,) <$> assignmentP <* "," <*> assignmentP + +assignmentP = Assignment <$> decimal <* "-" <*> decimal + +successfulParse :: Text -> [Pair] +successfulParse input = + case parseOnly pairsP input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right pairs -> pairs diff --git a/data/advent04.txt b/data/advent04.txt new file mode 100644 index 0000000..1464d66 --- /dev/null +++ b/data/advent04.txt @@ -0,0 +1,1000 @@ +28-88,85-88 +9-70,18-18 +76-90,53-89 +17-86,11-17 +8-8,8-82 +16-85,15-95 +20-76,19-20 +56-57,57-66 +95-99,1-96 +14-17,17-67 +95-99,32-98 +30-99,29-98 +12-95,11-94 +3-82,1-72 +4-88,88-88 +2-10,11-59 +8-16,8-16 +22-95,23-94 +46-51,78-99 +61-99,62-94 +30-72,26-73 +6-93,2-92 +16-93,17-96 +1-99,2-88 +23-89,88-93 +30-73,3-73 +9-75,75-76 +75-97,70-99 +4-23,22-23 +5-94,4-4 +16-48,15-22 +10-62,61-97 +34-82,81-87 +16-98,15-99 +16-22,17-22 +20-40,21-58 +67-86,22-87 +6-73,6-33 +27-73,40-55 +57-98,58-97 +94-95,14-80 +16-90,91-91 +2-89,5-89 +17-95,96-96 +46-48,19-47 +50-59,51-60 +45-98,44-97 +99-99,6-97 +3-29,1-29 +26-48,25-49 +10-17,19-57 +28-57,28-56 +79-96,80-97 +5-78,4-25 +35-84,35-84 +24-99,20-98 +80-80,53-79 +88-97,12-88 +9-20,9-72 +75-78,35-54 +11-29,28-91 +48-74,49-75 +56-56,56-98 +41-69,33-42 +6-40,6-40 +49-80,49-79 +12-29,18-70 +19-20,13-80 +96-96,15-95 +5-99,4-4 +47-47,41-46 +53-82,65-85 +88-90,89-90 +12-12,12-88 +52-68,51-67 +1-80,2-81 +75-81,70-76 +35-58,35-57 +19-91,18-19 +18-73,72-74 +9-13,8-10 +5-40,35-37 +43-71,70-71 +24-24,25-43 +60-92,35-60 +43-52,47-53 +8-97,97-99 +57-89,57-89 +69-70,31-69 +48-49,48-79 +5-13,14-97 +9-95,9-83 +80-80,79-83 +30-77,29-78 +67-75,67-76 +20-67,21-87 +22-99,74-99 +12-12,11-96 +26-61,25-67 +19-48,47-91 +4-88,81-89 +75-96,74-97 +37-38,37-75 +3-3,4-76 +7-87,7-8 +1-99,2-99 +11-84,83-84 +71-95,69-95 +92-94,2-93 +7-95,21-95 +28-45,27-46 +16-24,16-63 +62-72,68-71 +36-96,90-98 +14-95,94-99 +26-67,25-55 +21-73,21-90 +35-44,34-43 +48-65,48-65 +80-89,80-99 +3-45,44-44 +27-86,67-85 +4-58,4-58 +12-38,11-39 +61-83,82-83 +4-86,4-85 +40-58,10-41 +8-9,8-98 +70-71,12-70 +40-40,40-78 +4-38,17-39 +5-23,22-94 +43-82,47-83 +32-74,86-96 +5-91,90-97 +10-93,11-86 +82-83,44-82 +61-69,57-68 +31-95,30-32 +42-59,42-98 +11-91,9-92 +42-81,19-63 +71-74,72-87 +88-89,6-88 +83-83,21-75 +50-61,59-62 +95-98,25-57 +13-17,16-87 +4-24,3-23 +4-56,3-5 +51-86,50-87 +49-55,30-56 +8-86,8-85 +3-99,3-3 +10-16,10-19 +6-96,5-95 +1-82,17-69 +35-62,35-55 +32-82,83-83 +9-76,75-75 +1-2,3-99 +86-94,93-99 +41-89,47-89 +2-60,3-61 +21-27,18-29 +5-6,6-54 +13-99,13-76 +71-71,34-70 +29-77,29-77 +7-83,6-64 +22-84,12-21 +93-97,15-94 +94-94,7-89 +24-94,32-99 +31-67,43-67 +46-61,46-60 +11-61,58-60 +5-58,5-5 +30-97,30-97 +65-83,82-83 +96-99,3-97 +11-97,6-12 +38-86,39-87 +24-24,24-66 +98-98,43-99 +5-32,29-33 +22-60,22-60 +9-72,71-71 +4-6,5-86 +27-30,31-94 +47-93,48-94 +1-2,5-91 +14-87,87-87 +26-75,25-96 +9-93,13-70 +6-10,9-95 +11-14,13-87 +69-71,2-69 +89-97,77-96 +5-16,4-14 +31-99,31-99 +30-39,33-88 +16-96,10-99 +20-74,19-73 +71-89,17-90 +50-51,26-50 +50-97,51-98 +4-84,4-84 +71-79,60-96 +39-83,81-82 +87-88,10-88 +93-99,68-99 +22-98,23-89 +12-96,13-97 +12-85,13-83 +3-97,3-98 +30-52,29-52 +11-96,97-97 +76-96,45-91 +22-97,23-98 +1-97,96-96 +4-83,5-84 +7-61,60-87 +62-66,37-61 +83-87,54-93 +40-74,7-40 +56-62,48-63 +88-94,57-89 +91-94,58-75 +1-88,87-87 +33-35,26-38 +9-10,9-94 +13-30,29-97 +95-95,23-95 +5-54,54-61 +16-73,72-73 +28-99,29-29 +32-91,33-90 +17-73,18-72 +20-34,13-20 +7-74,8-8 +43-58,42-59 +35-93,96-98 +31-71,62-89 +7-86,6-87 +37-78,38-79 +10-88,9-89 +5-94,20-94 +82-91,83-99 +14-34,4-14 +13-13,12-72 +1-98,16-98 +6-33,33-72 +35-83,36-36 +95-98,34-96 +23-23,22-68 +37-94,37-94 +42-97,96-99 +11-78,2-5 +23-88,87-87 +42-78,41-77 +58-74,58-78 +15-97,15-16 +3-11,5-83 +91-92,47-92 +63-96,62-99 +40-72,9-71 +64-89,37-69 +53-99,98-98 +28-33,26-34 +89-97,14-95 +16-93,92-97 +5-53,52-79 +52-52,53-96 +25-32,26-31 +35-73,35-74 +10-91,4-13 +2-28,5-28 +87-88,22-88 +31-76,32-75 +82-84,7-83 +38-61,60-60 +3-17,2-18 +74-93,4-94 +4-72,18-71 +69-71,19-70 +26-94,4-95 +44-77,52-78 +32-79,31-80 +45-79,23-46 +11-85,10-84 +75-95,19-76 +5-76,5-6 +60-71,65-70 +53-61,52-82 +4-4,5-95 +6-91,90-98 +45-92,46-92 +20-97,10-84 +21-45,31-46 +67-69,43-68 +49-70,12-70 +85-96,96-97 +7-77,8-69 +44-77,77-78 +13-86,85-85 +17-17,17-37 +77-79,13-78 +4-38,5-87 +22-23,23-47 +78-87,30-87 +12-20,1-11 +47-56,46-54 +3-52,1-2 +3-95,2-3 +9-40,8-13 +81-98,18-94 +42-69,38-89 +8-23,24-24 +16-73,15-17 +92-92,18-92 +3-78,78-79 +2-34,35-35 +19-61,60-83 +3-81,81-82 +7-61,6-40 +15-84,1-16 +52-70,30-69 +15-41,14-40 +32-73,32-74 +24-28,27-54 +2-98,18-98 +85-85,15-86 +1-88,2-88 +45-84,46-83 +20-26,34-52 +40-60,19-43 +51-81,80-81 +58-79,57-91 +12-52,28-38 +18-57,19-96 +4-99,2-3 +99-99,5-99 +52-75,53-53 +24-96,24-95 +9-93,10-87 +77-93,7-78 +17-81,18-80 +1-2,4-42 +24-47,46-73 +57-58,50-58 +21-58,21-82 +31-65,3-65 +5-10,3-8 +55-97,94-96 +31-40,30-34 +18-37,36-94 +18-88,17-27 +57-65,58-64 +55-56,55-56 +12-87,12-88 +69-98,14-52 +53-61,20-52 +40-62,16-63 +1-97,98-98 +2-84,1-2 +8-55,7-56 +91-91,91-92 +5-95,5-95 +9-17,2-16 +73-73,72-91 +44-88,5-87 +47-83,48-82 +1-23,22-83 +11-84,12-86 +90-93,5-92 +63-75,7-70 +60-89,89-90 +2-93,93-93 +14-60,13-28 +36-89,36-36 +49-99,4-97 +15-42,15-41 +12-90,90-91 +71-96,99-99 +37-37,37-70 +40-72,39-73 +25-60,26-59 +49-94,49-49 +20-80,21-81 +11-29,24-54 +12-88,55-89 +9-71,8-70 +4-69,5-68 +37-72,48-73 +26-98,27-58 +85-91,13-95 +8-41,8-8 +56-98,30-55 +21-92,20-97 +33-99,3-32 +1-61,3-94 +15-98,14-97 +60-99,59-61 +54-82,53-91 +21-33,17-41 +74-97,75-96 +10-51,52-52 +5-70,4-69 +41-93,42-92 +6-96,7-97 +17-28,2-27 +17-83,6-11 +53-99,54-96 +5-5,4-80 +36-81,53-72 +60-77,65-94 +44-87,87-88 +19-93,19-20 +63-80,43-70 +15-46,14-45 +61-78,77-78 +79-94,70-95 +20-41,20-20 +4-11,4-10 +10-98,4-10 +94-95,67-90 +9-56,10-10 +4-93,3-94 +49-59,49-83 +10-94,11-90 +20-21,20-92 +19-95,17-62 +1-95,69-96 +6-91,49-90 +12-98,11-99 +56-89,56-80 +42-96,42-97 +41-92,13-52 +78-94,57-86 +2-82,2-81 +1-1,1-60 +17-71,71-96 +10-88,9-34 +17-17,17-18 +35-99,34-90 +8-97,8-35 +73-85,74-85 +3-56,47-55 +1-4,3-76 +66-93,66-93 +27-27,26-66 +6-79,27-80 +49-89,48-89 +60-99,83-95 +64-64,48-64 +63-64,57-65 +15-17,30-95 +59-62,59-66 +4-29,5-99 +52-72,70-70 +5-91,1-98 +16-19,16-34 +35-35,1-34 +33-85,28-89 +7-78,6-79 +36-79,10-35 +52-74,51-75 +45-77,47-95 +26-26,26-93 +1-3,3-93 +69-88,8-69 +63-64,14-63 +46-46,45-88 +11-37,12-18 +5-92,6-91 +67-68,68-69 +31-89,32-32 +49-89,50-90 +3-4,4-94 +1-96,2-86 +6-91,7-90 +77-78,8-78 +38-76,38-77 +2-98,97-99 +43-48,48-71 +12-76,11-77 +25-40,25-41 +16-55,15-55 +1-91,2-91 +42-82,42-42 +32-90,31-33 +13-98,13-97 +18-80,18-65 +92-97,7-78 +1-3,2-69 +3-88,2-89 +6-94,7-67 +22-98,5-97 +24-32,33-96 +5-70,83-93 +1-61,60-61 +1-99,78-96 +88-98,4-89 +95-97,50-96 +56-93,55-97 +13-76,52-65 +86-87,40-87 +35-62,34-69 +26-44,27-60 +47-66,65-66 +2-97,1-87 +1-96,3-85 +13-82,16-82 +25-85,3-86 +36-63,3-99 +12-98,97-99 +4-6,2-5 +38-67,65-67 +77-96,27-30 +15-95,14-14 +22-87,22-60 +12-19,12-81 +16-79,8-80 +34-94,3-93 +26-62,25-62 +9-91,8-9 +94-98,30-33 +14-68,68-91 +79-95,84-95 +92-95,93-96 +2-96,95-99 +25-58,9-57 +7-87,6-6 +44-84,43-83 +78-88,77-87 +67-70,61-71 +35-64,64-65 +40-40,5-39 +9-74,9-74 +70-81,82-82 +11-58,10-11 +2-13,13-83 +56-71,70-88 +4-8,7-94 +5-88,4-93 +40-88,39-88 +14-84,69-83 +5-30,29-30 +91-94,92-94 +4-73,2-45 +69-98,30-70 +44-72,73-73 +32-41,32-38 +76-81,68-81 +62-75,62-69 +23-36,35-69 +95-96,26-95 +15-89,14-64 +8-75,9-97 +15-17,14-58 +16-18,18-92 +57-77,58-76 +90-98,23-91 +1-97,96-96 +37-96,1-38 +8-99,9-53 +4-9,10-10 +11-27,11-22 +83-95,80-86 +88-90,54-89 +53-66,53-57 +14-14,15-90 +6-9,8-98 +6-99,32-93 +28-70,29-70 +6-64,64-98 +18-68,19-69 +16-42,16-43 +3-91,2-91 +1-95,2-94 +18-19,8-19 +2-99,9-97 +76-86,42-86 +83-94,25-84 +21-64,9-87 +89-95,29-90 +4-96,3-97 +20-96,19-20 +28-74,28-29 +19-22,22-93 +62-87,83-97 +72-73,23-73 +22-52,21-51 +39-39,39-98 +9-95,9-95 +53-95,66-95 +36-87,19-37 +86-87,34-86 +12-56,11-11 +48-97,47-81 +26-62,54-58 +21-63,22-64 +27-90,90-90 +54-70,64-68 +7-84,92-94 +15-35,35-57 +19-91,20-92 +99-99,78-95 +46-89,46-47 +8-68,9-57 +4-77,1-69 +21-36,35-58 +24-30,26-30 +50-90,91-91 +2-31,8-64 +3-98,98-98 +43-70,70-71 +24-79,25-78 +30-30,31-73 +7-10,11-90 +1-95,2-2 +70-82,82-93 +90-90,4-59 +2-82,2-68 +4-19,4-31 +33-68,10-39 +13-22,21-91 +24-67,24-25 +1-9,4-10 +3-3,2-50 +42-84,43-83 +23-84,62-83 +78-79,78-80 +30-58,30-58 +71-71,38-72 +35-43,46-82 +49-55,31-75 +22-98,22-99 +27-57,26-57 +24-24,24-77 +89-92,79-89 +70-95,10-93 +13-36,14-21 +13-98,80-97 +11-14,17-48 +59-92,59-92 +19-26,18-40 +6-32,7-31 +12-48,12-12 +22-87,23-98 +5-96,6-88 +3-85,1-4 +85-88,43-85 +19-89,20-81 +52-52,53-71 +87-96,14-88 +4-5,1-1 +8-79,7-78 +76-77,7-77 +67-99,68-98 +21-68,66-69 +93-94,91-95 +24-88,8-87 +10-52,19-53 +27-86,28-84 +35-95,36-94 +27-86,85-86 +30-31,31-57 +39-87,38-91 +92-92,16-93 +2-83,1-92 +74-74,73-73 +5-25,23-26 +11-12,12-73 +68-80,71-80 +67-92,66-91 +23-33,25-33 +46-77,34-47 +25-78,23-78 +95-97,53-96 +99-99,1-99 +42-43,43-89 +17-18,9-18 +7-48,48-55 +36-61,60-62 +45-82,82-94 +1-96,3-96 +33-66,34-65 +21-81,20-82 +74-76,15-75 +25-25,25-86 +10-79,94-96 +8-94,5-9 +16-89,8-93 +20-21,21-95 +48-94,49-74 +88-89,89-89 +24-60,25-91 +85-85,31-86 +4-82,4-41 +27-88,27-87 +18-94,17-95 +67-76,51-89 +13-49,26-48 +9-88,9-9 +41-86,42-85 +57-96,14-58 +95-95,11-92 +58-58,57-86 +23-93,12-15 +1-97,2-97 +22-22,8-22 +51-91,50-90 +7-25,51-92 +74-88,73-87 +3-82,4-81 +89-98,67-90 +7-99,7-98 +26-62,31-71 +32-88,32-88 +14-65,46-64 +56-82,81-82 +54-64,53-65 +4-85,5-5 +85-86,3-85 +7-58,1-8 +4-82,5-56 +47-92,48-75 +27-79,28-80 +7-34,8-94 +35-40,35-46 +84-94,80-95 +83-99,84-98 +10-81,11-73 +5-91,20-23 +1-2,2-99 +50-50,9-51 +5-78,4-79 +9-99,61-98 +79-79,61-78 +13-20,14-20 +8-93,7-92 +3-27,3-99 +28-66,29-65 +15-97,14-99 +31-43,42-67 +53-57,54-56 +11-89,88-93 +77-82,78-78 +4-99,4-99 +16-75,12-14 +17-52,33-51 +8-83,41-55 +48-90,44-49 +1-4,3-72 +25-91,91-92 +3-10,4-9 +32-98,8-94 +32-33,1-33 +33-43,14-42 +23-83,24-83 +30-31,31-49 +7-84,84-85 +26-50,49-65 +73-85,46-92 +85-86,13-39 +7-78,7-75 +32-93,26-26 +59-96,5-97 +48-99,48-79 +21-24,21-25 +58-95,99-99 +26-27,22-26 +19-19,3-99 +46-97,46-96 +2-3,2-99 +26-92,6-21 +1-99,1-2 +22-31,31-81 +10-65,9-86 +1-99,2-98 +13-78,13-79 +96-96,54-96 +8-94,7-23 +1-97,96-99 +28-70,27-71 +37-95,37-94 +9-90,4-89 +9-92,10-93 +8-80,86-94 +2-2,4-9 +6-41,6-41 +25-76,24-77 +95-98,85-95 +4-82,1-2 +56-73,56-84 +2-99,20-99 +14-17,17-91 +56-74,55-75 +45-97,99-99 +56-56,16-55 +43-93,44-86 +61-84,25-83 +82-99,5-83 +19-37,3-18 +42-42,4-42 +34-41,35-78 +12-79,11-80 +3-72,4-71 +16-99,23-85 +21-46,47-47 +33-65,33-33 +36-57,36-58 +5-82,82-83 +2-95,1-95 +1-37,5-31 +70-70,7-69 +17-86,18-87 +17-91,44-91 +73-88,73-89 +40-89,39-92 +68-97,91-96 +25-97,7-96 +36-83,37-83 +70-74,30-63 +56-93,57-93 +98-99,58-99 +15-95,94-97 +26-40,40-41 +3-99,2-44 +20-32,31-31 +57-97,56-57 +38-61,17-37 +13-62,12-63 +60-76,21-78 +57-66,56-67 +60-97,77-98 +43-68,42-43 +23-56,23-79 +34-44,35-44 +39-74,38-90 +3-84,2-85 +3-31,4-99 +74-85,73-85 +11-19,18-79 +25-25,26-64 +86-91,93-94 +16-95,15-92 +64-71,63-69 +45-45,35-44 +94-97,17-92 +29-97,28-98 +67-71,20-71 +7-86,7-85 +14-45,22-39 +6-66,6-19 +44-93,94-94 +52-62,52-61 +3-3,4-71 +22-80,21-81 +35-98,35-98 +37-44,38-43 +50-64,50-54 +50-99,51-99 +9-24,10-23 +24-24,23-86 +24-72,25-72 +33-61,49-62 +35-72,73-77 +78-79,79-96 +30-30,30-58 +58-76,36-75 +34-69,35-68 +7-91,5-6 +1-91,1-91 +2-2,2-99 +46-78,40-46 +7-78,6-79 +54-70,4-32 +63-76,5-77 +15-60,59-86 +1-75,69-75 +4-66,67-67 +15-99,15-95 +10-17,2-53 +2-46,18-61 +71-71,48-71 +19-98,14-99 +12-48,11-31 +30-30,30-98 +63-92,6-64 +21-94,95-95 +13-66,65-67 +7-11,14-79 +18-18,16-17 +11-45,91-97 +26-34,25-34 +40-60,4-81 +19-69,18-68 +34-79,99-99 +29-39,25-57 +2-98,98-98 +96-99,47-96 +6-45,9-45 +1-11,10-85 +6-77,5-76 +68-79,68-79 +45-55,46-49 +66-69,68-70 +14-80,20-77 +25-69,31-70 +16-53,15-54 +56-99,56-99 +45-47,32-46 +50-63,44-62 +18-75,29-76 +11-11,12-31 +19-91,90-91 +16-95,17-96 +19-95,19-94 +20-95,19-20 +17-96,16-95 +13-16,13-78 +50-62,34-63 +80-99,81-98 +13-81,83-96 +92-98,42-93 +38-89,57-89 +62-65,66-88 +54-85,55-85 +17-87,16-17 +3-6,6-74 +5-99,6-98 +22-41,73-73 +14-78,12-78 +35-51,34-47 +94-94,15-94 +58-75,13-71 +20-51,18-21 +31-60,30-96 +29-89,28-95 +39-40,40-82 +14-85,17-84 +27-57,26-94 +24-91,23-24 +8-72,71-76 +49-69,6-69 +28-28,29-52 +25-88,54-87 +89-90,16-90 +28-99,28-30 +17-17,17-88 +19-67,68-77 +64-64,34-65 +3-97,22-98 +17-98,16-97 +30-89,91-97 +13-30,14-31 +1-4,3-97 +2-93,1-98 +13-83,13-83 +80-90,80-91 +7-10,3-9 +65-79,70-80 +24-92,24-93 +16-94,43-81 +33-86,62-85 +53-78,79-79 +3-70,69-99 +95-95,16-96 +41-69,42-74 +83-83,51-82 +2-39,3-39 +6-15,5-16 +6-49,6-49 +57-95,95-96 +7-78,6-99 +78-83,81-99 +41-99,7-98 +1-98,2-97 +41-90,23-42 +9-61,9-24 +13-79,40-55 +4-13,10-18 +51-55,50-77 +87-90,86-91 +15-16,15-96 +19-29,20-28 +46-46,47-49 +9-82,7-7 +12-55,6-20 +4-41,4-41 +1-94,66-93 +4-91,4-92 +7-61,21-91 +7-90,38-91 +12-32,32-35 +6-94,93-94 +6-7,7-7 \ No newline at end of file diff --git a/data/advent04a.txt b/data/advent04a.txt new file mode 100644 index 0000000..99a66c5 --- /dev/null +++ b/data/advent04a.txt @@ -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 \ No newline at end of file diff --git a/problems/day04.html b/problems/day04.html new file mode 100644 index 0000000..cb0c49b --- /dev/null +++ b/problems/day04.html @@ -0,0 +1,168 @@ + + + + +Day 4 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 8*

       y(2022)

+ + + +
+

--- Day 4: Camp Cleanup ---

Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs.

+

However, as some of the Elves compare their section assignments with each other, they've noticed that many of the assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list of the section assignments for each pair (your puzzle input).

+

For example, consider the following list of section assignment pairs:

+
2-4,6-8
+2-3,4-5
+5-7,7-9
+2-8,3-7
+6-6,4-6
+2-6,4-8
+
+

For the first few pairs, this list means:

+
    +
  • Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8).
  • +
  • The Elves in the second pair were each assigned two sections.
  • +
  • The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also got 7, plus 8 and 9.
  • +
+

This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this:

+
.234.....  2-4
+.....678.  6-8
+
+.23......  2-3
+...45....  4-5
+
+....567..  5-7
+......789  7-9
+
+.2345678.  2-8
+..34567..  3-7
+
+.....6...  6-6
+...456...  4-6
+
+.23456...  2-6
+...45678.  4-8
+
+

Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are 2 such pairs.

+

In how many assignment pairs does one range fully contain the other?

+
+

Your puzzle answer was 513.

--- Part Two ---

It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all.

+

In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:

+
    +
  • 5-7,7-9 overlaps in a single section, 7.
  • +
  • 2-8,3-7 overlaps all of the sections 3 through 7.
  • +
  • 6-6,4-6 overlaps in a single section, 6.
  • +
  • 2-6,4-8 overlaps in sections 4, 5, and 6.
  • +
+

So, in this example, the number of overlapping assignment pairs is 4.

+

In how many assignment pairs do the ranges overlap?

+
+

Your puzzle answer was 878.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file