Skip to content

Instantly share code, notes, and snippets.

@falsefalse
Last active January 2, 2016 07:19
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 falsefalse/8268869 to your computer and use it in GitHub Desktop.
Save falsefalse/8268869 to your computer and use it in GitHub Desktop.
BQ
/*
* Do it.
*
*/
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in the direction of movement
dir && th[dir](!!0);
// launch new course, save it
dir && th[ _reverse(dir) ](!!1);
_course = dir;
}
var dirs = ['right', 'bottom', 'left', 'top']; // [0, 90, 180, 270];
function _to_a(dir) { return dirs.indexOf(dir) * 90; }
function _to_dir(a) {
a %= 360;
if (a < 0) a = 360 + a;
return dirs[ ~~(a / 90) ]
}
function _reverse(dir) {
return _to_dir( (_to_a(dir) + 180) % 360 );
}
var walls = {};
function _not_blocked(dir) { return !walls[dir]; }
function update_walls(side, c) { walls[side] = ~~c; }
function _turn(block) {
return _to_dir( _to_a(block) + 90 );
}
function change_course(side, contact) {
if (contact) {
var turn = _turn(side);
while (!_not_blocked(turn)) turn = _turn(turn);
course( turn );
}
else if (side != _course && side != _reverse(_course)) {
course(side);
}
}
this.on('all', function() {
var args = [].slice.call(arguments, 0),
match = args[0].match(/sensor\:(.+)$/);
if (match && match[1]) {
update_walls (match[1], args[1]);
change_course(match[1], args[1]);
}
});
course('right');
/*
* Open all three doors to exit.
*
* The answer is ?.
*/
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in the direction of movement
dir && th[dir](!!0);
// launch new course, save it
dir && th[ _reverse(dir) ](!!1);
_course = dir;
}
var dirs = ['right', 'bottom', 'left', 'top']; // [0, 90, 180, 270];
function _to_a(dir) { return dirs.indexOf(dir) * 90; }
function _to_dir(a) {
a %= 360;
if (a < 0) a = 360 + a;
return dirs[ ~~(a / 90) ];
}
function _reverse(dir) {
return _to_dir( (_to_a(dir) + 180) % 360 );
}
course('top');
function start(c) {
if (c)
course('right');
else {
course('top');
pod.radar.angle(0);
pod.radar.ping();
pod.off('sensor:top', start);
}
}
pod.on('sensor:top', start);
function swallow(times, tuple) {
return function(c, r, d) { if (!--times) return tuple; };
}
function check(c, r, d) {
course();
pod.radar.angle(90);
pod.radar.ping();
return -1;
}
function press_move(course, radar) {
return [
// press and check
check,
['top', radar],
['bottom', radar],
check,
// swing to next button in said course
[course, 'top'],
swallow(1, [course, 'top'])
];
}
var path = [
['bottom', 'right'],
['right', 'top'],
swallow(4, ['right', 'top'])
].concat(
press_move('left' , 'right'),
press_move('left' , 'right'),
press_move('right', 'left' ),
press_move('right', 'left' ),
press_move('left' , 'right'),
press_move('left' , 'right'),
press_move('right', 'left'),
check
);
var bt = [];
function detect(bt) {
var cont = 1, prev;
if (bt.length < 4) return false;
for (var i = bt.length - 1; i > -1; i--) {
prev = bt[i-1];
if (bt[i] == prev) cont++;
else break;
}
return (cont > 4 && cont != bt.length);
}
function wall_automata(distance) {
var radar = _to_dir(pod.radar.angle()),
command = path.shift(), ret;
if (typeof command == 'function') {
ret = command(_course, radar, distance);
if (ret && ret.length >= 0)
command = ret;
else if (ret == -1)
command = path.shift();
else
path.unshift(command);
}
if (command && command.length == 2) {
course(command[0]);
pod.radar.angle(_to_a(command[1]));
}
}
var c = pod.cannon;
pod.on('radar:hit radar:miss', function(a, d) {
if (!d) {
course('bottom');
pod.radar.angle(270);
}
// filter it up in 'dis bitch
if (a != pod.radar.angle()) return;
if (bt.push(d) > 8) bt.shift();
if (detect(bt) ||
(d <= 10 && _course == _to_dir(pod.radar.angle())) ) {
wall_automata(d);
bt = [];
}
pod.radar.ping();
});
/*
* Do it.
*
*/
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in the direction of movement
dir && th[dir](!!0);
// launch new course, save it
dir && th[ _reverse(dir) ](!!1);
_course = dir;
}
var dirs = ['right', 'bottom', 'left', 'top']; // [0, 90, 180, 270];
function _to_a(dir) { return dirs.indexOf(dir) * 90; }
function _to_dir(a) {
a %= 360;
if (a < 0) a = 360 + a;
return dirs[ ~~(a / 90) ]
}
function _reverse(dir) { return _to_dir( (_to_a(dir) + 180) % 360 ); }
function _turn_ccw(block) { return _to_dir( _to_a(block) + 90 ); }
function _turn_cw (block) { return _to_dir( _to_a(block) - 90 ); }
var bt = [];
function detect(bt) {
if (bt.length < 3) return false;
for (var i = bt.length - 1, cont = 1; i > -1; i--) {
if (bt[i] == bt[i-1]) cont++;
else break;
}
return (cont > 2 && cont != bt.length);
}
this.on('radar:hit radar:miss', function(a, d) {
if (!d) {
course('right');
return;
}
var wall = _turn_cw(_course);
if ( a == _to_a(_course) ) {
if (d < 12) {
var turn = _turn_ccw(_course);
course(turn);
pod.radar.angle(_to_a(turn));
}
pod.radar.angle( _to_a(wall) );
}
else {
if (bt.push(d) > 8) bt.shift();
if ( d > 100 && detect(bt) ) {
course( _to_dir(a) );
pod.radar.angle( _to_dir(a) );
bt = [];
}
pod.radar.angle( _to_a(_course) );
}
pod.radar.ping();
});
pod.radar.ping();
/*
* You have a cannon object available to you which can send out projectiles.
* The projectiles don't move terribly quickly but they will destroy your enemy
* if they hit.
*
* The cannon object attached to this has the following methods:
*
* cannon.aim() - returns the current direction the cannon is pointing.
* cannon.aim(number) - points the cannon in the supplied direction.
* cannon.fire() - fires a projectile.
* cannon.ready() - tests to see if the cannon can be fired again. The cannon
* requires a cooldown period after each firing.
*
*
* Go up the elevator.
*/
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in the direction of movement
dir && th[dir](!!0);
// launch new course, save it
dir && th[ _reverse(dir) ](!!1);
_course = dir;
}
var dirs = ['right', 'bottom', 'left', 'top']; // [0, 90, 180, 270];
function _to_a(dir) { return dirs.indexOf(dir) * 90; }
function _to_dir(a) {
a %= 360;
if (a < 0) a = 360 + a;
return dirs[ ~~(a / 90) ]
}
function _reverse(dir) { return _to_dir( (_to_a(dir) + 180) % 360 ); }
function _turn_ccw(block) { return _to_dir( _to_a(block) + 90 ); }
function _turn_cw (block) { return _to_dir( _to_a(block) - 90 ); }
var bt = [], pings_for_turn = 4;
function detect(bt) {
if (bt.length < 3) return false;
for (var i = bt.length - 1, cont = 1; i > -1; i--) {
if (bt[i] == bt[i-1]) cont++;
else break;
}
var diff = bt[bt.length - 1] - bt[ (i == 0) ? 0 : i - 1 ];
return (cont > pings_for_turn && cont != bt.length && Math.abs(diff) > 10);
}
this.on('radar:hit radar:miss', function(a, d) {
if (!d) {
pod.radar.ping();
return;
}
var wall = _turn_cw(_course);
if ( a == _to_a(_course) ) {
if (d < 12) {
var turn = _turn_ccw(_course);
course(turn);
pod.radar.angle(_to_a(turn));
}
pod.radar.angle( _to_a(wall) );
}
else {
if (bt.push(d) > 8) bt.shift();
if ( d >= 10 && detect(bt) ) {
course( _to_dir(a) );
pod.radar.angle( _to_dir(a) );
if (--pings_for_turn == 2) --pings_for_turn;
if (pings_for_turn == 0) pings_for_turn = 3
bt = [];
}
pod.radar.angle( _to_a(_course) );
}
pod.radar.ping();
var c = pod.cannon;
if (c.ready()) { c.aim( _to_a('top') ); c.fire(); }
});
course('right')
pod.radar.ping();
console.log(this.radar);
/*
* Destroy the other bot before it destroys you.
*
*/
// same as 13th with some inane tweaking
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in the direction of movement
dir && th[dir](!!0);
// launch new course, save it
dir && th[ _reverse(dir) ](!!1);
_course = dir;
}
var dirs = ['right', 'bottom', 'left', 'top']; // [0, 90, 180, 270];
function _to_a(dir) { return dirs.indexOf(dir) * 90; }
function _to_dir(a) {
a %= 360;
if (a < 0) a = 360 + a;
return dirs[ ~~(a / 90) ]
}
function _reverse(dir) { return _to_dir( (_to_a(dir) + 180) % 360 ); }
function _turn_ccw(block) { return _to_dir( _to_a(block) + 90 ); }
function _turn_cw (block) { return _to_dir( _to_a(block) - 90 ); }
var bt = [], pings_for_turn = 4;
function detect(bt) {
if (bt.length < 3) return false;
for (var i = bt.length - 1, cont = 1; i > -1; i--) {
if (bt[i] == bt[i-1]) cont++;
else break;
}
var diff = bt[bt.length - 1] - bt[ (i == 0) ? 0 : i - 1 ];
return (cont > pings_for_turn && cont != bt.length && Math.abs(diff) > 10);
}
this.on('radar:hit radar:miss', function(a, d) {
if (!d) {
pod.radar.ping();
return;
}
var wall = _turn_cw(_course);
if ( a == _to_a(_course) ) {
if (d < 12) {
var turn = _turn_ccw(_course);
course(turn);
pod.radar.angle(_to_a(turn));
}
pod.radar.angle( _to_a(wall) );
}
else {
if (bt.push(d) > 8) bt.shift();
if ( d >= 10 && detect(bt) ) {
course( _to_dir(a) );
pod.radar.angle( _to_dir(a) );
if (--pings_for_turn == 2) --pings_for_turn;
if (pings_for_turn == 0) pings_for_turn = 3
bt = [];
}
pod.radar.angle( _to_a(_course) );
}
pod.radar.ping();
var c = pod.cannon;
if (c.ready()) { c.aim( c.aim() + 90); c.fire(); }
});
course('bottom')
pod.radar.ping();
var t = this.thrusters;
var walls = { top: 0, left: 0, bottom: 0, right: 0 };
function update_walls(side, contact) {
walls[side] = ~~contact;
}
var _course;
function course(way, aux) {
t[way](!!0); // don't thrust in direction of movement
_course = way;
t[ _reverse(way) ](!!1);
//if (aux) course(aux);
}
var W = [ 'top', 'right', 'bottom', 'left' ];
function _reverse(side) {
var i = W.indexOf(side);
return W[ (i + 2) % W.length ];
}
function _turns(block) {
var
turns = [],
i = W.indexOf(block),
next = i + 1;
if (next >= W.length) next = 0;
if (next < 0) next = W.length - Math.abs(next);
next = W[next];
if (_not_blocked(next))
return next;
else if (_not_blocked(_reverse(next)))
return _reverse(next)
}
function _not_blocked(dir) {
return !walls[dir];
}
function change_course(side, contact) {
var turns;
if (contact) {
t[ _reverse(side) ](!!0);
course( _turns(side) );
}
else if (side != _course && side != _reverse(_course)) {
t[ _reverse(_course) ](!!0);
course(side);
}
}
this.on('all', function() {
var args = [].slice.call(arguments, 0),
match = args[0].match(/sensor\:(.+)$/);
if (match && match[1]) {
update_walls (match[1], args[1]);
change_course(match[1], args[1]);
}
});
course('right');
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in direction of movement
dir && th[dir](!!0);
// launch new course, save it
dir && th[ _reverse(dir) ](!!1);
_course = dir;
}
function _reverse(dir) {
if (dir == 'top') return 'bottom';
if (dir == 'bottom') return 'top';
if (dir == 'left') return 'right';
if (dir == 'right') return 'left';
}
var path = [
{
'bottom': { true: 'right' },
'right': { true: 'top.' }
},
{
'left': { true: 'bottom', false: 'left.' }
},
{
'top': { true: '.' }
},
{
'left': { true: 'top' },
'top': { true: 'right' },
'right': { true: 'bottom', false: '.' }
},
{
'left': { true: 'top', false: 'right.' }
},
{
'right': { true: 'top', false: 'right' }
}
];
var segment = path.shift();
function automata(dir, contact) {
if (segment[dir] && segment[dir][contact]) {
var command = segment[dir][contact];
if (/\.$/.test(command)) {
segment = path.shift();
command = command.replace('.', '');
}
if (command != '') course(command);
}
}
course('bottom');
pod.on('all', function() {
var args = [].slice.call(arguments, 0),
match = args[0].match(/sensor\:(.+)$/);
if (match && match[1]) {
var dir = match[1], contact = args[1];
automata(dir, contact);
}
});
/*
* Not only does your robot come equipped with sensors and thrusters. It also
* has a radar that can be used to determine distances.
*
* The radar has two methods:
*
*
* - angle() - the current direction the radar is pointing (0-359)
* - angle(number) - set the radar direction (0-359)
*
* - ping() - fires the radar
*
* One of two events will return after firing the radar:
* - 'radar:hit' - an object was found
* - 'radar:miss' - no object was found
*
* When a hit event is received, the handler will receive the angle the
* ping was sent out on and the distance to the object, e.g.,
* this.on('radar:hit', function(angle, distance) {
* // do stuff
* });
*
* Bonus info:
*
* Those red jumpy things will kill your robot. Don't touch them.
*/
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in direction of movement
th[dir](!!0);
// launch new course, save it
th[ _reverse(_course = dir) ](!!1);
//pod.radar.angle( angles[_course] );
return dir;
}
function _reverse(dir) {
if (dir == 'top') return 'bottom';
if (dir == 'bottom') return 'top';
if (dir == 'left') return 'right';
if (dir == 'right') return 'left';
}
var angles = {
'top': -90,
'bottom': 90,
'left': 180,
'right': 0
};
function _angle_to_dir(angle) {
for (var d in angles) {
if (angles[d] === angle) return d;
}
}
var bt = [], changed = 0;
function head_looking(dir, angle) {
bt = [], changed = 0;
course(dir);
pod.radar.angle( angles[angle] );
}
head_looking('right', 'bottom');
this.on('radar:hit radar:miss', function(angle, dist) {
dist = dist || Math.Infinity;
bt.push(dist);
if (bt.length > 8) bt.shift();
if (bt.length >= 8) {
if (!dist || dist > bt[bt.length - 8])
changed += 1;
else
changed = 0;
}
if (changed >= 4) {
// first gate
if (angle == 90) head_looking(_angle_to_dir(angle), 'right');
// exit
if (!dist && angle == 0) {
course(_angle_to_dir(angle));
return;
}
}
this.radar.ping();
});
this.radar.ping();
/*
* This other bot is helping-- somewhat.
*/
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in direction of movement
dir && th[dir](!!0);
// launch new course, save it
dir && th[ _reverse(dir) ](!!1);
_course = dir;
}
function _reverse(dir) {
if (dir == 'top') return 'bottom';
if (dir == 'bottom') return 'top';
if (dir == 'left') return 'right';
if (dir == 'right') return 'left';
}
var angles = {
'top': -90,
'bottom': 90,
'left': 180,
'right': 0
};
course('top');
pod.on('sensor:top', function(c) {
if (c) course('right')
pod.radar.angle(angles['right']);
pod.radar.ping();
});
pod.on('radar:hit radar:miss', function(a, d) {
d = d || Math.Infinity;
if (d <= 4)
course()
else
course('right');
pod.radar.ping();
});
/*
* Open all three doors to exit.
*
* The answer is 5.
*/
var th = this.thrusters, pod = this;
var _course;
function course(dir) {
// shutdown current engine
_course && th[ _reverse(_course) ](!!0);
// don't thrust in the direction of movement
dir && th[dir](!!0);
// launch new course, save it
dir && th[ _reverse(dir) ](!!1);
_course = dir;
}
var dirs = ['right', 'bottom', 'left', 'top']; // [0, 90, 180, 270];
function _to_a(dir) { return dirs.indexOf(dir) * 90; }
function _to_dir(a) {
a %= 360;
if (a < 0) a = 360 + a;
return dirs[ ~~(a / 90) ]
}
function _reverse(dir) {
return _to_dir( (_to_a(dir) + 180) % 360 );
}
course('top');
function start(c) {
if (c)
course('right');
else {
course('top');
pod.radar.angle(0);
pod.radar.ping();
pod.off('sensor:top', start);
}
}
pod.on('sensor:top', start);
var path = [
// 'dir': { 'radar' : ['dir', 'radar'] }
{
'top': {
'right': ['right', 'top']
},
'right': {
'top': ['top', 'right', '.']
}
},
{
'top': {
'right': ['right', 'top']
},
'right': {
'top': swallow(4, ['bottom', 'right', '.'])
}
},
{
'bottom': {
'right': swallow(2, ['left', 'bottom'])
},
'left': {
'bottom': ['bottom', 'bottom', '.']
}
}
];
function swallow(times, course) {
return function(c, r, d) { if (!--times) return course; }
}
var segment = path.shift(), bt = [];
function detect(bt) {
console.log(bt);
var cont = 1, prev, last = bt[bt.length - 1];
if (bt.length <= 3) return false;
for (var i = bt.length - 1; i > -1; i--) {
prev = bt[i-1];
if (bt[i] == prev)
cont++;
else
break;
}
if (cont > ~~(bt.length / 2) && cont != bt.length) {
return true;
}
}
function wall_automata(distance) {
var a = pod.radar.angle(),
last = bt[bt.length - 1],
radar = _to_dir(a);
if (!segment) return;
if (segment[_course] && segment[_course][radar]) {
var command = segment[_course][radar];
if (typeof command == 'function') {
command = command(_course, radar, distance);
}
if (!command) return;
course(command[0]);
pod.radar.angle(_to_a(command[1]));
if (command[2]) segment = path.shift();
}
}
pod.on('radar:hit radar:miss', function(a, d) {
d = d || Math.Infinity;
bt.push(d);
if (bt.length >= 8) bt.shift();
if (detect(bt) ||
(d <= 10 && _course == _to_dir(pod.radar.angle())) ) {
wall_automata(d);
bt = [];
}
pod.radar.ping();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment