optimize
This commit is contained in:
parent
77b6c259c6
commit
d59790e7d6
@ -35,7 +35,7 @@ const touching = (v1,v2) => [v1.distanceX(v2), v1.distanceY(v2)].every(
|
|||||||
export function moveOnce( head, tail, d ) {
|
export function moveOnce( head, tail, d ) {
|
||||||
head.add(d);
|
head.add(d);
|
||||||
|
|
||||||
if( touching(head,tail) ) return;
|
if( touching(head,tail) ) return tail.toString();
|
||||||
|
|
||||||
const delta = head.clone().subtract(tail).normalize();
|
const delta = head.clone().subtract(tail).normalize();
|
||||||
|
|
||||||
@ -45,26 +45,23 @@ export function moveOnce( head, tail, d ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tail.add(delta);
|
tail.add(delta);
|
||||||
|
|
||||||
|
return tail.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
const moveAround = (directives) => {
|
const moveAround = (directives) => {
|
||||||
const head = new Victor(0,0);
|
const head = new Victor(0,0);
|
||||||
const tail = new Victor(0,0);
|
const tail = new Victor(0,0);
|
||||||
|
|
||||||
const visited = [ tail.toString() ]
|
const move = d => moveOnce(head,tail,d);
|
||||||
|
return [
|
||||||
for ( const d of directives ) {
|
tail.toString(),
|
||||||
moveOnce(head,tail,d);
|
...directives.map( move )
|
||||||
|
]
|
||||||
visited.push( tail.toString() );
|
|
||||||
}
|
|
||||||
|
|
||||||
return visited;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default R.createPipe(
|
export default R.createPipe(
|
||||||
moveAround,
|
moveAround,
|
||||||
passthru( x => console.log(x) ),
|
|
||||||
R.uniq,
|
R.uniq,
|
||||||
R.countBy( R.identity )
|
moves => moves.length
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user