day 2 part 1
This commit is contained in:
parent
a7e0e74fee
commit
232efd2a10
47
src/main/scala/Day02.scala
Normal file
47
src/main/scala/Day02.scala
Normal 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"
|
||||||
|
|
@ -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())
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user