Skip to content

Commit

Permalink
Create 21608.js
Browse files Browse the repository at this point in the history
  • Loading branch information
oesnuj committed Jul 18, 2024
1 parent 3aef1ba commit 304cf75
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions oesnuj/๊ตฌํ˜„/21608.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const fs = require('fs');
const filepath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = fs.readFileSync(filepath).toString().trim().split('\n');
//์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์ •๋ฆฌ
const N = +input.shift();
const students = input.map(x => {
const [num, ...likes] = x.trim().split(' ').map(Number);
return { num, likes };
});

let board = Array.from({ length: N }, () => Array(N).fill(0));

const dr = [-1, 1, 0, 0];
const dc = [0, 0, -1, 1];

main();

function main(){
for(let i=0; i< N**2; i++){
if(i == 0){
board[1][1] = students[i].num; //์ฒซํ•™์ƒ์€ 1,1์— ๋ฌด์กฐ๊ฑด ์•‰๋Š”๋‹ค.
continue;
}
choiceSeat(students[i].num); //ํ•™์ƒ์„ ์กฐ๊ฑด์— ๋งž๊ฒŒ ์•‰ํžˆ๊ธฐ
}
console.log(calcSatisfy()); //๋ชจ๋‘ ์•‰์€ ํ›„ ๋งŒ์กฑ๋„ ๊ณ„์‚ฐํ•˜๊ธฐ
}

// ์ตœ์ ์˜ ์ž๋ฆฌ ์„ ํƒ ๋ฐ ํ•™์ƒ ๋ฐฐ์น˜ ํ•จ์ˆ˜
function choiceSeat(studentNum){
const neighborInfos = []; //์ธ์ ‘ ์ž๋ฆฌ ์ •๋ณด๋ฅผ ๋ชจ์œผ๋Š” ๋ฐฐ์—ด
for(let i = 0; i<N; i++){
for(let j =0; j<N; j++){
if(board[i][j] !== 0) continue; //์ด๋ฏธ ์ฐจ์žˆ๋Š” ์ž๋ฆฌ ํŒจ์Šค
neighborInfos.push(getSeatInfo(i, j, studentNum));
}
}
neighborInfos.sort((a, b) => {
if (a.match !== b.match) {
return b.match - a.match; // match ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
} else if (a.empty !== b.empty) {
return b.empty - a.empty; // empty ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
} else if (a.r !== b.r) {
return a.r - b.r; // r ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
} else {
return a.c - b.c; // c ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
}
});
board[neighborInfos[0].r][neighborInfos[0].c] = studentNum; //์ตœ์ ์˜ ์œ„์น˜์— ์•‰ํžˆ๊ธฐ
}

// ํŠน์ • ์ž๋ฆฌ์˜ ์ธ์ ‘ ์ •๋ณด ๊ณ„์‚ฐ ํ•จ์ˆ˜
function getSeatInfo(r, c, studentNum){
let empty = 0;
let match = 0;
// ํ•™์ƒ ๋ฒˆํ˜ธ์— ๋งž๋Š” ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ๋“ค ์ฐพ๊ธฐ
let studentLikes = students.find(student => student.num === studentNum)?.likes || [];
for(let i = 0; i< 4; i++){
nr = r + dr[i];
nc= c + dc[i];
if(nr < 0 || nc < 0 || nr >= N || nc >= N) continue;
if (board[nr][nc] == 0) empty++;
else if(studentLikes.includes(board[nr][nc])) match++
}
return { r: r, c: c, empty: empty, match: match };
}

//๋งŒ์กฑ๋„ ์ฒ˜๋ฆฌ
function calcSatisfy(){
let result = 0;
for(let i = 0; i<N; i++){ //๋‚จ์•„์žˆ๋Š” ๋ชจ๋“  ์ž๋ฆฌ์˜ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘
for(let j =0; j<N; j++){
result += Math.floor(10 ** (getSeatInfo(i, j ,board[i][j]).match-1));
}
}
return result;
}

0 comments on commit 304cf75

Please sign in to comment.