Skip to content

Instantly share code, notes, and snippets.

@sangkeon
Last active April 1, 2022 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sangkeon/2ea2bb355ef23fdeaa902611c83dc2bf to your computer and use it in GitHub Desktop.
Save sangkeon/2ea2bb355ef23fdeaa902611c83dc2bf to your computer and use it in GitHub Desktop.
case class TargetArea(x1:Int, x2:Int, y1:Int, y2:Int)
val targetArea = TargetArea(124,174,-123,-86) //TargetArea(20, 30, -10, -5)
case class Probe(x:Int, y:Int, maxY:Int)
case class Velocity(x:Int, y:Int)
def nextPosition(p: Probe, v: Velocity) : (Probe, Velocity) = {
val nvx = nextXV(v.x)
val nvy = nextYV(v.y)
// println("next p ..." + (p.x + v.x) + " ... " + (p.y + v.y))
// println("next velo ..." + nvx + " ... " + nvy)
val maxy = math.max(p.maxY, p.y + v.y)
(Probe(p.x + v.x, p.y + v.y, maxy), Velocity(nvx, nvy))
}
def nextXV(x:Int):Int = {
if ( x >= 1) x - 1
else if ( x <= -1) x + 1
else 0
}
def nextYV(y:Int):Int = {
y - 1
}
def findY(p:Probe, v: Velocity): Option[(Int,Int)] = {
val next = nextPosition(p, v)
if (next._1.x > targetArea.x2 || next._1.y < targetArea.y1) None
else if ( next._1.x >= targetArea.x1 && next._1.x <= targetArea.x2 &&
next._1.y >= targetArea.y1 && next._1.y <= targetArea.y2
) Some(next._1.y, next._1.maxY)
else findY(next._1, next._2)
}
object Main extends App {
val q =
for (
x <- 0 to targetArea.x2;
y <- targetArea.y1 to -targetArea.y1;
if findY(Probe(0,0,0), Velocity(x,y)) != None
) yield findY(Probe(0,0,0), Velocity(x,y)).get
val r =
for (
x <- 0 to targetArea.x2;
y <- targetArea.y1 to -targetArea.y1;
if findY(Probe(0,0,0), Velocity(x,y)) != None
) yield (x,y)
val miny = q.map(_._1).min
println("q1=" + q.filter(_._1 == miny).map(_._2).max)
println("q2=" + r.length)
// ymin = q.map(x -> x._1).min
//
// println(q.max)
//
// println(q.flatten)
//
//
// println(findY(Probe(0,0), Velocity(7,2)))
// println(findY(Probe(0,0), Velocity(6,3)))
// println(findY(Probe(0,0), Velocity(9,0)))
// println(findY(Probe(0,0,0), Velocity(6,9)))
// println(findY(Probe(0,0), Velocity(17,-4)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment