From 2d7c7f8d0dc572b0f67365acc44ca1b149889c58 Mon Sep 17 00:00:00 2001 From: Nathan van Ofwegen Date: Thu, 2 Dec 2021 09:52:39 +0100 Subject: [PATCH] Retractor --- input/day1/example1.txt => src/day1/example.txt | 0 src/{ => day1}/index.ts | 12 +++++------- input/day1/1.txt => src/day1/input.txt | 0 src/utils/index.ts | 5 +++++ 4 files changed, 10 insertions(+), 7 deletions(-) rename input/day1/example1.txt => src/day1/example.txt (100%) rename src/{ => day1}/index.ts (56%) rename input/day1/1.txt => src/day1/input.txt (100%) create mode 100644 src/utils/index.ts diff --git a/input/day1/example1.txt b/src/day1/example.txt similarity index 100% rename from input/day1/example1.txt rename to src/day1/example.txt diff --git a/src/index.ts b/src/day1/index.ts similarity index 56% rename from src/index.ts rename to src/day1/index.ts index 109a283..fd34197 100644 --- a/src/index.ts +++ b/src/day1/index.ts @@ -1,11 +1,12 @@ import * as fs from 'fs'; import path from 'path'; +import {readFile} from "../utils"; -let input = fs.readFileSync(path.resolve(__dirname, './../input/day1/1.txt'), {encoding: "utf-8"}).split("\r\n"); +let input = readFile(path.resolve(__dirname, 'input.txt')); let count = -1; let prev = -1; -input.forEach(inp => { +input.forEach((inp: any) => { const measure = Number(inp); if (measure > prev) count++; prev = measure; @@ -13,13 +14,10 @@ input.forEach(inp => { console.log(count); - -input = fs.readFileSync(path.resolve(__dirname, './../input/day1/1.txt'), {encoding: "utf-8"}).split("\r\n"); - let window: any = []; count = -1; prev = -1; -input.forEach(inp => { +input.forEach((inp: any) => { const measure = Number(inp); if (window.length === 3) { @@ -28,7 +26,7 @@ input.forEach(inp => { window.push(measure); - let measureSum = window.reduce((sum:number, current:number) => sum + current, 0); + let measureSum = window.reduce((sum: number, current: number) => sum + current, 0); if (measureSum > prev && window.length === 3) count++; prev = measureSum; diff --git a/input/day1/1.txt b/src/day1/input.txt similarity index 100% rename from input/day1/1.txt rename to src/day1/input.txt diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..cca24ef --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,5 @@ +import * as fs from 'fs' + +export const readFile = (file: string): any => { + fs.readFileSync(file, {encoding: "utf-8"}).split("\r\n"); +} \ No newline at end of file