137 lines
No EOL
3.5 KiB
TypeScript
137 lines
No EOL
3.5 KiB
TypeScript
const fs = require('fs').promises;
|
|
|
|
const input: string = (await fs.readFile('./input.txt')).toString()
|
|
const testInput = `
|
|
467..114..
|
|
...*......
|
|
..35..633.
|
|
......#...
|
|
617*......
|
|
.....+.58.
|
|
..592.....
|
|
......755.
|
|
...$.*....
|
|
.664.598..
|
|
`
|
|
|
|
console.log("Solution first part: " + partOne(input))
|
|
// console.log("Solution second part: " + partTwo(input))
|
|
|
|
|
|
function partOne(input: string): string {
|
|
const raw = input.split("\n")
|
|
let lines = []
|
|
|
|
for(const line of raw) {
|
|
lines.push(line.split(''))
|
|
}
|
|
|
|
let validNumbers = []
|
|
let currentNumber = ''
|
|
let currentStart = 0
|
|
for (const [lineI, line] of lines.entries()) {
|
|
for(const [i, char] of line.entries()) {
|
|
if(!Number.isNaN(parseInt(char))) {
|
|
if (currentNumber == '') { currentStart = i}
|
|
currentNumber += char
|
|
} else {
|
|
if (currentNumber !== '') {
|
|
if(checkHasAdjacents(lines, lineI, currentNumber.length, currentStart, false)) {
|
|
validNumbers.push(parseInt(currentNumber))
|
|
} else {
|
|
console.log(`${lineI+1}: ${currentNumber}`)
|
|
}
|
|
}
|
|
currentNumber = ''
|
|
}
|
|
}
|
|
if (currentNumber !== '') {
|
|
if(checkHasAdjacents(lines, lineI, currentNumber.length, currentStart, false)) {
|
|
validNumbers.push(parseInt(currentNumber))
|
|
}
|
|
}
|
|
currentNumber = ''
|
|
}
|
|
let result = validNumbers.reduce((acc, item) => { return acc + item })
|
|
return `${result}`
|
|
}
|
|
|
|
function grabChar(lines: Array<Array<string>>, lineIndex: number, start: number): string | false {
|
|
if (lines[lineIndex] && lines[lineIndex][start]) {
|
|
return lines[lineIndex][start]
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
function grabCharRange(lines: Array<Array<string>>, lineIndex: number, start: number, end: number): Array<string> {
|
|
let chars: Array<string> = []
|
|
let i = start
|
|
if (!lines[lineIndex]) {
|
|
return []
|
|
}
|
|
while(i <= end) {
|
|
if (lines[lineIndex][i]) {
|
|
chars.push(lines[lineIndex][i])
|
|
}
|
|
i++
|
|
}
|
|
return chars
|
|
}
|
|
|
|
function checkHasAdjacents(lines: Array<Array<string>>, lineIndex: number, length: number, start: number, log: boolean): boolean {
|
|
let hasAdjacents = false
|
|
let notSymbols = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.']
|
|
|
|
// Left
|
|
let leftChar = grabChar(lines, lineIndex, start -1)
|
|
if (leftChar && notSymbols.indexOf(leftChar) === -1) {
|
|
hasAdjacents = true
|
|
}
|
|
|
|
// Left Diagonal
|
|
let leftTopChar = grabChar(lines, lineIndex - 1, start - 1)
|
|
if (leftTopChar && notSymbols.indexOf(leftTopChar) === -1) {
|
|
hasAdjacents = true
|
|
}
|
|
let leftBottomChar = grabChar(lines, lineIndex + 1, start - 1)
|
|
if (leftBottomChar && notSymbols.indexOf(leftBottomChar) === -1) {
|
|
hasAdjacents = true
|
|
}
|
|
// Right
|
|
let rightChar = grabChar(lines, lineIndex, start + length)
|
|
if (rightChar && notSymbols.indexOf(rightChar) === -1) {
|
|
hasAdjacents = true
|
|
}
|
|
// Right Diagonal
|
|
let rightTopChar = grabChar(lines, lineIndex - 1, start + length)
|
|
if (rightTopChar && notSymbols.indexOf(rightTopChar) === -1) {
|
|
hasAdjacents = true
|
|
}
|
|
let rightBottomChar = grabChar(lines, lineIndex + 1, start + length)
|
|
if (rightBottomChar && notSymbols.indexOf(rightBottomChar) === -1) {
|
|
hasAdjacents = true
|
|
}
|
|
|
|
// Top
|
|
let topChars = grabCharRange(lines, lineIndex - 1, start, start + length -1)
|
|
for (const char of topChars) {
|
|
if (notSymbols.indexOf(char) === -1) {
|
|
hasAdjacents = true
|
|
}
|
|
}
|
|
|
|
|
|
// Bottom
|
|
let bottomChars = grabCharRange(lines, lineIndex + 1, start, start + length - 1)
|
|
for (const char of bottomChars) {
|
|
if (notSymbols.indexOf(char) === -1) {
|
|
hasAdjacents = true
|
|
}
|
|
}
|
|
|
|
return hasAdjacents
|
|
}
|
|
|
|
function partTwo(input: string): string {
|
|
} |