It took me the afternoon but I got a LSP working

This commit is contained in:
2023-12-02 16:16:57 +01:00
parent a36a4ccbe9
commit eedd910d67
8 changed files with 35 additions and 31 deletions

View File

@@ -6,18 +6,17 @@ import Text.Read
data Round = Round { red :: Int, green :: Int, blue :: Int }
data Game = Game { gid :: Int, rounds :: [Round] }
parseCubes :: [String] -> Round -> Round
parseCubes [] round = round
parseCubes ((h: ' ': 'r': 'e': 'd': []): t) round = parseCubes t round {red = read [h]}
parseCubes ((h1: h2: ' ': 'r': 'e': 'd': []): t) round = parseCubes t round {red = read [h1, h2]}
parseCubes ((h: ' ': 'g': 'r': 'e': 'e': 'n': []): t) round = parseCubes t round {green = read [h]}
parseCubes ((h1: h2: ' ': 'g': 'r': 'e': 'e': 'n': []): t) round = parseCubes t round {green = read [h1, h2]}
parseCubes ((h: ' ': 'b': 'l': 'u': 'e': []): t) round = parseCubes t round {blue = read [h]}
parseCubes ((h1: h2: ' ': 'b': 'l': 'u': 'e': []): t) round = parseCubes t round {blue = read [h1, h2]}
parseCubes ([h, ' ', 'r', 'e', 'd']: t) round = parseCubes t round {red = read [h]}
parseCubes ([h1, h2, ' ', 'r', 'e', 'd']: t) round = parseCubes t round {red = read [h1, h2]}
parseCubes ([h, ' ', 'g', 'r', 'e', 'e', 'n']: t) round = parseCubes t round {green = read [h]}
parseCubes ([h1, h2, ' ', 'g', 'r', 'e', 'e', 'n']: t) round = parseCubes t round {green = read [h1, h2]}
parseCubes ([h, ' ', 'b', 'l', 'u', 'e']: t) round = parseCubes t round {blue = read [h]}
parseCubes ([h1, h2, ' ', 'b', 'l', 'u', 'e']: t) round = parseCubes t round {blue = read [h1, h2]}
parseRound :: String -> Round
parseRound roundRaw = parseCubes (split ", " roundRaw) Round {red = 0, green = 0, blue = 0}

View File

@@ -4,8 +4,9 @@ import Commons
import Part1
import Part2
main = do games <- parse
let part1Res = sum (Part1.getPossibleIds games)
putStrLn (show (part1Res))
print part1Res
let part2Res = sum (Part2.getPower games)
putStrLn (show (part2Res))
print part2Res

View File

@@ -5,13 +5,13 @@ import Commons
checkRoundsPossible :: [Round] -> Bool
checkRoundsPossible [] = True
checkRoundsPossible (round: t) = red round <= 12 && green round <= 13 && blue round <= 14 && (checkRoundsPossible t)
checkRoundsPossible (round: t) = red round <= 12 && green round <= 13 && blue round <= 14 && checkRoundsPossible t
checkGamePossible :: Game -> Bool
checkGamePossible Game{rounds=rounds} = checkRoundsPossible rounds
getPossibleIds :: [Game] -> [Int]
getPossibleIds [] = []
getPossibleIds (game: t) = if (checkGamePossible game)
then (gid game: getPossibleIds t)
else do getPossibleIds t
getPossibleIds (game: t) = if checkGamePossible game
then gid game: getPossibleIds t
else getPossibleIds t

View File

@@ -4,15 +4,14 @@ import Commons
getMinCubesRound :: [Round] -> Round
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)
getPowerGame :: Game -> Int
getPowerGame Game{rounds=rounds} = let minRound = getMinCubesRound rounds
in (red minRound) * (green minRound) * (blue minRound)
in red minRound * green minRound * blue minRound
getPower :: [Game] -> [Int]
getPower [] = []
getPower (game: t) = (getPowerGame game: getPower t)
getPower = map getPowerGame