From 2dcca0e720c39f3dacbcd7c11b5494ffb4a0b45b Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 31 Oct 2023 21:53:42 -0500 Subject: [PATCH] format --- src/main/scala/Day01.scala | 5 +-- src/main/scala/Day02.scala | 70 +++++++++++++++++++------------------- src/main/scala/Main.scala | 2 -- 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/main/scala/Day01.scala b/src/main/scala/Day01.scala index dd26e44..1c95205 100644 --- a/src/main/scala/Day01.scala +++ b/src/main/scala/Day01.scala @@ -20,7 +20,6 @@ object Day01 extends Solution: max_calories_amount = current_sum current_sum = 0 - "part 1: " + max_calories_amount.toString() @@ -44,10 +43,8 @@ object Day01 extends Solution: else if current_sum > max2 then max3 = max2 max2 = current_sum - else if current_sum > max3 then - max3 = current_sum + else if current_sum > max3 then max3 = current_sum current_sum = 0 "part 2: " + (max1 + max2 + max3).toString() - diff --git a/src/main/scala/Day02.scala b/src/main/scala/Day02.scala index df0081b..c11b02d 100644 --- a/src/main/scala/Day02.scala +++ b/src/main/scala/Day02.scala @@ -5,26 +5,23 @@ enum RPS: case Rock, Paper, Scissor def get_score() = this match - case Rock => 1 - case Paper => 2 + case Rock => 1 + case Paper => 2 case Scissor => 3 def get_match_score(other: RPS) = (this, other) match case (Rock, Scissor) | (Paper, Rock) | (Scissor, Paper) => 6 case (Rock, Rock) | (Paper, Paper) | (Scissor, Scissor) => 3 - 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.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 - - + case (Draw, _) => opponent_choice object Day02 extends Solution: override def part_01() = @@ -37,20 +34,22 @@ object Day02 extends Solution: for line <- file.getLines() do val (opponent_choice, self_choice) = ( - 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' => RPS.Rock - case 'Y' => RPS.Paper - case 'Z' => RPS.Scissor - case _ => return "Unexpected char found (pos 2)" + 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' => RPS.Rock + case 'Y' => RPS.Paper + case 'Z' => RPS.Scissor + case _ => return "Unexpected char found (pos 2)" ) - score += self_choice.get_score() + self_choice.get_match_score(opponent_choice) + score += self_choice.get_score() + self_choice.get_match_score( + opponent_choice + ) s"part 2: $score" @@ -61,25 +60,26 @@ object Day02 extends Solution: 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)" + 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) + score += self_choice.get_score() + self_choice.get_match_score( + opponent_choice + ) s"part 2: $score" - diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala index dbffe2c..efafd1d 100644 --- a/src/main/scala/Main.scala +++ b/src/main/scala/Main.scala @@ -6,5 +6,3 @@ import scala.util.Random println("Advent of code 2022 with Scala") println(Day02.part_01()) println(Day02.part_02()) - -