refactoring
This commit is contained in:
parent
12e65157e2
commit
85a70f1767
@ -2,7 +2,7 @@ import fs from "fs-extra";
|
|||||||
import fp from "lodash/fp.js";
|
import fp from "lodash/fp.js";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
|
||||||
import * as p1 from './part1.mjs';
|
import * as p1 from "./part1.mjs";
|
||||||
|
|
||||||
function completionString(line) {
|
function completionString(line) {
|
||||||
const stack = [];
|
const stack = [];
|
||||||
@ -10,35 +10,33 @@ function completionString(line) {
|
|||||||
for (const c of line) {
|
for (const c of line) {
|
||||||
if (p1.closing[c]) {
|
if (p1.closing[c]) {
|
||||||
stack.unshift(p1.closing[c]);
|
stack.unshift(p1.closing[c]);
|
||||||
} else {
|
} else if (c !== stack.shift()) {
|
||||||
if(c !== stack.shift()) return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return stack;
|
return stack;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function linesScore(lines) {
|
export function linesScore(lines) {
|
||||||
lines = lines.map( completionString ).filter(x=>x);
|
lines = lines.map(completionString).filter((x) => x);
|
||||||
|
|
||||||
const points = {
|
const points = {
|
||||||
')': 1,
|
")": 1,
|
||||||
']': 2,
|
"]": 2,
|
||||||
'}': 3,
|
"}": 3,
|
||||||
'>': 4,
|
">": 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);
|
return lines.map(lineScore);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function solution(lines) {
|
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 < b ? -1 : 1));
|
||||||
|
|
||||||
return scores[(scores.length - 1) / 2];
|
return scores[(scores.length - 1) / 2];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user