풀이
김영준
// 개인정보는 유효기간이 정해져있다. // 유효기간이 지나면 파기 // 모든 달은 28일까지 있다. // 파기해야 할 개인정보의 번호를 오름차순을 통해 배열로 리턴 function solution(today, terms, privacies) { const afterArr = []; // 합산 된 날짜를 담는 배열 const result = []; // 결과를 담는 배열 // 약관 종류에 따른 유효 기간을 반환하는 함수 const termCheck = (crtTerm) => { for(let i = 0; i < terms.length; i ++){ const filterTerm = terms.filter((term) => crtTerm === term[0]); return Number(filterTerm.join("").slice(2)); } } for(let i = 0; i < privacies.length; i ++){ const privacy = privacies[i].split(" "); // 공백을 기준으로 나눔 let [startDay, term] = privacy; // 수집 날짜와 약관 종류 const expDay = termCheck(term); // 약관 종류를 전달하여 유효 기간 반환 let year = Number(startDay.substr(0,4)); let month = Number(startDay.substr(5,2)); let day = Number(startDay.substr(8,2)); // day가 1보다 작으면 28로 지정하고 month를 감소 if(day - 1 < 1){ day = 28; if(month - 1 < 1){ month = 12; year--; } else month--; } else day--; // 수집 기간과 유효 기간을 더해서 12가 넘으면 연도 증가 if(month + expDay >= 12){ year+= Math.floor((month + expDay) / 12); // 만약 12 24 36...이면 0이 아니라 12월이 되어야 함 if((month + expDay) % 12 === 0) { year--; month = 12; } else month = (month + expDay) % 12; } else month = month + expDay; // 한 글자면 앞에 0을 붙임 afterArr.push(`${year}-${month.toString().padStart(2,"0")}-${day.toString().padStart(2,"0")}`); } const nowDay = new Date(today.split(".").join("-")) // .을 -로 구분하게 변경 후 Date 타입으로 변경 // 합산된 날짜가 현재 날짜보다 작으면 유효 기간이 지났으니 result에 index를 push afterArr.forEach((date, i) => { new Date(date) < nowDay ? result.push(i+1) : null }) return result ; } // 풀고 보니까 왜 이렇게 길지...?
이종현
function solution(today, terms, privacies) { const answer = []; const map = new Map(); const day = today.split('.').map(Number); const daySum = (day[0] * 12 * 28) + (day[1] * 28) + day[2]; for (let i = 0; i < terms.length; i++) { const arr = terms[i].split(' '); map.set(arr[0], +arr[1]); } for (let i = 0; i < privacies.length; i++) { const arr = privacies[i].split('.'); const alpabet = arr[2].split(' ') console.log(arr, alpabet) const sum = (arr[0] * 12 * 28) + (arr[1] * 28) + +alpabet[0] + (map.get(alpabet[1]) * 28); if (daySum >= sum) answer.push(i + 1); } return answer; }
박노철
function solution(today, terms, privacies) { const [ty, tm, td]=today.split(".").map(v=>v*1); /* 약관의 종류는 여러가지, 각 약관마다 유효기간이 있다. 모든 달은 28일까지, 오늘날짜는 yyyy.mm.dd 이다. 약관은 중복x, 유효기간은 달 1<= <=100, privacies 수집일, 종류 파기해야할 개인정보의 번호를 오름차순으로 리턴 */ //terms를 객체롤 변환 const termsObj={}; for(let a of terms){ let [type, months]=a.split(" "); termsObj[type]=months*28; } // 모든것을 다 day기준으로 바꾸고 비교한다. const answer=[]; const totalToday=(ty*12*28)+(tm*28)+td; privacies.forEach((a,index)=>{ let [day, type]=a.split(" "); let [y, m, d]=day.split(".").map(v=>v*1); const plusDays=termsObj[type]; const totalDay=(y*12*28)+(m*28)+d+plusDays; //만약 현재가 더 크다면 계약기간이 지난것이다. if(totalToday>=totalDay)answer.push(index+1) }) return answer }
이민희
function solution(today, terms, privacies) { const termsMap = terms.reduce((obj, term) => { const [termType, termDuration] = term.split(' ') obj[termType] = +termDuration return obj }, {}) // { A: 6, B: 12, C: 3 } const result = []; privacies.forEach((privacy, index) => { const [startDate, termType] = privacy.split(' ') const [startY, startM, startD] = startDate.split('.').map(str => +str) const sumMonth = startM + termsMap[termType] - (startD === 1 ? 1 : 0) // 시작 달 + 유효기간 const expireY = startY + (sumMonth % 12 === 0 ? parseInt(sumMonth / 12) - 1 : parseInt(sumMonth / 12)) const expireM = sumMonth % 12 || 12 const expireD = startD === 1 ? 28 : startD - 1 if (new Date(today) > new Date(`${expireY}.${expireM}.${expireD}`)) result.push(index + 1) }) return result }
박건우
// 모든 달은 28일까지 // 약관 종류 - 번호 매칭 // 파기해야할 개인정보 번호를 오름차순으로 function solution(today, terms, privacies) { const result = [] const termsMemory = {} // 날짜 환산함수입니다. const translateDate = (date) => { const [year, month, day] = date.split('.').map(v => + v) return year * 12 * 28 + month * 28 + day } // 매개변수를 입맛에 맞게 모두 변형해줍니다. today = translateDate(today) terms = terms.map(term => term.split(' ')) privacies = privacies.map(privacy => privacy.split(' ')) // 약관 타입에 따라 기간을 환산해서 저장해줍니다. for(let [type, month] of terms) { termsMemory[type] = month * 28 } // 파기할 정보를 찾습니다 privacies.forEach(([date, type], index) => { const deadline = translateDate(date) + termsMemory[type] // 크기 비교 생각보다 헷갈리네요 같은 것도 파기해줍니다 if(today >= deadline) { result.push(index + 1) } }) return result.sort((a, b) => a - b); // 디폴트 오름차순이라고 콜백 안주면 에러나네요 } // 개인적으로 1레벨 중 가장 어려운 문제였습니다.
박주연
function solution(today, terms, privacies) { const answer = []; const [ty, tm, td] = today.split('.').map(v=>Number(v)); const todayDate = ty * 12 * 28 + tm * 28 + td; const termInfo = {}; for(let i of terms){ const [type, month ] = i.split(" "); termInfo[type] = Number(month); } for (let j = 0; j < privacies.length; j++){ const [privacyDate, type] = privacies[j].split(" "); const [y, m, d] = privacyDate.split('.').map(v => Number(v)); const expirationDate = y * 12 * 28 + (m + termInfo[type]) * 28 + d; if (expirationDate <= todayDate){ //원래는 이렇게 코드를 짰는데 10,12,15,18,20이 계속 실패떠서 찾아보니 //privacies에 동일한 요소가 있으면 생기는 오류더라고요! 그래서 indexOf 안쓰는 방향으로 수정했습니다 //answer.push(privacies.indexOf(j)+1); answer.push(j+1); } } return answer; }