diff --git a/2022/day2-v2.scala b/2022/day2-v2.scala index 43a6f1d..1f74413 100644 --- a/2022/day2-v2.scala +++ b/2022/day2-v2.scala @@ -1,16 +1,18 @@ import scala.io.Source import scala.math.floorMod +extension (i: Int) def %%(modulus: Int) = floorMod(i, modulus) + enum RPS(val score: Int): case Rock extends RPS(1) case Paper extends RPS(2) case Scissors extends RPS(3) - def +(amount: Int) = RPS.fromOrdinal(floorMod(ordinal + amount, 3)) - def -(amount: Int) = RPS.fromOrdinal(floorMod(ordinal - amount, 3)) + def +(amount: Int) = RPS.fromOrdinal((ordinal + amount) %% 3) + def -(amount: Int) = RPS.fromOrdinal((ordinal - amount) %% 3) def -(other: RPS) = score - other.score - def vs(other: RPS) = floorMod(this - other, 3) match + def vs(other: RPS) = (this - other) %% 3 match case 1 => 6 case 2 => 0 case _ => 3