From 0f9d151dec8331db03634edc5d688716a1d0fe8f Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 1 Dec 2023 15:59:04 -0500 Subject: [PATCH] day 1, with javascript --- 2023/01/benchmark.js | 32 ++++++++++++++++++++++++++++++++ 2023/01/benchmark.test.js | 13 +++++++++++++ 2023/01/part1.js | 16 ++++++++++++++++ 2023/01/part2.js | 32 ++++++++++++++++++++++++++++++++ 2023/01/solutions.yml | 2 ++ 2023/01/test.js | 16 ++++++++++++++++ 2023/benchmark.json | 2 ++ 2023/package.json | 6 +++++- 8 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 2023/01/benchmark.js create mode 100644 2023/01/benchmark.test.js create mode 100644 2023/01/part1.js create mode 100644 2023/01/part2.js create mode 100644 2023/01/solutions.yml create mode 100644 2023/01/test.js diff --git a/2023/01/benchmark.js b/2023/01/benchmark.js new file mode 100644 index 0000000..ac4bbe2 --- /dev/null +++ b/2023/01/benchmark.js @@ -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 + })); +}) diff --git a/2023/01/benchmark.test.js b/2023/01/benchmark.test.js new file mode 100644 index 0000000..4ec0bda --- /dev/null +++ b/2023/01/benchmark.test.js @@ -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]) +}); + diff --git a/2023/01/part1.js b/2023/01/part1.js new file mode 100644 index 0000000..0817df4 --- /dev/null +++ b/2023/01/part1.js @@ -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) + ); diff --git a/2023/01/part2.js b/2023/01/part2.js new file mode 100644 index 0000000..7b00301 --- /dev/null +++ b/2023/01/part2.js @@ -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), +); diff --git a/2023/01/solutions.yml b/2023/01/solutions.yml new file mode 100644 index 0000000..52275e1 --- /dev/null +++ b/2023/01/solutions.yml @@ -0,0 +1,2 @@ +1: 56397 +2: 55701 diff --git a/2023/01/test.js b/2023/01/test.js new file mode 100644 index 0000000..5649d82 --- /dev/null +++ b/2023/01/test.js @@ -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']); +}) diff --git a/2023/benchmark.json b/2023/benchmark.json index 5e2a017..bdd5f2b 100644 --- a/2023/benchmark.json +++ b/2023/benchmark.json @@ -1,2 +1,4 @@ {"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"} +{"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} diff --git a/2023/package.json b/2023/package.json index 6b82c36..b53a198 100644 --- a/2023/package.json +++ b/2023/package.json @@ -1,5 +1,6 @@ { "name": "2023", + "type": "module", "version": "1.0.0", "description": "", "main": "vitest.config.js", @@ -10,6 +11,9 @@ "author": "Yanick Champoux (http://techblog.babyl.ca/)", "license": "ISC", "dependencies": { - "vitest": "^0.34.6" + "fs-extra": "^11.2.0", + "tinybench": "^2.5.1", + "vitest": "^0.34.6", + "yaml": "^2.3.4" } }