๐Ÿ’ก๋ฌธ์ œ ๋ถ„์„ ์š”์•ฝ

๐Ÿ’ก์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๊ณ„

์ ˆ์ฐจ

๐Ÿ’ก์ฝ”๋“œ


const fs = require('fs');
const path = require('path');

const input = fs.readFileSync(path.join(__dirname, 'input.txt')).toString().trim().split('\\n');

// ๋ฐฐ์—ด ๋ถ„ํ•ด
let [n, ...arr] = input; 
// n์€ ํ…Œ์ŠคํŠธ ์ˆ˜, ๋‚˜๋จธ์ง€๋Š” ํ…Œ์ŠคํŠธ ์ •๋ณด

// ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ์ˆซ์ž ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
arr = arr.map((item) => item.split(' ').map(Number));
let answer = '';

for (let i = 0; i < arr.length; i += 2) {
  let count = 0; // ์ถœ๋ ฅ๋œ ๋ฌธ์„œ์˜ ์ˆ˜
  const priorities = arr[i + 1]; // ํ˜„์žฌ ํ…Œ์ŠคํŠธ์˜ ์šฐ์„ ์ˆœ์œ„ ๋ฐฐ์—ด
  let location = arr[i][1]; // ๋ชฉํ‘œ ๋ฌธ์„œ์˜ ์ดˆ๊ธฐ ์œ„์น˜

  while (true) {
    // ํ˜„์žฌ ํ์—์„œ ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„ ๋ฌธ์„œ๋ฅผ ์ฐพ๊ธฐ
    const max = Math.max(...priorities);

    // ํ ์•ž์—์„œ ๋ฌธ์„œ ํ•˜๋‚˜ ๊บผ๋‚ด๊ธฐ
    const number = priorities.shift();

    // ๊บผ๋‚ธ ๋ฌธ์„œ์˜ ์ค‘์š”๋„(number)๊ฐ€ ์ตœ๋Œ€ ์ค‘์š”๋„(max) ๋น„๊ต
    if (number === max) {
      count++;

      // ๋ชฉํ‘œ ๋ฌธ์„œ์˜ ์œ„์น˜๊ฐ€ 0์ผ ๊ฒฝ์šฐ
      if (location === 0) {
        //ํ˜„์žฌ ์ถœ๋ ฅ๋œ ๋ฌธ์„œ ์ˆ˜(count)๋ฅผ ์ •๋‹ต ๋ฐฐ์—ด์— ์ถ”๊ฐ€
        answer += count + '\\n';
        break;
      }

    } else {
      // ๊บผ๋‚ธ ๋ฌธ์„œ์˜ ์ค‘์š”๋„๊ฐ€ ์ตœ๋Œ€๋ณด๋‹ค ๋‚ฎ์œผ๋ฉด ํ์˜ ๋งจ ๋’ค์— ๋‹ค์‹œ ์ถ”๊ฐ€
      priorities.push(number);
    }

    // ํ˜„์žฌ ๋ฌธ์„œ์˜ ์œ„์น˜๊ฐ€ 0์ผ ๊ฒฝ์šฐ
    if (location === 0) {
      // ํ์—์„œ ๋งˆ์ง€๋ง‰ ๋ฌธ์„œ๋กœ ์œ„์น˜ ์˜ฎ๊ธฐ๊ธฐ
      location = priorities.length - 1;

    } else {
      // ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋‹จ์ˆœํžˆ 1 ๊ฐ์†Œ
      location--;
    }
  }
}

console.log(answer.trim());

๐Ÿ’ก์‹œ๊ฐ„ ๋ณต์žก๋„