From c31ec1dac0ff2a5b8c57d40dd89fb10b04f859ef Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Thu, 19 Dec 2024 22:41:22 +0100 Subject: [PATCH] Day 19, Part 1 --- adventofcode2024.cabal | 1 + src/Day19/Puzzle1.hs | 31 +++++++++++++++++++++++++++++++ src/Main.hs | 2 ++ 3 files changed, 34 insertions(+) create mode 100644 src/Day19/Puzzle1.hs diff --git a/adventofcode2024.cabal b/adventofcode2024.cabal index af94059..ab1ea7d 100644 --- a/adventofcode2024.cabal +++ b/adventofcode2024.cabal @@ -67,3 +67,4 @@ executable adventofcode2024 Day17.Puzzle1 Day18.Puzzle1 Day18.Puzzle2 + Day19.Puzzle1 diff --git a/src/Day19/Puzzle1.hs b/src/Day19/Puzzle1.hs new file mode 100644 index 0000000..0da8698 --- /dev/null +++ b/src/Day19/Puzzle1.hs @@ -0,0 +1,31 @@ +{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} + +module Day19.Puzzle1 (day19_1) where + +import Control.Monad (guard) +import Data.List (isPrefixOf) +import Data.List.Split (splitOn) + +getDesignCombinations :: String -> [String] -> String -> [String] +getDesignCombinations design towels cur = do + towel <- towels + guard $ towel `isPrefixOf` design + + let design' = drop (length towel) design + if null design' + then return $ drop 1 cur + else getDesignCombinations design' towels (cur ++ "," ++ towel) + +isDesignPossible :: String -> [String] -> Bool +isDesignPossible design towels = + let combinations = getDesignCombinations design towels "" + in (not . null) combinations + +day19_1 :: IO () +day19_1 = do + contents <- lines <$> readFile "input/day19.txt" + let [[ts], designs] = splitOn [""] contents + towels = splitOn ", " ts + putStrLn $ + "Day 19, Puzzle 1 solution: " + ++ show (length . filter id $ map (`isDesignPossible` towels) designs) diff --git a/src/Main.hs b/src/Main.hs index f10130c..4013256 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -30,6 +30,7 @@ import Day15.Puzzle1 import Day17.Puzzle1 import Day18.Puzzle1 import Day18.Puzzle2 +import Day19.Puzzle1 import System.Environment (getArgs) main :: IO () @@ -66,4 +67,5 @@ main = do "17" : "1" : _ -> day17_1 "18" : "1" : _ -> day18_1 "18" : "2" : _ -> day18_2 + "19" : "1" : _ -> day19_1 _ -> error "Not implemented"