Play with syntax

This commit is contained in:
2023-12-04 19:05:20 +01:00
parent e746e22b0b
commit 53cad097f8
15 changed files with 27 additions and 26 deletions

View File

@@ -16,10 +16,10 @@ parseCubes ([h, "green"]: t) round = parseCubes t round {green = read h}
parseCubes ([h, "blue"]: t) round = parseCubes t round {blue = read h}
parseRound :: String -> Round
parseRound roundRaw = parseCubes (map words (split ", " roundRaw)) Round {red = 0, green = 0, blue = 0}
parseRound roundRaw = parseCubes (map words $ split ", " roundRaw) Round {red = 0, green = 0, blue = 0}
parseRounds :: String -> [Round]
parseRounds roundsRaw = map parseRound (split "; " roundsRaw)
parseRounds roundsRaw = map parseRound $ split "; " roundsRaw
parseGameId :: String -> String
parseGameId ('G': 'a': 'm': 'e': ' ': t) = parseGameId t
@@ -28,7 +28,7 @@ parseGameId [] = []
parseGame :: String -> Game
parseGame line = let (h: t: _) = split ": " line
in Game {gid = read (parseGameId h), rounds = parseRounds t}
in Game {gid = read . parseGameId $ h, rounds = parseRounds t}
parse :: IO [Game]
parse = do done <- isEOF
@@ -37,4 +37,4 @@ parse = do done <- isEOF
else do line <- getLine
let game = parseGame line
otherGames <- parse
return (game: otherGames)
return $ game: otherGames

View File

@@ -6,7 +6,7 @@ import Part2
main = do games <- parse
let part1Res = sum (Part1.getPossibleIds games)
let part1Res = sum $ Part1.getPossibleIds games
print part1Res
let part2Res = sum (Part2.getPower games)
let part2Res = sum $ Part2.getPower games
print part2Res

View File

@@ -1,13 +1,14 @@
module Part2 where
import Commons
import Data.Function (on)
getMinCubesRound :: [Round] -> Round
getMinCubesRound [round] = round
getMinCubesRound (round: otherRound: t) = getMinCubesRound (Round {red = max (red round) (red otherRound),
green = max (green round) (green otherRound),
blue = max (blue round) (blue otherRound)}: t)
getMinCubesRound (round: otherRound: t) = getMinCubesRound $ Round {red = (max `on` red) round otherRound,
green = (max `on` green) round otherRound,
blue = (max `on` blue) round otherRound}: t
getPowerGame :: Game -> Int
getPowerGame Game{rounds=rounds} = let minRound = getMinCubesRound rounds