-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day-04-B.js
60 lines (55 loc) · 1.09 KB
/
Day-04-B.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
const input = document.body.innerText.trim().split("\n")
const ballz = input.shift().split(",").map(n => +n)
const boards = []
function check(board) {
for (let y = 0; y < 5; y++) {
let sumx = 0
let sumy = 0
for (let x = 0; x < 5; x++) {
sumx += board[y][x]
sumy += board[x][y]
}
if (!sumx || !sumy) return true
}
return false
}
function score(board) {
let sum = 0
for (let y = 0; y < 5; y++) {
for (let x = 0; x < 5; x++) {
sum += board[y][x]
}
}
return sum
}
function call(board, number) {
for (let y = 0; y < 5; y++) {
for (let x = 0; x < 5; x++) {
if (board[y][x] === number) {
board[y][x] = 0
return true
}
}
}
return false
}
while (input.length) {
input.shift() // clear empty line
const board = []
for (let i = 0; i < 5; i++) {
board.push(input.shift().trim().split(/\s+/g).map(n => +n))
}
boards.push(board)
}
let boardScore = 0
for (let ball of ballz) {
for (let i = 0; i < boards.length; i++) {
let board = boards[i]
if (call(board, ball) && check(board)) {
boardScore = ball * score(board)
boards.splice(i, 1)
i--
}
}
}
boardScore