Day 19, Part 1
This commit is contained in:
parent
9aaa19a1a5
commit
c31ec1dac0
@ -67,3 +67,4 @@ executable adventofcode2024
|
|||||||
Day17.Puzzle1
|
Day17.Puzzle1
|
||||||
Day18.Puzzle1
|
Day18.Puzzle1
|
||||||
Day18.Puzzle2
|
Day18.Puzzle2
|
||||||
|
Day19.Puzzle1
|
||||||
|
31
src/Day19/Puzzle1.hs
Normal file
31
src/Day19/Puzzle1.hs
Normal file
@ -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)
|
@ -30,6 +30,7 @@ import Day15.Puzzle1
|
|||||||
import Day17.Puzzle1
|
import Day17.Puzzle1
|
||||||
import Day18.Puzzle1
|
import Day18.Puzzle1
|
||||||
import Day18.Puzzle2
|
import Day18.Puzzle2
|
||||||
|
import Day19.Puzzle1
|
||||||
import System.Environment (getArgs)
|
import System.Environment (getArgs)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@ -66,4 +67,5 @@ main = do
|
|||||||
"17" : "1" : _ -> day17_1
|
"17" : "1" : _ -> day17_1
|
||||||
"18" : "1" : _ -> day18_1
|
"18" : "1" : _ -> day18_1
|
||||||
"18" : "2" : _ -> day18_2
|
"18" : "2" : _ -> day18_2
|
||||||
|
"19" : "1" : _ -> day19_1
|
||||||
_ -> error "Not implemented"
|
_ -> error "Not implemented"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user