format
This commit is contained in:
parent
2102e35083
commit
2dcca0e720
@ -21,7 +21,6 @@ object Day01 extends Solution:
|
|||||||
|
|
||||||
current_sum = 0
|
current_sum = 0
|
||||||
|
|
||||||
|
|
||||||
"part 1: " + max_calories_amount.toString()
|
"part 1: " + max_calories_amount.toString()
|
||||||
|
|
||||||
override def part_02() =
|
override def part_02() =
|
||||||
@ -44,10 +43,8 @@ object Day01 extends Solution:
|
|||||||
else if current_sum > max2 then
|
else if current_sum > max2 then
|
||||||
max3 = max2
|
max3 = max2
|
||||||
max2 = current_sum
|
max2 = current_sum
|
||||||
else if current_sum > max3 then
|
else if current_sum > max3 then max3 = current_sum
|
||||||
max3 = current_sum
|
|
||||||
|
|
||||||
current_sum = 0
|
current_sum = 0
|
||||||
|
|
||||||
"part 2: " + (max1 + max2 + max3).toString()
|
"part 2: " + (max1 + max2 + max3).toString()
|
||||||
|
|
||||||
|
@ -5,26 +5,23 @@ enum RPS:
|
|||||||
case Rock, Paper, Scissor
|
case Rock, Paper, Scissor
|
||||||
|
|
||||||
def get_score() = this match
|
def get_score() = this match
|
||||||
case Rock => 1
|
case Rock => 1
|
||||||
case Paper => 2
|
case Paper => 2
|
||||||
case Scissor => 3
|
case Scissor => 3
|
||||||
|
|
||||||
def get_match_score(other: RPS) = (this, other) match
|
def get_match_score(other: RPS) = (this, other) match
|
||||||
case (Rock, Scissor) | (Paper, Rock) | (Scissor, Paper) => 6
|
case (Rock, Scissor) | (Paper, Rock) | (Scissor, Paper) => 6
|
||||||
case (Rock, Rock) | (Paper, Paper) | (Scissor, Scissor) => 3
|
case (Rock, Rock) | (Paper, Paper) | (Scissor, Scissor) => 3
|
||||||
case _ => 0
|
case _ => 0
|
||||||
|
|
||||||
|
|
||||||
enum Outcome:
|
enum Outcome:
|
||||||
case Win, Draw, Lose
|
case Win, Draw, Lose
|
||||||
|
|
||||||
def get_choice(opponent_choice: RPS): RPS = (this, opponent_choice) match
|
def get_choice(opponent_choice: RPS): RPS = (this, opponent_choice) match
|
||||||
case (Win, RPS.Rock) | (Lose, RPS.Scissor) => RPS.Paper
|
case (Win, RPS.Rock) | (Lose, RPS.Scissor) => RPS.Paper
|
||||||
case (Win, RPS.Paper) | (Lose, RPS.Rock) => RPS.Scissor
|
case (Win, RPS.Paper) | (Lose, RPS.Rock) => RPS.Scissor
|
||||||
case (Win, RPS.Scissor) | (Lose, RPS.Paper) => RPS.Rock
|
case (Win, RPS.Scissor) | (Lose, RPS.Paper) => RPS.Rock
|
||||||
case (Draw, _) => opponent_choice
|
case (Draw, _) => opponent_choice
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
object Day02 extends Solution:
|
object Day02 extends Solution:
|
||||||
override def part_01() =
|
override def part_01() =
|
||||||
@ -37,20 +34,22 @@ object Day02 extends Solution:
|
|||||||
|
|
||||||
for line <- file.getLines() do
|
for line <- file.getLines() do
|
||||||
val (opponent_choice, self_choice) = (
|
val (opponent_choice, self_choice) = (
|
||||||
line.charAt(0) match
|
line.charAt(0) match
|
||||||
case 'A' => RPS.Rock
|
case 'A' => RPS.Rock
|
||||||
case 'B' => RPS.Paper
|
case 'B' => RPS.Paper
|
||||||
case 'C' => RPS.Scissor
|
case 'C' => RPS.Scissor
|
||||||
case _ => return "Unexpected char found (pos 0)"
|
case _ => return "Unexpected char found (pos 0)"
|
||||||
,
|
,
|
||||||
line.charAt(2) match
|
line.charAt(2) match
|
||||||
case 'X' => RPS.Rock
|
case 'X' => RPS.Rock
|
||||||
case 'Y' => RPS.Paper
|
case 'Y' => RPS.Paper
|
||||||
case 'Z' => RPS.Scissor
|
case 'Z' => RPS.Scissor
|
||||||
case _ => return "Unexpected char found (pos 2)"
|
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"
|
s"part 2: $score"
|
||||||
|
|
||||||
@ -64,22 +63,23 @@ object Day02 extends Solution:
|
|||||||
|
|
||||||
for line <- file.getLines() do
|
for line <- file.getLines() do
|
||||||
val (opponent_choice, outcome) = (
|
val (opponent_choice, outcome) = (
|
||||||
line.charAt(0) match
|
line.charAt(0) match
|
||||||
case 'A' => RPS.Rock
|
case 'A' => RPS.Rock
|
||||||
case 'B' => RPS.Paper
|
case 'B' => RPS.Paper
|
||||||
case 'C' => RPS.Scissor
|
case 'C' => RPS.Scissor
|
||||||
case _ => return "Unexpected char found (pos 0)"
|
case _ => return "Unexpected char found (pos 0)"
|
||||||
,
|
,
|
||||||
line.charAt(2) match
|
line.charAt(2) match
|
||||||
case 'X' => Outcome.Lose
|
case 'X' => Outcome.Lose
|
||||||
case 'Y' => Outcome.Draw
|
case 'Y' => Outcome.Draw
|
||||||
case 'Z' => Outcome.Win
|
case 'Z' => Outcome.Win
|
||||||
case _ => return "Unexpected char found (pos 2)"
|
case _ => return "Unexpected char found (pos 2)"
|
||||||
)
|
)
|
||||||
|
|
||||||
val self_choice = outcome.get_choice(opponent_choice)
|
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"
|
s"part 2: $score"
|
||||||
|
|
||||||
|
@ -6,5 +6,3 @@ import scala.util.Random
|
|||||||
println("Advent of code 2022 with Scala")
|
println("Advent of code 2022 with Scala")
|
||||||
println(Day02.part_01())
|
println(Day02.part_01())
|
||||||
println(Day02.part_02())
|
println(Day02.part_02())
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user