From 56981df4c5c8e4b2d88a43dae276083766d0378b Mon Sep 17 00:00:00 2001 From: Mulling Date: Sun, 10 Dec 2023 01:58:52 -0300 Subject: [PATCH] Add day 3 part 1 --- content/aoc/aoc-2023-mulling.md | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/content/aoc/aoc-2023-mulling.md b/content/aoc/aoc-2023-mulling.md index 63b38e2..c62a89b 100644 --- a/content/aoc/aoc-2023-mulling.md +++ b/content/aoc/aoc-2023-mulling.md @@ -8,6 +8,7 @@ draft: false - [Day 1](#day-1) - [Day 2](#day-2) +- [Day 3](#day-3) ## Day 1 This problem kinda stinks, plus I'm still very rusty with the ol' Haskell. @@ -93,3 +94,39 @@ main = interact (show . sum . ([power . tail . dropWhile (/=':')] <*>) . lines) | isPrefixOf "blue" color = go [r, g, max (read num) b] xs go acc _ = product acc ``` + +## Day 3 +### Part 1 +Not looking forward to part 2. + +```haskell +import Control.Applicative +import Data.Char + +zipper :: Maybe [Bool] -> [[Char]] -> [([Char], [Bool])] +zipper _ [] = [] +zipper (Just prev) [xs] = [(xs, prev)] +zipper (Just prev) (xs : xss@(next : _)) = (xs, zipWith (||) prev (getSymbols <$> next)) : zipper (Just $ getSymbols <$> xs) xss +zipper Nothing (xs : xss@(next : _)) = (xs, getSymbols <$> next) : zipper (Just $ getSymbols <$> xs) xss +zipper Nothing _ = [] + +go :: Bool -> [Char] -> [Char] -> [Bool] -> [[Char]] +go True acc [] [] = [acc] +go _ _ [] [] = [] +go emit acc (c : cs) (p : ps) + | isDigit c = go (emit || p) (acc <> [c]) cs ps + | emit || p || c /= '.' = case acc of + [] -> go (p || c /= '.') [] cs ps + _ -> acc : go (p || c /= '.') [] cs ps + | otherwise = go False [] cs ps +go _ _ _ _ = [] + +getSymbols :: Char -> Bool +getSymbols = liftA2 (&&) (not . isDigit) (/= '.') + +tags :: ([Char], [Bool]) -> [[Char]] +tags (xs, syms) = go False [] xs syms + +main :: IO () +main = interact (show . sum . map (\xs -> read xs :: Int) . concatMap tags . zipper Nothing . lines) +```