From d59790e7d680afa4147d8256fda6d90a3c1a5d31 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 9 Dec 2022 12:33:37 -0500 Subject: [PATCH] optimize --- 2022/09/part1.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/2022/09/part1.js b/2022/09/part1.js index 3027d39..6a35258 100644 --- a/2022/09/part1.js +++ b/2022/09/part1.js @@ -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 );