Skip to content

Instantly share code, notes, and snippets.

@JustinAzoff
Created October 15, 2021 20:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinAzoff/7a4a52f87ba5c3b452c55a5ae68839f3 to your computer and use it in GitHub Desktop.
Save JustinAzoff/7a4a52f87ba5c3b452c55a5ae68839f3 to your computer and use it in GitHub Desktop.
function overlaps(a: Pin, b: Pin): bool
{
print "Check overlap", a,b;
if (a$stop <= b$start)
return F;
if (a$start >= b$stop)
return F;
return T;
}
function can_pin(lower: Pin, pins: vector of Pin): bool
{
#print lower, "in", pins, "?";
for(p in pins) {
#print pins[p];
if(overlaps(lower, pins[p])) {
print lower, "overlaps with", p, pins[p];
return T;
}
}
return F;
}
function try(lock: vector of Lock::Tumbler) {
local lower = lock[0]$pins[0];
print lower;
for(p in lock) {
if (p == 0) next;
#print lock[p];
if (!can_pin(lower, lock[p]$pins))
return;
}
for(p in lock) {
print lock[p];
}
print "Can solve?!";
print "Spins=", Lock::spins;
terminate();
print "";
}
event Lock::pick(lock: vector of Lock::Tumbler) {
try(lock);
####
#
# YOU MAY IT USEFUL TO FILL OUT THIS EVENT'S BODY
#
####
# pick again
Lock::ticks += 1msec;
schedule 1msec { Lock::pick(lock) };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment