main
Araozu 2023-10-31 21:53:42 -05:00
parent 2102e35083
commit 2dcca0e720
3 changed files with 36 additions and 41 deletions

View File

@ -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()

View File

@ -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"

View File

@ -6,5 +6,3 @@ import scala.util.Random
println("Advent of code 2022 with Scala")
println(Day02.part_01())
println(Day02.part_02())