1
0
Fork 0

solve 2022 day 2 part 2

This commit is contained in:
Sloane Perrault 2022-12-02 08:02:37 -05:00
parent 2cd9633fe0
commit c5ac6c069e

View file

@ -1,23 +1,48 @@
module Aoc.Day2 (solve) where
solve 1 input = sum $ map scoreGame guide
parseInput :: String -> [[String]]
parseInput = map words . lines
-- A: rock, B: paper, C: scissors
scoreGame :: [String] -> Integer
scoreGame ["A", "A"] = 1 + 3
scoreGame ["A", "B"] = 2 + 6
scoreGame ["A", "C"] = 3 + 0
scoreGame ["B", "A"] = 1 + 0
scoreGame ["B", "B"] = 2 + 3
scoreGame ["B", "C"] = 3 + 6
scoreGame ["C", "A"] = 1 + 6
scoreGame ["C", "B"] = 2 + 0
scoreGame ["C", "C"] = 3 + 3
scoreGame _ = 0
totalScore :: [[String]] -> Integer
totalScore = sum . map scoreGame
solve :: Integer -> String -> Integer
solve 1 input = totalScore games
where
guide = map words . lines $ input
games = map convertGuide $ parseInput input
loss = 0
draw = 3
win = 6
convertGuide [p, "X"] = [p, "A"]
convertGuide [p, "Y"] = [p, "B"]
convertGuide [p, "Z"] = [p, "C"]
convertGuide xs = xs
rock = 1
paper = 2
scissors = 3
solve 2 input = totalScore games
where
games = map solveGuide $ parseInput input
scoreGame ["A", "X"] = rock + draw
scoreGame ["A", "Y"] = paper + win
scoreGame ["A", "Z"] = scissors + loss
scoreGame ["B", "X"] = rock + loss
scoreGame ["B", "Y"] = paper + draw
scoreGame ["B", "Z"] = scissors + win
scoreGame ["C", "X"] = rock + win
scoreGame ["C", "Y"] = paper + loss
scoreGame ["C", "Z"] = scissors + draw
-- X: loss, Y: draw, Z: win
solveGuide ["A", "X"] = ["A", "C"]
solveGuide ["A", "Y"] = ["A", "A"]
solveGuide ["A", "Z"] = ["A", "B"]
solveGuide ["B", "X"] = ["B", "A"]
solveGuide ["B", "Y"] = ["B", "B"]
solveGuide ["B", "Z"] = ["B", "C"]
solveGuide ["C", "X"] = ["C", "B"]
solveGuide ["C", "Y"] = ["C", "C"]
solveGuide ["C", "Z"] = ["C", "A"]
solveGuide xs = xs
solve _ _ = 0