Skip to content

Instantly share code, notes, and snippets.

@shintakezou
Last active May 20, 2019 11:56
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 shintakezou/d09684bd68ec4014a65644762098a1ef to your computer and use it in GitHub Desktop.
Save shintakezou/d09684bd68ec4014a65644762098a1ef to your computer and use it in GitHub Desktop.
Run two canoes to solve Italian's magazine puzzle "Quesito con la Susi n. 959"
class Canoa
{
private double pos; // m
private double vel; // m/s
public Canoa(double init_pos, double v) {
this.vel = v;
this.pos = init_pos;
}
public double getPosition() { return this.pos; }
public void next(double elapsed) {
this.pos += this.vel * elapsed;
}
}
// Quesito con la Susi n. 595
class Simulation
{
private final static double delta = 0.1; // time increment
private final static double speed = 1.0; // arbitrary speed
private final static double prec = 0.1; // for target distance comparison
// any value greater than the solution. Tune accordingly
private final static double max_dist_tests = 5000.0;
// min dist between piers.
private final static double min_dist = 600.0 + 400.0; // was 500 for test
// this is chosen supposing the distance will match min_dist + N*dist_incr
//
// for the chosen value and the results, it could be 100; but we need
// to reduce it in case the solution is something like 2012.
// If we choose wrong values, we could miss the target distance and have
// no results - unless we increase prec to, say, 100.0... then we would have
// a broad interval for the solution, which will be given by the output result + alpha*prec)
//
// config values should be chosen thoughtfully, and tuning by trial might be needed
private final static double dist_incr = 10.0;
private static boolean approx_pos(Canoa c, double d) {
return (c.getPosition() >= d - prec/2.0) &&
(c.getPosition() <= d + prec/2.0);
}
private static void Simulation() {
double cur_dist;
for (cur_dist = min_dist;
cur_dist < max_dist_tests;
cur_dist += dist_incr)
{
Canoa susi = new Canoa(600.0, speed); // 600 is from where Susi starts
Canoa gianni = new Canoa(cur_dist, -2.0*speed); // Gianni's speed's 2 times Susi's speed
while (!approx_pos(gianni, 0.0)) {
gianni.next(delta);
susi.next(delta);
}
if (approx_pos(susi, cur_dist - 400.0)) {
System.out.println(cur_dist);
break;
}
}
}
public static void main(String[] args) {
Simulation();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment