프로그래머스 | [3차] 방금그곡

커몽·2021년 6월 30일
0

프로그래머스 level2

목록 보기
18/38
function solution(m, musicinfos) {
  var answer = [];
  let mm = m.replace(/(\D)#/g, (s, p) => p.toLowerCase());
  const getCode = (code, n) => {
    let temp = '';
    for (let i = 0; i < Math.floor(n / code.length); i++) temp += code;
    for (let i = 0; i < n % code.length; i++)temp += code[i];

    return temp;
  }
  const getPlayTime = (s, e) => {
    let [sh, sm] = s.split(':');
    let [eh, em] = e.split(':');
    let m = 0;
    if (eh - sh >= 1) {
      m += (eh - sh) * 60;
    }
    if (sm > em) {
      m -= Math.abs(em - sm);
    } else {
      m += Math.abs(em - sm);
    }
    return m;
  }

  for (let i = 0; i < musicinfos.length; i++) {
    let [start, end, title, code] = musicinfos[i].split(',');

    let codeT = code.replace(/(\D)#/g, (s, p) => p.toLowerCase());

    let playTime = getPlayTime(start, end);
    let playCode = getCode(codeT, playTime);
    if (playCode.includes(mm)) {
      answer.push([title, playTime]);
    }
  }
  if (answer.length === 0) {
    return `(None)`;
  }
  answer.sort((a, b) => b[1] - a[1]);
  return answer[0][0]

}

0개의 댓글