day 2 part 2

main
Araozu 2023-10-31 21:51:40 -05:00
parent 232efd2a10
commit 2102e35083
1 changed files with 39 additions and 1 deletions

View File

@ -15,6 +15,17 @@ enum RPS:
case _ => 0 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: object Day02 extends Solution:
override def part_01() = override def part_01() =
val file = read_input("02") match val file = read_input("02") match
@ -43,5 +54,32 @@ object Day02 extends Solution:
s"part 2: $score" 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"