Move scala codebase in a directory on its own

This commit is contained in:
Xavier Morel
2020-12-03 10:23:51 +01:00
parent a04451f2f7
commit 7dc5d839af
36 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
import scala.io.StdIn.readInt
val MAX_X = 300
val MAX_Y = 300
val SQ_SIZE = 3
case class Pos(x: Int, y: Int)
def cellPower(serial: Int)(p: Pos): Int = {
val rackId = p.x + 10
(((((rackId * p.y) + serial) * rackId) / 100) % 10) - 5
}
// TODO Should not recompute stuff each time
def squarePower(serial: Int)(p: Pos): Int = {
(p.x to (p.x + SQ_SIZE - 1)).flatMap(x => (p.y to (p.y + SQ_SIZE - 1)).map(y => Pos(x, y))).map(cellPower(serial)).sum
}
/*
println(cellPower(57)(Pos(122, 79)) + " expected -5")
println(cellPower(39)(Pos(217, 196)) + " expected 0")
println(cellPower(71)(Pos(101, 153)) + " expected 4")
println(squarePower(18)(Pos(33,45)) + " expected 29")
// */
val input = readInt
val maxCell = (1 to (MAX_X - SQ_SIZE)).flatMap(x => (1 to (MAX_Y - SQ_SIZE)).map(y => Pos(x, y))).maxBy(squarePower(input))
println(maxCell)