Skip to content

Commit 7a4303f

Browse files
committed
strated 2023
1 parent a449419 commit 7a4303f

File tree

9 files changed

+1377
-0
lines changed

9 files changed

+1377
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

2022/.DS_Store

6 KB
Binary file not shown.

2023/1/code.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const fs = require('fs');
2+
3+
const words = [
4+
'one',
5+
'two',
6+
'three',
7+
'four',
8+
'five',
9+
'six',
10+
'seven',
11+
'eight',
12+
'nine'
13+
]
14+
15+
const hasWord = (word) => {
16+
for (let index = 0; index < words.length; index++) {
17+
const element = words[index];
18+
if (word.includes(element)) {
19+
return index + 1;
20+
}
21+
}
22+
return null;
23+
}
24+
25+
const findFirstNumber = (line) => {
26+
let letters = '';
27+
for (let index = 0; index < line.length; index++) {
28+
const element = line[index];
29+
if (parseInt(element)) {
30+
return parseInt(element);
31+
}
32+
letters += element;
33+
const found = hasWord(letters);
34+
if (found) { return found; }
35+
}
36+
}
37+
38+
const findLasatNumber = (line) => {
39+
let letters = '';
40+
for (let index = line.length - 1; index >= 0; index--) {
41+
const element = line[index];
42+
if (parseInt(element)) {
43+
return parseInt(element);
44+
}
45+
letters = element.concat(letters);
46+
const found = hasWord(letters);
47+
if (found) { return found; }
48+
}
49+
}
50+
51+
const allContents = fs.readFileSync('puzzleInput.txt', 'utf-8');
52+
const numbers = [];
53+
allContents.split(/\r?\n/).forEach((line) => {
54+
const firstNumber = findFirstNumber(line);
55+
const lastNumber = findLasatNumber(line);
56+
const value = `${firstNumber}${lastNumber}`;
57+
numbers.push(Number(value));
58+
});
59+
const total = numbers.reduce((cum, x) => cum + x, 0)
60+
console.log(total);
61+

2023/1/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "1",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "code.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC"
11+
}

0 commit comments

Comments
 (0)