Robots & Lasers!
a js solution
by ItsLeeOwen
HTML
<a href="http://www.puzzlenode.com/puzzles/4-robots-vs-lasers">Robots v Lasers</a>
<hr />
<output>
<div ng-repeat="command in commands">{{command}}</div>
</output>
JavaScript
if (console.clear)
console.clear();
angular.module('app', []).directive('output', function() {
return {restrict: 'E', controller: function($scope, $http) {
$scope.commands = [];
$http.get("https://dl.dropboxusercontent.com/u/7218835/codernight/lasersandrobots/input.txt")
.success(function(input) {
var conveyors = input.split("\n\n");
for (var i=0; i<conveyors.length; i++ )
$scope.commands.push(exit(conveyors[i].split("\n")));
function exit(conveyor) {
var X = conveyor[1].indexOf('X'),
parity = X % 2,
west = 0,
east = 0,
tick = function(i) {
if (parity == (i % 2) && '|' == conveyor[0][i])
return 1;
else if (parity != (i % 2) && '|' == conveyor[2][i])
return 1;
return 0;
};
for (var i=0; i<conveyor[1].length; i++) {
if (i <= X)
west += tick(i);
else if (west == 0)
break;
if (i >= X)
east += tick(i);
}
return west > east ? 'GO EAST' : 'GO WEST';
}
});
}
}
});