Day 01 Part 01
This commit is contained in:
commit
cb140cac39
7 changed files with 1253 additions and 0 deletions
30
01/index.ts
Normal file
30
01/index.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
const fs = require('fs').promises;
|
||||
|
||||
const input: string = (await fs.readFile('./input.txt')).toString()
|
||||
|
||||
console.log("Solution first part: " + partOne())
|
||||
|
||||
|
||||
function partOne(): string {
|
||||
let digits = []
|
||||
for (const line of input.split('\n')) {
|
||||
let first = undefined
|
||||
let last = undefined
|
||||
for (const char of line) {
|
||||
const digit = parseInt(char)
|
||||
if (!Number.isNaN(digit)) {
|
||||
if (first === undefined) {
|
||||
first = digit
|
||||
}
|
||||
last = digit
|
||||
}
|
||||
}
|
||||
digits.push(parseInt(`${first}${last}`))
|
||||
}
|
||||
let sum = digits.reduce((acc, item) => {
|
||||
return acc += item
|
||||
})
|
||||
return `${sum}`
|
||||
}
|
||||
|
||||
function partTwo() {}
|
Loading…
Add table
Add a link
Reference in a new issue