Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Add day 3 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Mulling committed Dec 10, 2023
1 parent 68600bf commit 56981df
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions content/aoc/aoc-2023-mulling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
```

0 comments on commit 56981df

Please sign in to comment.