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

Commit

Permalink
Add day 2 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mulling committed Dec 7, 2023
1 parent df63229 commit 31d0e3a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions content/aoc/aoc-2023-mulling.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,38 @@ main = interact (show . sum . map (valid . ([(";"`T.splitOn`)]<*>) . T.splitOn "
valid' _ = False
parse xs = read (T.unpack xs) :: Int
```
### Part 2
Another round of type tetris.

```haskell
{-# LANGUAGE OverloadedStrings #-}

import qualified Data.Text as T

main :: IO ()
main = interact (show . sum. map (power . tail . T.splitOn ":" . T.pack). lines)
where
power sets = go [0, 0, 0] $ concat ([T.words] <*> sets)
go :: [Int] -> [T.Text] -> Int
go [r, g, b] (num:color:xs) | T.isPrefixOf "red" color = go [max (parse num) r, g, b] xs
| T.isPrefixOf "green" color = go [r, max (parse num) g, b] xs
| T.isPrefixOf "blue" color = go [r, g, max (parse num) b] xs
go acc _ = product acc
parse xs = read (T.unpack xs) :: Int
```

And without Data.Text things get way simpler.

```haskell
import Data.List

main :: IO ()
main = interact (show . sum . ([power . tail . dropWhile (/=':')] <*>) . lines)
where
power :: String -> Int
power = go [0,0,0] . words
go [r, g, b] (num:color:xs) | isPrefixOf "red" color = go [max (read num) r, g, b] xs
| isPrefixOf "green" color = go [r, max (read num) g, b] xs
| isPrefixOf "blue" color = go [r, g, max (read num) b] xs
go acc _ = product acc
```

0 comments on commit 31d0e3a

Please sign in to comment.