Slightly less ugly part 2 solution for day 02

This commit is contained in:
Natty 2023-12-02 15:01:55 +01:00
parent c7ae8ad496
commit 8501f5e48b
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
1 changed files with 5 additions and 2 deletions

View File

@ -22,10 +22,13 @@ mapCubes :: T.Text -> (Integer, [[(Integer, String)]])
mapCubes line = parseParts $ T.splitOn (T.pack ": ") line mapCubes line = parseParts $ T.splitOn (T.pack ": ") line
mergeMaxCube :: [(Integer, String)] -> (Integer, String) -> [(Integer, String)] mergeMaxCube :: [(Integer, String)] -> (Integer, String) -> [(Integer, String)]
mergeMaxCube acc (aa, col) = (\(a, c) -> if c == col then (max a aa, c) else (a, c)) <$> acc mergeMaxCube (h@(ax, colx) : xs) b@(ab, col)
| colx == col = (max ax ab, colx) : xs
| otherwise = h : mergeMaxCube xs b
mergeMaxCube [] v = [v]
main :: IO () main :: IO ()
main = do main = do
lns <- lines <$> getContents lns <- lines <$> getContents
let values = mapCubes . T.pack <$> lns let values = mapCubes . T.pack <$> lns
print $ sum $ product . (fst <$>) . foldl mergeMaxCube [(0, "red"), (0, "green"), (0, "blue")] . concat . snd <$> values print $ sum $ product . (fst <$>) . foldl mergeMaxCube [] . concat . snd <$> values