advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

commit 07d0e5ca5f8a19672cbec9c028d1351f540d03cc
parent 4c9bdcf38343377bccb21899cc5460fea5c37b34
Author: finwo <finwo@pm.me>
Date:   Wed,  8 Dec 2021 17:39:51 +0100

Moved re-used fns from day-01 to common

Diffstat:
Asrc/common/index.ts | 2++
Asrc/common/line-by-line.ts | 12++++++++++++
Asrc/common/sum.ts | 3+++
Msrc/day-01/puzzle-01.ts | 15++-------------
Msrc/day-01/puzzle-02.ts | 18++----------------
5 files changed, 21 insertions(+), 29 deletions(-)

diff --git a/src/common/index.ts b/src/common/index.ts @@ -0,0 +1,2 @@ +export * from './line-by-line'; +export * from './sum'; diff --git a/src/common/line-by-line.ts b/src/common/line-by-line.ts @@ -0,0 +1,12 @@ +import readline from 'readline'; +import fs from 'fs'; +import events from 'events'; + +export function lineByLine(filename: string, cb: ()=>{}): Promise<void> { + const rl = readline.createInterface({ + input : fs.createReadStream(filename), + output : null, + }); + rl.on('line', cb); + return events.once(rl, 'close'); +} diff --git a/src/common/sum.ts b/src/common/sum.ts @@ -0,0 +1,3 @@ +export function sum(...n: number[]): number { + return n.reduce((r,a)=>r+a, 0); +} diff --git a/src/day-01/puzzle-01.ts b/src/day-01/puzzle-01.ts @@ -1,7 +1,4 @@ -import readline from 'readline'; -import fs from 'fs'; -import events from 'events'; - +import { lineByLine } from '../common'; let lastDepth = null; let increasedTotal = 0; @@ -14,12 +11,7 @@ let texts = [ (async () => { - const rl = readline.createInterface({ - input : fs.createReadStream(__dirname + '/input-01'), - output : null, - }); - - rl.on('line', line => { + await lineByLine(__dirname + '/input-01', line => { if ((!line) || isNaN(line)) return; const depth = parseInt(line); @@ -35,13 +27,10 @@ let texts = [ lastDepth = depth; }); - await events.once(rl, 'close'); - process.stdout.write('\n\n'); process.stdout.write('---[ REPORT ]---\n'); process.stdout.write(`Increased ${increasedTotal} times\n`); process.stdout.write('\n'); - })(); diff --git a/src/day-01/puzzle-02.ts b/src/day-01/puzzle-02.ts @@ -1,6 +1,4 @@ -import readline from 'readline'; -import fs from 'fs'; -import events from 'events'; +import { lineByLine, sum } from '../common'; let history = []; let sumLast = null; @@ -12,18 +10,9 @@ let texts = [ 'increased', ]; -function sum(...n) { - return n.reduce((r,a)=>r+a, 0); -} - (async () => { - const rl = readline.createInterface({ - input : fs.createReadStream(__dirname + '/input-01'), - output : null, - }); - - rl.on('line', line => { + await lineByLine(__dirname + '/input-01', line => { if ((!line) || isNaN(line)) return; const depth = parseInt(line); history.push(depth); @@ -46,13 +35,10 @@ function sum(...n) { }); - await events.once(rl, 'close'); - process.stdout.write('\n\n'); process.stdout.write('---[ REPORT ]---\n'); process.stdout.write(`Increased ${increasedTotal} times\n`); process.stdout.write('\n'); - })();