Skip to content

Instantly share code, notes, and snippets.

@Insolita
Created December 8, 2017 17:39
Show Gist options
  • Save Insolita/cc8c4f2c46d701ecc0a4b2f8e3cd101a to your computer and use it in GitHub Desktop.
Save Insolita/cc8c4f2c46d701ecc0a4b2f8e3cd101a to your computer and use it in GitHub Desktop.
firstvds_full.js
let mind=(D)=>{
let my = D.john.position,nx = D.nexa.position,inf=D.infection;
storage.firstGoal = storage.firstGoal||0;
storage.nexa_catch=storage.nexa_catch||0;
let A = v => Math.abs(v);
let B = v => v.length;
let nearOneStep = (it) =>((my.x === it.x && nearNextRowY(it)) || (my.y === it.y && nearNextRowX(it)));
let nearNextRowY = (it) => (A(my.y-it.y)===1);
let nearNextRowX = (it) => (A(my.x-it.x)===1);
let directionByX = (x, y)=> (my.x !== x)? (my.x > x ? 'left' : 'right') :(my.y > y ? 'up' : 'down');
let directionByY = (x, y)=> (my.y !== y)? (my.y > y ? 'up' : 'down') :(my.x > x ? 'left' : 'right');
let isCurrentInfected = ()=>inf.some((it) =>(my.x === it.x && my.y === it.y));
let resolveDirection = (x, y)=>(!isNexaCatch()&& !storage.firstGoal)?directionByY(x,y):directionByX(x,y);
let isNexaCatch = () =>{if (my.x===nx.x && my.y===nx.y){storage.nexa_catch += 1;} return (storage.nexa_catch >0);};
let isNearNexa = () => (my.x===nx.x);
let pipe= v =>v.reduce((res, it)=>{if(B(res)===0){res = inf.filter(it);}return res;}, []);
let nearestTarget = v=>B(v)?v[0]:[];
let furtherNexaTarget = () => {
return inf.reduce((max, item) => {
let distance = (A(nx.x - item.x) + A(nx.y - item.y));
if (max.dst < distance) {
max.dst = distance;
max.coords = item;
}
return max;
}, {'dst': 0, 'coords': storage.target});
};
let resolveTarget = () => {
if(storage.firstGoal===0){
target = inf[0];
}else if (!isNexaCatch() && storage.firstGoal) {
target = nx;
}else if (storage.target && nx.y!==1 && B(inf)>=2){
target = furtherNexaTarget().coords;
}else{
target = pipe([nearOneStep,(it) => (my.y === it.y),nearNextRowY]);
target = B(target)? nearestTarget(target):furtherNexaTarget().coords;
}
storage.target = target;
return resolveDirection(target.x, target.y);
};
let isCurable = () => {
if (isCurrentInfected() && !(!isNexaCatch() && isNearNexa())) {
storage.target = null;
storage.firstGoal=1;
return true;
}
return false;
};
return isCurable()?'cure':resolveTarget();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment