day 2 part 1

main
Araozu 2023-10-31 21:42:07 -05:00
parent a7e0e74fee
commit 232efd2a10
2 changed files with 49 additions and 2 deletions

View File

@ -0,0 +1,47 @@
import scala.util.Success
import scala.util.Failure
enum RPS:
case Rock, Paper, Scissor
def get_score() = this match
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
object Day02 extends Solution:
override def part_01() =
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, 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)"
)
score += self_choice.get_score() + self_choice.get_match_score(opponent_choice)
s"part 2: $score"
override def part_02() = "part 2: :c"

View File

@ -4,7 +4,7 @@ import scala.util.Random
// Number guessing game // Number guessing game
@main def hello: Unit = @main def hello: Unit =
println("Advent of code 2022 with Scala") println("Advent of code 2022 with Scala")
println(Day01.part_01()) println(Day02.part_01())
println(Day01.part_02()) println(Day02.part_02())