day12 part 1

part 2 is uhhhh
This commit is contained in:
Gattix 2023-12-13 20:07:22 +01:00
parent 53e82f9d67
commit 698977c5cc
3 changed files with 1046 additions and 0 deletions

40
2023/day12/day12.hs Normal file
View File

@ -0,0 +1,40 @@
import Control.Arrow
import Control.Monad
import Data.List
main :: IO ()
main = interact $ show . day12 . map parse . lines
parse :: String -> (String, [Int])
parse = second (read . ('[' :) . (++ "]") . tail) . break (== ' ')
day12 :: [(String, [Int])] -> Int
day12 = sum . map (length . genArrangements)
part2ify :: (String, [Int]) -> (String, [Int])
part2ify (s, xs) = (intercalate "?" $ replicate 5 s, concat $ replicate 5 xs)
genMin :: [Int] -> [String]
genMin = foldr (\x -> (:) (replicate x '#' ++ ".")) []
genDots :: Int -> Int -> [[Int]]
genDots n x = filter (\xs -> sum xs == x) $ replicateM n [0 .. x]
buildMaxeds :: [String] -> [[Int]] -> [String]
buildMaxeds ss = map (build ss)
build :: [String] -> [Int] -> String
build [] [x] = replicate x '.'
build (s:ss) (x:xs) = replicate x '.' ++ s ++ build ss xs
genArrangements :: (String, [Int]) -> [String]
genArrangements (s, xs) = filter f $ buildMaxeds (genMin xs) (genDots w l)
where
f x = and (zipWith wEq s x)
w = succ $ length xs
l = length s - sum xs - pred (length xs)
wEq :: Char -> Char -> Bool
wEq _ '?' = True
wEq '?' _ = True
wEq c1 c2 = c1 == c2

1000
2023/day12/input Normal file

File diff suppressed because it is too large Load Diff

6
2023/day12/testinput Normal file
View File

@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1