Merge branch 'day1-js'
This commit is contained in:
commit
f14bd1d1b8
32
2023/01/benchmark.js
Normal file
32
2023/01/benchmark.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import fs from 'fs-extra';
|
||||||
|
import { parse } from 'yaml';
|
||||||
|
import { Bench } from 'tinybench';
|
||||||
|
import assert from 'assert';
|
||||||
|
|
||||||
|
import { solution_1 } from './part1.js';
|
||||||
|
import { solution_2 } from './part2.js';
|
||||||
|
|
||||||
|
const input = fs.readFileSync('./input',{ encoding: 'utf8'});
|
||||||
|
const solutions = parse( fs.readFileSync('./solutions.yml',{encoding:'utf8'}));
|
||||||
|
|
||||||
|
const bench = new Bench({time: 10000});
|
||||||
|
|
||||||
|
bench.add( '1', () => {
|
||||||
|
assert.equal(solution_1(input),solutions[1])
|
||||||
|
})
|
||||||
|
.add( '2', () => {
|
||||||
|
assert.equal(solution_2(input),solutions[2])
|
||||||
|
});
|
||||||
|
|
||||||
|
await bench.run();
|
||||||
|
|
||||||
|
bench.tasks.forEach( ({result,name}) => {
|
||||||
|
console.log(JSON.stringify({
|
||||||
|
day : 1,
|
||||||
|
year : 2023,
|
||||||
|
language: 'javascript',
|
||||||
|
part: name,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
time: result.mean / 1000
|
||||||
|
}));
|
||||||
|
})
|
13
2023/01/benchmark.test.js
Normal file
13
2023/01/benchmark.test.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { bench, expect } from 'vitest';
|
||||||
|
import { readFileSync } from 'fs-extra';
|
||||||
|
import { parse } from 'yaml';
|
||||||
|
|
||||||
|
import { solution_1 } from './part1.js';
|
||||||
|
|
||||||
|
const input = readFileSync('./input',{ encoding: 'utf8'});
|
||||||
|
const solutions = parse( readFileSync('./solutions.yml',{encoding:'utf8'}));
|
||||||
|
|
||||||
|
bench( 'part 1', () => {
|
||||||
|
expect(solution_1(input)).toBe(solutions[1])
|
||||||
|
});
|
||||||
|
|
16
2023/01/part1.js
Normal file
16
2023/01/part1.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import * as R from 'remeda';
|
||||||
|
|
||||||
|
|
||||||
|
export const extract_number = (line) => {
|
||||||
|
const n = line.match(/(\d)/g);
|
||||||
|
return parseInt(
|
||||||
|
n[0] + n[n.length-1]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const solution_1 = R.createPipe(
|
||||||
|
i => i.split("\n"),
|
||||||
|
R.compact,
|
||||||
|
R.map(extract_number),
|
||||||
|
R.sumBy(R.identity)
|
||||||
|
);
|
32
2023/01/part2.js
Normal file
32
2023/01/part2.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import * as R from 'remeda';
|
||||||
|
|
||||||
|
import { extract_number } from './part1.js';
|
||||||
|
|
||||||
|
const spelled = {
|
||||||
|
'one' : 1,
|
||||||
|
'two' : 2,
|
||||||
|
'three' : 3,
|
||||||
|
'four' : 4,
|
||||||
|
'five' : 5,
|
||||||
|
'six' : 6,
|
||||||
|
'seven' : 7,
|
||||||
|
'eight' : 8,
|
||||||
|
'nine' : 9,
|
||||||
|
};
|
||||||
|
|
||||||
|
const resolve_spelled_numbers = (line) => {
|
||||||
|
const re = Object.keys(spelled).join('|') + '|\\d';
|
||||||
|
|
||||||
|
const first = line.match( RegExp( `^.*?(${re})`) )[1];
|
||||||
|
const last = line.match( RegExp( `^.*(${re})`) )[1];
|
||||||
|
|
||||||
|
return [ first, last ].map( n => spelled[n] ?? n ).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
export const solution_2 = R.createPipe(
|
||||||
|
i => i.split("\n"),
|
||||||
|
R.compact,
|
||||||
|
R.map(resolve_spelled_numbers),
|
||||||
|
R.map(extract_number),
|
||||||
|
R.sumBy(R.identity),
|
||||||
|
);
|
2
2023/01/solutions.yml
Normal file
2
2023/01/solutions.yml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
1: 56397
|
||||||
|
2: 55701
|
16
2023/01/test.js
Normal file
16
2023/01/test.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { test, expect } from 'vitest';
|
||||||
|
import { readFileSync } from 'fs-extra';
|
||||||
|
import { parse } from 'yaml';
|
||||||
|
|
||||||
|
import { solution_1 } from './part1.js';
|
||||||
|
import { solution_2 } from './part2.js';
|
||||||
|
|
||||||
|
const input = readFileSync('./input',{ encoding: 'utf8'});
|
||||||
|
const solutions = parse( readFileSync('./solutions.yml',{encoding:'utf8'}));
|
||||||
|
|
||||||
|
test( 'part 1', () => {
|
||||||
|
expect( solution_1(input)).toBe(solutions['1']);
|
||||||
|
})
|
||||||
|
test( 'part 2', () => {
|
||||||
|
expect( solution_2(input)).toBe(solutions['2']);
|
||||||
|
})
|
@ -1,2 +1,4 @@
|
|||||||
{"language":"perl","time":0.00189813978688821,"part":1,"day":1,"year":2023,"timestamp":"2023-12-01T17:35:46"}
|
{"language":"perl","time":0.00189813978688821,"part":1,"day":1,"year":2023,"timestamp":"2023-12-01T17:35:46"}
|
||||||
{"language":"perl","part":2,"time":0.00721379310344828,"day":1,"year":2023,"timestamp":"2023-12-01T17:35:58"}
|
{"language":"perl","part":2,"time":0.00721379310344828,"day":1,"year":2023,"timestamp":"2023-12-01T17:35:58"}
|
||||||
|
{"day":1,"year":2023,"language":"javascript","part":"1","timestamp":"2023-12-01T20:55:40.986Z","time":0.0007267522645031297}
|
||||||
|
{"day":1,"year":2023,"language":"javascript","part":"2","timestamp":"2023-12-01T20:55:40.989Z","time":0.004363576172638605}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "2023",
|
"name": "2023",
|
||||||
|
"type": "module",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "vitest.config.js",
|
"main": "vitest.config.js",
|
||||||
@ -10,6 +11,9 @@
|
|||||||
"author": "Yanick Champoux <yanick@babyl.ca> (http://techblog.babyl.ca/)",
|
"author": "Yanick Champoux <yanick@babyl.ca> (http://techblog.babyl.ca/)",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"vitest": "^0.34.6"
|
"fs-extra": "^11.2.0",
|
||||||
|
"tinybench": "^2.5.1",
|
||||||
|
"vitest": "^0.34.6",
|
||||||
|
"yaml": "^2.3.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user