[21/11/01 KATA NINJA] ๋ฐฉ๊ธˆ ๊ทธ๊ณก

36922 ๋‹จ์–ด codekatacodekata

๋ฌธ์ œ

๊ธฐ์กด ์ฝ”๋“œ

const getPlayTime = (start, finish) => {
  const [sHour, sMin] = start.split(":");
  const [fHour, fMin] = finish.split(":");
  const pHour = +fHour - +sHour;
  const pMin = +fMin - +sMin;

  return pHour * 60 + pMin;
};
const getMelody = (time, info) => {
  let result = "";
  let index = 0;
  for (let check = 0; check < time; check++) {
    if (index === info.length) {
      index = 0;
    }
    result += info[index];
    index++;
  }
  return result;
};
function solution(m, musicinfos) {
  var result = [];
  let musics = [];
  musicinfos.forEach((music) => {
    let [start, finish, title, info] = music.split(",");
    const playTime = getPlayTime(start, finish);
    info = info.replace(/C#/gi, "H");
    info = info.replace(/D#/gi, "I");
    info = info.replace(/F#/gi, "J");
    info = info.replace(/G#/gi, "K");
    info = info.replace(/A#/gi, "L");
    const melody = getMelody(playTime, info);
    musics.push({ start, finish, title, info, melody, playTime });
  });
  m = m.replace(/C#/gi, "H");
  m = m.replace(/D#/gi, "I");
  m = m.replace(/F#/gi, "J");
  m = m.replace(/G#/gi, "K");
  m = m.replace(/A#/gi, "L");
  musics.forEach((music) => {
    if (music.melody.includes(m)) {
      result.push(music);
    }
  });
  if (result.length > 1) {
    result.sort((a, b) => {
      if (a.playTime > b.playTime) {
        return -1;
      }
      if (a.playTime < b.playTime) {
        return 1;
      }
    });
  }
  return result.length > 0 ? result[0].title : "(None)";
}

ํ’€์ด

  • ์†ŒํŠธ ์—ญ์ˆœ์ธ์ง€ ํ™•์ธํ•  ๊ฒƒ. (๋‚ด๋ฆผ์ฐจ์ˆœ์ธ์ง€ ์˜ค๋ฆ„์ฐจ์ˆœ์ธ์ง€)

  • #์Œ๋“ค์€ ํŠน์ˆ˜ํ•˜๊ฒŒ ๋ฐ”๊พธ์ž

function solution(m, musicinfos) {
    const answer = [];
    
    const memorize = getMusicNote(m);
    
    musicinfos.forEach((musicInfo)=>{
        const [s,f,title,note] = musicInfo.split(',');
        const min = getMin(s,f);
        const musicNote = getMusicNote(note)
        const musicNoteByTime = getPlayNoteByTime(musicNote,min,musicNote.length);
        
        if(isWantMusic(musicNoteByTime,memorize)){
            answer.push([title,min]);   
        }
        
    })
    answer.sort(([_,aTime],[__,bTime]) => +bTime - +aTime);
    
    return answer.length === 0 ? '(None)' : answer[0][0];
    
    function getMin(s,f){
        const [sH,sM] = s.split(":");
        const [fH,fM] = f.split(":");
        return (+fH - +sH) * 60 + (+fM - +sM);
    }
    function getMusicNote(note){
        const array = note.split("");
        
        array.forEach((i,idx)=>{
            if(i === '#'){
                array[idx-1] = array[idx-1].toLowerCase();
                array[idx-1] += '#'
                array.splice(idx,1);
            }
        })
        return array;
    }
    function getPlayNoteByTime(note,time,length){
        const array = [];
        for(let i=0;i<time;i++){
            array.push(note[i%length]);
        }
        return array;
    }
    function isWantMusic(note,memo){
        const noteStr = note.join('')
        const memoStr = memo.join('')
        

        return noteStr.includes(memoStr);
    }
}

๋‹ค๋ฅธ ํ’€์ด

์ •๊ทœํ‘œํ˜„์‹์„ ๋” ์ด์šฉํ•œ ์ฝ”๋“œ

function solution(m, musicinfos) {
    const answer = [];
    
    const memorize = getMusicNote(m);
    
    musicinfos.forEach((musicInfo)=>{
        const [s,f,title,note] = musicInfo.split(',');
        const min = getMin(s,f);
        const musicNote = getMusicNote(note)
        const musicNoteByTime = getPlayNoteByTime(musicNote,min,musicNote.length);
        
        
        if(isWantMusic(musicNoteByTime,memorize)){
            answer.push([title,min]);   
        }
        
    })
    answer.sort(([_,aTime],[__,bTime]) => +bTime - +aTime);
    
    return answer.length === 0 ? '(None)' : answer[0][0];
    
    function getMin(s,f){
        const [sH,sM] = s.split(":");
        const [fH,fM] = f.split(":");
        return (+fH - +sH) * 60 + (+fM - +sM);
    }
    function getMusicNote(note){
      // #์ด ๋“ค์–ด๊ฐ€๋Š” ์Œ์„ #์„ ์ง€์šฐ๊ณ , ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž๋กœ ๋Œ€์น˜
        return note.replace(/[CDFGA]#/gi,(match)=>match.toLowerCase()[0]);
    }
    function getPlayNoteByTime(note,time,length){
        let str = '';
        for(let i=0;i<time;i++){
            str += note[i % length];
        }
        return str;
    }
    function isWantMusic(note,memo){
        console.log(note,memo);
        return note.includes(memo);
    }
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ