Day 01 Part 02
This commit is contained in:
parent
cb140cac39
commit
164549a01d
1 changed files with 39 additions and 3 deletions
42
01/index.ts
42
01/index.ts
|
@ -1,11 +1,31 @@
|
|||
const fs = require('fs').promises;
|
||||
|
||||
const input: string = (await fs.readFile('./input.txt')).toString()
|
||||
// const testInput = `two1nine
|
||||
// eightwothree
|
||||
// abcone2threexyz
|
||||
// xtwone3four
|
||||
// 4nineeightseven2
|
||||
// zoneight234
|
||||
// 7pqrstsixteen`
|
||||
|
||||
console.log("Solution first part: " + partOne())
|
||||
const patterns: {[id: string]: string} = {
|
||||
one: '1',
|
||||
two: '2',
|
||||
three: '3',
|
||||
four: '4',
|
||||
five: '5',
|
||||
six: '6',
|
||||
seven: '7',
|
||||
eight: '8',
|
||||
nine: '9'
|
||||
}
|
||||
|
||||
console.log("Solution first part: " + partOne(input))
|
||||
console.log("Solution second part: " + partTwo(input))
|
||||
|
||||
|
||||
function partOne(): string {
|
||||
function partOne(input: string): string {
|
||||
let digits = []
|
||||
for (const line of input.split('\n')) {
|
||||
let first = undefined
|
||||
|
@ -27,4 +47,20 @@ function partOne(): string {
|
|||
return `${sum}`
|
||||
}
|
||||
|
||||
function partTwo() {}
|
||||
function partTwo(input: string): string {
|
||||
let fixedParts: Array<string> = []
|
||||
|
||||
for (const line of input.split('\n')) {
|
||||
let modified = line
|
||||
for (const pattern of Object.keys(patterns)) {
|
||||
let count = 0
|
||||
for (const match of modified.matchAll(new RegExp(pattern, 'g'))) {
|
||||
let pos = match.index! + count
|
||||
modified = [modified.slice(0, pos), pattern[0], patterns[pattern], pattern[pattern.length], modified.slice(pos)].join('')
|
||||
count += 3
|
||||
}
|
||||
}
|
||||
fixedParts.push(modified)
|
||||
}
|
||||
return partOne(fixedParts.join('\n'))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue