-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.js
180 lines (167 loc) · 5.62 KB
/
day04.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
var fs = require('fs')
const height = 5
const width = 5
function load(file) {
var input = fs.readFileSync(file, 'utf8')
let numbers = []
let lines = input.split("\n")
lines.shift().split(",").forEach(item => {
numbers.push(+item)
})
lines.shift()
let matrixes = []
let matrix = []
lines.forEach(line => {
if (line.length==0) {
matrixes.push(matrix)
matrix = []
return lines
} else {
let matrixLine = []
line.split(" ").forEach(item => {
if (item.trim().length != 0) { // string.split sucks in js !
matrixLine.push(+item)
}
})
matrix.push(matrixLine)
}
})
let emptyMatrixes = []
for (let i = 0; i < matrixes.length; i++) {
let matrix = []
for (let y = 0; y < height; y++) {
matrix.push([0,0,0,0,0])
}
emptyMatrixes.push(matrix)
}
return {numbers: numbers, matrixes:matrixes, score: emptyMatrixes}
}
function runDay04(file) {
console.log("Step1", file)
let bingo = load(file)
for (let index = 0; index < bingo.numbers.length; index++) {
let number = bingo.numbers[index]
for (let matrixIndex = 0; matrixIndex < bingo.matrixes.length; matrixIndex++) {
const matrix = bingo.matrixes[matrixIndex];
for (let y = 0; y < matrix.length; y++) {
for (let x = 0; x < matrix[y].length; x++) {
if (number == matrix[y][x]) {
//console.log("Found ", number, "at", x, y, "for matrix", matrixIndex)
bingo.score[matrixIndex][y][x] = 1
}
}
}
}
let bingoedMatrixIndex = checkForBingo(bingo)
if (bingoedMatrixIndex != -1) {
let bingoFinalSum = computeFinalSum(bingo, bingoedMatrixIndex)
let finalScore = bingoFinalSum * number
console.log('> ', bingoFinalSum)
console.log('> ', finalScore)
return finalScore
}
}
}
function checkForBingo(bingo) {
// check line
for (let matrixIndex = 0; matrixIndex < bingo.matrixes.length; matrixIndex++) {
const matrix = bingo.matrixes[matrixIndex];
for (let y = 0; y < matrix.length; y++) {
let count = 0
for (let x = 0; x < matrix[y].length; x++) {
if (bingo.score[matrixIndex][y][x] == 0) {
break
}
count++
}
if (count == width) {
console.log('> ',"BINGO matrix", matrixIndex, " line " , y)
return matrixIndex
}
}
}
// check col
for (let matrixIndex = 0; matrixIndex < bingo.matrixes.length; matrixIndex++) {
const matrix = bingo.matrixes[matrixIndex];
for (let x = 0; x < width; x++) {
let count = 0
for (let y = 0; y < matrix.length; y++) {
if (bingo.score[matrixIndex][y][x] == 0) {
break
}
count++
}
if (count == height) {
console.log('> ',"BINGO matrix", matrixIndex, " column " , x)
return matrixIndex
}
}
}
return -1
}
function computeFinalSum(bingo, matrixIndex) {
const matrix = bingo.matrixes[matrixIndex];
let sum = 0
for (let y = 0; y < matrix.length; y++) {
for (let x = 0; x < matrix[y].length; x++) {
if (bingo.score[matrixIndex][y][x] == 0) {
sum += bingo.matrixes[matrixIndex][y][x]
}
}
}
return sum
}
function displayMatrix(matrix) {
for (let y = 0; y < matrix.length; y++) {
let line = ""
for (let x = 0; x < matrix[y].length; x++) {
if (matrix[y][x] == 0) {
line += "."
} else {
line += "X"
}
}
console.log(line)
}
}
function runDay04Step2(file) {
console.log("Step2", file)
let bingo = load(file)
console.log("we start with ", bingo.matrixes.length, " matrixes ")
for (let index = 0; index < bingo.numbers.length; index++) {
let number = bingo.numbers[index]
console.log("calling", number)
for (let matrixIndex = 0; matrixIndex < bingo.matrixes.length; matrixIndex++) {
const matrix = bingo.matrixes[matrixIndex];
for (let y = 0; y < matrix.length; y++) {
for (let x = 0; x < matrix[y].length; x++) {
if (number == matrix[y][x]) {
bingo.score[matrixIndex][y][x] = 1
}
}
}
}
while(true) { // argh
let bingoedMatrixIndex = checkForBingo(bingo)
if (bingoedMatrixIndex != -1) {
if (bingo.matrixes.length == 1) {
let finalSum = computeFinalSum(bingo,bingoedMatrixIndex)
let finalScore = finalSum * number
console.log('> ', finalSum)
console.log('> ', finalScore)
return finalScore
} else {
bingo.matrixes.splice(bingoedMatrixIndex, 1)
bingo.score.splice(bingoedMatrixIndex, 1)
console.log("we got now ", bingo.matrixes.length, "matrixes left ")
}
} else {
break
}
}
}
}
runDay04('input-test.txt')
runDay04('input.txt')
runDay04Step2('input-test.txt')
runDay04Step2('input.txt')