diff --git a/src/main/scala/Day02.scala b/src/main/scala/Day02.scala index 9e6bc39..df0081b 100644 --- a/src/main/scala/Day02.scala +++ b/src/main/scala/Day02.scala @@ -15,6 +15,17 @@ enum RPS: case _ => 0 +enum Outcome: + case Win, Draw, Lose + + def get_choice(opponent_choice: RPS): RPS = (this, opponent_choice) match + case (Win, RPS.Rock) | (Lose, RPS.Scissor) => RPS.Paper + case (Win, RPS.Paper) | (Lose, RPS.Rock) => RPS.Scissor + case (Win, RPS.Scissor) | (Lose, RPS.Paper) => RPS.Rock + case (Draw, _) => opponent_choice + + + object Day02 extends Solution: override def part_01() = val file = read_input("02") match @@ -43,5 +54,32 @@ object Day02 extends Solution: s"part 2: $score" - override def part_02() = "part 2: :c" + override def part_02() = + val file = read_input("02") match + case Success(f) => f + case Failure(ex) => + return "Error opening file: " + ex.getMessage() + + var score = 0 + + for line <- file.getLines() do + val (opponent_choice, outcome) = ( + line.charAt(0) match + case 'A' => RPS.Rock + case 'B' => RPS.Paper + case 'C' => RPS.Scissor + case _ => return "Unexpected char found (pos 0)" + , + line.charAt(2) match + case 'X' => Outcome.Lose + case 'Y' => Outcome.Draw + case 'Z' => Outcome.Win + case _ => return "Unexpected char found (pos 2)" + ) + + val self_choice = outcome.get_choice(opponent_choice) + + score += self_choice.get_score() + self_choice.get_match_score(opponent_choice) + + s"part 2: $score"