solve 2022 day 2 part 2
This commit is contained in:
parent
2cd9633fe0
commit
c5ac6c069e
1 changed files with 42 additions and 17 deletions
|
@ -1,23 +1,48 @@
|
||||||
module Aoc.Day2 (solve) where
|
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
|
where
|
||||||
guide = map words . lines $ input
|
games = map convertGuide $ parseInput input
|
||||||
|
|
||||||
loss = 0
|
convertGuide [p, "X"] = [p, "A"]
|
||||||
draw = 3
|
convertGuide [p, "Y"] = [p, "B"]
|
||||||
win = 6
|
convertGuide [p, "Z"] = [p, "C"]
|
||||||
|
convertGuide xs = xs
|
||||||
|
|
||||||
rock = 1
|
solve 2 input = totalScore games
|
||||||
paper = 2
|
where
|
||||||
scissors = 3
|
games = map solveGuide $ parseInput input
|
||||||
|
|
||||||
scoreGame ["A", "X"] = rock + draw
|
-- X: loss, Y: draw, Z: win
|
||||||
scoreGame ["A", "Y"] = paper + win
|
solveGuide ["A", "X"] = ["A", "C"]
|
||||||
scoreGame ["A", "Z"] = scissors + loss
|
solveGuide ["A", "Y"] = ["A", "A"]
|
||||||
scoreGame ["B", "X"] = rock + loss
|
solveGuide ["A", "Z"] = ["A", "B"]
|
||||||
scoreGame ["B", "Y"] = paper + draw
|
solveGuide ["B", "X"] = ["B", "A"]
|
||||||
scoreGame ["B", "Z"] = scissors + win
|
solveGuide ["B", "Y"] = ["B", "B"]
|
||||||
scoreGame ["C", "X"] = rock + win
|
solveGuide ["B", "Z"] = ["B", "C"]
|
||||||
scoreGame ["C", "Y"] = paper + loss
|
solveGuide ["C", "X"] = ["C", "B"]
|
||||||
scoreGame ["C", "Z"] = scissors + draw
|
solveGuide ["C", "Y"] = ["C", "C"]
|
||||||
|
solveGuide ["C", "Z"] = ["C", "A"]
|
||||||
|
solveGuide xs = xs
|
||||||
|
|
||||||
|
solve _ _ = 0
|
||||||
|
|
Loading…
Reference in a new issue