Day 02
This commit is contained in:
41
day02/Commons.hs
Normal file
41
day02/Commons.hs
Normal file
@@ -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)
|
||||
11
day02/Main.hs
Normal file
11
day02/Main.hs
Normal file
@@ -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))
|
||||
17
day02/Part1.hs
Normal file
17
day02/Part1.hs
Normal file
@@ -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
|
||||
18
day02/Part2.hs
Normal file
18
day02/Part2.hs
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user