Skip to content

Commit f75661e

Browse files
[BOJ] 2578 빙고 (S4)
1 parent 984f0fe commit f75661e

2 files changed

Lines changed: 67 additions & 6 deletions

File tree

서정우/6주차/260204.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const fs = require("fs");
2+
const filePath = process.platform === "linux" ? "/dev/stdin" : "./서정우/input.txt";
3+
const input = fs.readFileSync(filePath).toString().trim().split("\n");
4+
5+
const bingoBoard = input.slice(0, 5).map((line) => line.split(" ").map(Number));
6+
const calls = input.slice(5).join(" ").split(" ").map(Number);
7+
8+
const marked = Array.from({ length: 5 }, () => Array(5).fill(false));
9+
const rowBingo = Array(5).fill(false);
10+
const colBingo = Array(5).fill(false);
11+
let diag1Bingo = false,
12+
diag2Bingo = false;
13+
14+
const markNumber = (num) => {
15+
for (let i = 0; i < 5; i++) {
16+
for (let j = 0; j < 5; j++) {
17+
if (bingoBoard[i][j] === num) {
18+
marked[i][j] = true;
19+
return [i, j];
20+
}
21+
}
22+
}
23+
return null;
24+
};
25+
26+
const checkBingo = ([x, y]) => {
27+
let bingo = 0;
28+
if (!rowBingo[x] && marked[x].every((v) => v)) {
29+
rowBingo[x] = true;
30+
bingo++;
31+
}
32+
if (!colBingo[y] && marked.every((row) => row[y])) {
33+
colBingo[y] = true;
34+
bingo++;
35+
}
36+
if (x === y && !diag1Bingo && marked.every((row, idx) => row[idx])) {
37+
diag1Bingo = true;
38+
bingo++;
39+
}
40+
if (x + y === 4 && !diag2Bingo && marked.every((row, idx) => row[4 - idx])) {
41+
diag2Bingo = true;
42+
bingo++;
43+
}
44+
return bingo;
45+
};
46+
47+
let bingoCount = 0;
48+
for (let callCount = 0; callCount < calls.length; callCount++) {
49+
const pos = markNumber(calls[callCount]);
50+
if (pos) {
51+
bingoCount += checkBingo(pos);
52+
if (bingoCount >= 3) {
53+
console.log(callCount + 1);
54+
break;
55+
}
56+
}
57+
}

서정우/input.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
5
2-
03:05
3-
12:40
4-
03:25
5-
03:25
6-
01:10
1+
11 12 2 24 10
2+
16 1 13 3 25
3+
6 20 5 21 17
4+
19 4 8 14 9
5+
22 15 7 23 18
6+
5 10 7 16 2
7+
4 22 8 17 13
8+
3 18 1 6 25
9+
12 19 23 14 21
10+
11 24 9 20 15

0 commit comments

Comments
 (0)