-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2DArray.js
193 lines (167 loc) · 5.14 KB
/
2DArray.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict'
// https://www.hackerrank.com/challenges/2d-array/problem
const fs = require('fs')
process.stdin.resume()
process.stdin.setEncoding('utf-8')
let inputString = ''
let currentLine = 0
/*
* Complete the contacts function below.
*/
function hourglassSum(arr) {
// console.log(arr)
const totalHourglasses = 16
let startingColumn = 0 // starts on 1
let rowTop = 0
let rowMid = 1
let rowBottom = 2
function getHourGlass() {
let row1 = arr[rowTop]
// console.log('-----------------------------------startingColumn', startingColumn)
// console.log('row 1 >>>>>>', row1)
const a1 = [row1[startingColumn], row1[startingColumn + 1], row1[startingColumn + 2]]
let row2 = arr[rowMid]
// console.log('row 2 >>>>>>', row2)
const a2 = [row2[startingColumn + 1]]
let row3 = arr[rowBottom]
// console.log('row 3 >>>>>>', row3)
const a3 = [row3[startingColumn], row3[startingColumn + 1], row3[startingColumn + 2]]
const hourGlass = [...a1, ...a2, ...a3]
// console.log('', hourGlass)
return hourGlass
}
const hourGlasses = []
for (let x = 0; x < totalHourglasses; x++) {
const hourGlass = getHourGlass()
// console.log('', Math.max(...hourGlass))
hourGlasses.push(hourGlass)
// console.log('============== x', x)
startingColumn = startingColumn + 1
if (startingColumn === 4) {
startingColumn = 0
}
if ( x === 3) {
// console.log('-----------------------------------------------------')
rowTop = rowTop + 1
rowMid = rowMid + 1
rowBottom = rowBottom + 1
} else if (x === 7) {
// console.log('-----------------------------------------------------')
rowTop = rowTop + 1
rowMid = rowMid + 1
rowBottom = rowBottom + 1
} else if (x === 11) {
// console.log('-----------------------------------------------------')
rowTop = rowTop + 1
rowMid = rowMid + 1
rowBottom = rowBottom + 1
} else if (x === 15) {
// console.log('-----------------------------------------------------')
rowTop = rowTop + 1
rowMid = rowMid + 1
rowBottom = rowBottom + 1
}
}
// console.log(hourGlasses)
let highestSum = 0
for (let x = 0; x < hourGlasses.length; x++) {
let accumulator = 0
for (let w = 0; w < hourGlasses[x].length; w++) {
accumulator = accumulator + hourGlasses[x][w]
}
if (x === 0) {
highestSum = accumulator
}
console.log('sum', accumulator)
if (accumulator > highestSum) {
highestSum = accumulator
}
}
return highestSum
}
function readLine() {
return inputString[currentLine++]
}
function main() {
let arr = Array(6)
for (let i = 0; i < 6; i++) {
arr[i] = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10))
}
let result = hourglassSum(arr)
console.log(result)
process.exit(0)
}
let reader = fs.createReadStream('input2d00.txt')
reader.on('data', function (chunk) {
inputString += chunk.toString()
})
reader.on("end", () => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''))
main()
})
// totalHourglasses ========================= 1
// row 1, column 1, column 2, column 3
// row 2, column 2
// row 3, column 1, column 2, column 3
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 2
// row 1, column 2, column 3, column 4
// row 2, column 3
// row 3, column 2, column 3, column 4
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 3
// row 1, column 3, column 4, column 5
// row 2, column 4
// row 3, column 3, column 4, column 5
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 4
// row 1, column 4, column 5, column 6
// row 2, column 5
// row 3, column 4, column 5, column 6
//
// startingColumn = startingColumn = 1
//
// if (startingColumn === totalStackColumns) { startingColumn = 1}
// rowTop = rowTop + 1
// rowMid = rowMid + 1
// rowBottom = rowBottom + 1
// currentlyRow = currentlyRow + 1
// if currentlyRow === totalStacks then it is done
// totalHourglasses ========================= 5
// row 2, column 1, column 2, column 3
// row 3, column 2
// row 4, column 1, column 2, column 3
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 6
// row 2, column 2, column 3, column 4
// row 3, column 3
// row 4, column 2, column 3, column 4
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 7
// row 2, column 3, column 4, column 5
// row 3, column 4
// row 4, column 3, column 4, column 5
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 8
// row 2, column 4, column 5, column 6
// row 3, column 5
// row 4, column 4, column 5, column 6
//
// startingColumn = startingColumn = 1
//
// if (startingColumn === totalStackColumns) { startingColumn = 1}
//