main
Yanick Champoux 2022-12-09 12:33:37 -05:00
parent 77b6c259c6
commit d59790e7d6
1 changed files with 9 additions and 12 deletions

View File

@ -35,7 +35,7 @@ const touching = (v1,v2) => [v1.distanceX(v2), v1.distanceY(v2)].every(
export function moveOnce( head, tail, d ) {
head.add(d);
if( touching(head,tail) ) return;
if( touching(head,tail) ) return tail.toString();
const delta = head.clone().subtract(tail).normalize();
@ -45,26 +45,23 @@ export function moveOnce( head, tail, d ) {
}
tail.add(delta);
return tail.toString();
}
const moveAround = (directives) => {
const head = new Victor(0,0);
const tail = new Victor(0,0);
const visited = [ tail.toString() ]
for ( const d of directives ) {
moveOnce(head,tail,d);
visited.push( tail.toString() );
}
return visited;
const move = d => moveOnce(head,tail,d);
return [
tail.toString(),
...directives.map( move )
]
};
export default R.createPipe(
moveAround,
passthru( x => console.log(x) ),
R.uniq,
R.countBy( R.identity )
moves => moves.length
);