-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3part1.js
93 lines (71 loc) · 2.73 KB
/
day3part1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Importing the fs module
let fs = require("fs")
// Intitializing the readFileLines with the file
const readFileLines = filename =>
fs.readFileSync(filename)
.toString('UTF8')
.split('\r\n'); //regex splits it by line
// Calling the readFiles function with file name
let allNumbers = readFileLines('2303input.txt');
let runningTotal = 0
let cols = allNumbers[0].length
let sampleRows = allNumbers.map(row => row.split(''));
let rows = sampleRows.length
const regex = /[^0-9.]+/;
function addPartsNumbers(sampleRows) {
let viewer = function (row, col) {
let currentNumber = ''
if (row < 0 || row >= rows || col < 0 || col >= cols || sampleRows[row][col] === '.') {
return 0;
}
if (!isNaN(Number(sampleRows[row][col]))){ //
//console.log(`the number is: ${sampleRows[row][col]}`)
currentNumber += sampleRows[row][col]
sampleRows[row][col] = '.'
//console.log(currentNumber)
//search towards the left
let step = 1
while(sampleRows[row][col-step] && !isNaN(Number(sampleRows[row][col-step]))){
currentNumber = sampleRows[row][col-step] + currentNumber
sampleRows[row][col-step] = '.'
//console.log(currentNumber)
step++
}
//search towards the right
step = 1
while(sampleRows[row][col+step] && !isNaN(Number(sampleRows[row][col+step]))){
currentNumber = currentNumber + sampleRows[row][col+step]
sampleRows[row][col+step] = '.'
//console.log(currentNumber)
step++
}
}
console.log(currentNumber)
return Number(currentNumber)
}
// Printing the response array
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if(regex.test(sampleRows[i][j])){
console.log(`before we enter ${sampleRows[i][j]}`)
//mark current cell as visited
sampleRows[i][j] = '.'
//middle ones
runningTotal += viewer(i-1,j)
runningTotal += viewer(i+1,j)
//to the left of the symbol
runningTotal += viewer(i-1,j-1)
runningTotal += viewer(i,j-1)
runningTotal += viewer(i+1,j-1)
//to the right of the symbol
runningTotal += viewer(i-1,j+1)
runningTotal += viewer(i,j+1)
runningTotal += viewer(i+1,j+1)
}
}
}
sampleRows = sampleRows.map(row => row.join(''));
console.log(sampleRows);
console.log(runningTotal)
}
addPartsNumbers(sampleRows)