working!
This commit is contained in:
parent
2967c0e9d3
commit
12e65157e2
@ -2,7 +2,45 @@ import fs from "fs-extra";
|
||||
import fp from "lodash/fp.js";
|
||||
import _ from "lodash";
|
||||
|
||||
export const processInput = (input) => input;
|
||||
export const processInput = (file) => fs.readFile(file,'utf8').then(
|
||||
content => content.split("\n").filter(x=>x).map(
|
||||
line => line.split('')
|
||||
)
|
||||
);
|
||||
|
||||
export function solution(input) {
|
||||
export const closing = {
|
||||
'{':'}',
|
||||
'<': ">",
|
||||
'(': ')',
|
||||
'[': ']',
|
||||
};
|
||||
|
||||
function firstIllegalCharacter(line) {
|
||||
const stack = [];
|
||||
|
||||
for ( const c of line ) {
|
||||
if( closing[c] ) {
|
||||
stack.push(closing[c]);
|
||||
} else {
|
||||
if(c !== stack.pop()) return c;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
export function solution(lines) {
|
||||
const points = {
|
||||
')': 3,
|
||||
']': 57,
|
||||
'}': 1197,
|
||||
'>': 25137,
|
||||
}
|
||||
|
||||
return _.sum(
|
||||
lines.map( firstIllegalCharacter ).map(
|
||||
c => points[c] || 0
|
||||
)
|
||||
)
|
||||
}
|
||||
|
@ -4,5 +4,41 @@ import _ from "lodash";
|
||||
|
||||
import * as p1 from './part1.mjs';
|
||||
|
||||
export function solution(input) {
|
||||
function completionString(line) {
|
||||
const stack = [];
|
||||
|
||||
for ( const c of line ) {
|
||||
if( p1.closing[c] ) {
|
||||
stack.unshift(p1.closing[c]);
|
||||
} else {
|
||||
if(c !== stack.shift()) return;
|
||||
}
|
||||
}
|
||||
|
||||
return stack;
|
||||
|
||||
}
|
||||
|
||||
export function linesScore(lines) {
|
||||
lines = lines.map( completionString ).filter(x=>x);
|
||||
|
||||
const points = {
|
||||
')': 1,
|
||||
']': 2,
|
||||
'}': 3,
|
||||
'>': 4,
|
||||
}
|
||||
|
||||
const lineScore = line => line.reduce( (a,b) => 5*a + points[b], 0 );
|
||||
|
||||
return lines.map(lineScore);
|
||||
}
|
||||
|
||||
export function solution(lines) {
|
||||
|
||||
const scores =linesScore(lines);
|
||||
|
||||
scores.sort( (a,b) => a<b?-1:1 );
|
||||
|
||||
return scores[ (scores.length-1) / 2 ];
|
||||
}
|
||||
|
10
2021/10/sample
Normal file
10
2021/10/sample
Normal file
@ -0,0 +1,10 @@
|
||||
[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]
|
@ -10,11 +10,14 @@ const sample = p1.processInput('sample');
|
||||
const input = p1.processInput('input');
|
||||
|
||||
tap.test("part1", async (t) => {
|
||||
t.equal(p1.solution(await sample), 0);
|
||||
// t.equal(p1.solution(await input), 0);
|
||||
t.equal(p1.solution(await sample), 26397);
|
||||
t.equal(p1.solution(await input), 311949);
|
||||
});
|
||||
|
||||
tap.test("part2", async (t) => {
|
||||
// t.equal(p2.solution(await sample), 0);
|
||||
// t.equal(p2.solution(await input), 0);
|
||||
t.match(p2.linesScore(await sample), [
|
||||
288957, 5566, 1480781, 995444, 294
|
||||
]);
|
||||
t.equal(p2.solution(await sample), 288957);
|
||||
t.equal(p2.solution(await input), 3042730309);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user