Slightly less ugly part 2 solution for day 02
This commit is contained in:
parent
c7ae8ad496
commit
8501f5e48b
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue