From 8c18d7f74c79a1c81c9f3c0549044e56c0bf3a3d Mon Sep 17 00:00:00 2001 From: x0x7 Date: Thu, 28 Nov 2024 09:48:26 -0500 Subject: [PATCH] Did part 1 of day 1 2023 --- 2023/Day 1/part1.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2023/Day 1/part1.js diff --git a/2023/Day 1/part1.js b/2023/Day 1/part1.js new file mode 100644 index 0000000..0c93e29 --- /dev/null +++ b/2023/Day 1/part1.js @@ -0,0 +1,27 @@ +#!/usr/bin/env node + + +var inputstring = require('fs').readFileSync('input.txt','utf8'); + +var lines = inputstring.split('\n').filter(i=>i); +var minNumerical = '0'.charCodeAt(0); +var maxNumerical = '9'.charCodeAt(0); + +function onlynumerical(str) { + return str.split('').map(i=>i.charCodeAt(0)).filter(i=>(i>=minNumerical && i<=maxNumerical)).map(i=>i-48); +} +function firstandlast(arr) { + return [arr[0],arr[arr.length-1]]; +} +var nums = lines.map(i=>{ + var nums = firstandlast(onlynumerical(i)); + //console.log({nums}); + return nums[0]*10+nums[1]; +}); +//console.log({nums}); + +var sum=0; +for(var i of nums) { + sum+=i; +} +console.log(sum); \ No newline at end of file