From 85a70f17674346ea2c03d2202cbe7a05394ca3eb Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 10 Dec 2021 13:39:15 -0500 Subject: [PATCH] refactoring --- 2021/10/part2.mjs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/2021/10/part2.mjs b/2021/10/part2.mjs index 2029fc8..d0003f6 100644 --- a/2021/10/part2.mjs +++ b/2021/10/part2.mjs @@ -2,43 +2,41 @@ import fs from "fs-extra"; import fp from "lodash/fp.js"; import _ from "lodash"; -import * as p1 from './part1.mjs'; +import * as p1 from "./part1.mjs"; function completionString(line) { const stack = []; - for ( const c of line ) { - if( p1.closing[c] ) { + for (const c of line) { + if (p1.closing[c]) { stack.unshift(p1.closing[c]); - } else { - if(c !== stack.shift()) return; + } else if (c !== stack.shift()) { + return; } } return stack; - } export function linesScore(lines) { - lines = lines.map( completionString ).filter(x=>x); + lines = lines.map(completionString).filter((x) => x); const points = { - ')': 1, - ']': 2, - '}': 3, - '>': 4, - } + ")": 1, + "]": 2, + "}": 3, + ">": 4, + }; - const lineScore = line => line.reduce( (a,b) => 5*a + points[b], 0 ); + const lineScore = (line) => line.reduce((a, b) => 5 * a + points[b], 0); return lines.map(lineScore); } export function solution(lines) { + const scores = linesScore(lines); - const scores =linesScore(lines); + scores.sort((a, b) => (a < b ? -1 : 1)); - scores.sort( (a,b) => a