This commit is contained in:
Yanick Champoux 2022-12-09 12:33:37 -05:00
parent 77b6c259c6
commit d59790e7d6

View File

@ -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
); );