From b167933285daf6c70ae95834d625a3c6cb8b1a26 Mon Sep 17 00:00:00 2001 From: RhiobeT Date: Sat, 2 Dec 2023 12:45:15 +0100 Subject: [PATCH] Day 02 --- day02/Commons.hs | 41 +++++++++++++++++++++++++++++++++++++++++ day02/Main.hs | 11 +++++++++++ day02/Part1.hs | 17 +++++++++++++++++ day02/Part2.hs | 18 ++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 day02/Commons.hs create mode 100644 day02/Main.hs create mode 100644 day02/Part1.hs create mode 100644 day02/Part2.hs diff --git a/day02/Commons.hs b/day02/Commons.hs new file mode 100644 index 0000000..bbcaf0c --- /dev/null +++ b/day02/Commons.hs @@ -0,0 +1,41 @@ +module Commons where + +import Data.String.Utils +import System.IO +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]} + +parseRound :: String -> Round +parseRound roundRaw = parseCubes (split ", " roundRaw) Round {red = 0, green = 0, blue = 0} + +parseRounds :: String -> [Round] +parseRounds roundsRaw = map parseRound (split "; " roundsRaw) + +parseGame :: String -> Game +parseGame ('G': 'a': 'm': 'e': ' ': h: ':': ' ': t) = Game {gid = read [h], rounds = parseRounds t} +parseGame ('G': 'a': 'm': 'e': ' ': h1: h2: ':': ' ': t) = Game {gid = read [h1, h2], rounds = parseRounds t} +parseGame ('G': 'a': 'm': 'e': ' ': h1: h2: h3: ':': ' ': t) = Game {gid = read [h1, h2, h3], rounds = parseRounds t} +parseGame (_: t) = parseGame t + +parse :: IO [Game] +parse = do done <- isEOF + if done + then return [] + else do line <- getLine + let game = parseGame line + otherGames <- parse + return (game: otherGames) diff --git a/day02/Main.hs b/day02/Main.hs new file mode 100644 index 0000000..aa9c2e6 --- /dev/null +++ b/day02/Main.hs @@ -0,0 +1,11 @@ +module Main where + +import Commons +import Part1 +import Part2 + +main = do games <- parse + let part1Res = sum (Part1.getPossibleIds games) + putStrLn (show (part1Res)) + let part2Res = sum (Part2.getPower games) + putStrLn (show (part2Res)) diff --git a/day02/Part1.hs b/day02/Part1.hs new file mode 100644 index 0000000..1873614 --- /dev/null +++ b/day02/Part1.hs @@ -0,0 +1,17 @@ +module Part1 where + +import Commons + + +checkRoundsPossible :: [Round] -> Bool +checkRoundsPossible [] = True +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 diff --git a/day02/Part2.hs b/day02/Part2.hs new file mode 100644 index 0000000..78505ce --- /dev/null +++ b/day02/Part2.hs @@ -0,0 +1,18 @@ +module Part2 where + +import Commons + + +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) + +getPower :: [Game] -> [Int] +getPower [] = [] +getPower (game: t) = (getPowerGame game: getPower t)