From c5ac6c069e08781509dd46956fbb7613671fa0ed Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Fri, 2 Dec 2022 08:02:37 -0500 Subject: [PATCH] solve 2022 day 2 part 2 --- 2022/src/Aoc/Day2.hs | 59 +++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/2022/src/Aoc/Day2.hs b/2022/src/Aoc/Day2.hs index ba86738..96db79b 100644 --- a/2022/src/Aoc/Day2.hs +++ b/2022/src/Aoc/Day2.hs @@ -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