Go๋ฏผ๋ณด๋‹ค Go

ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž

Create

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์ด๊ฒŒ ๋˜๋„ค...?

SleepingOff 2024. 6. 3. 09:16

๐Ÿพ๋“ค์–ด๊ฐ€๋ฉฐ

์š”์ฆ˜ ์•„์นจ์— ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ๋กœ ํ–ˆ๋Š”๋ฐ, ์–ด์ œ 2๋ฌธ์ œ ์ค‘ ํ•œ ๋ฌธ์ œ์—์„œ ๋ง‰ํ˜€ ๋‹ฌ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜ ๋ฐ”๋กœ ๋‹ค์‹œ ์‹œ๋„ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์•„๋ฌด๋ฆฌ ๋ฐ˜๋ก€๋ฅผ ์ฐพ์•„์„œ ๋„ฃ์–ด๋ด๋„ ํ†ต๊ณผ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค... ๋ถ„๋ช… 70์ ์ด ๋‚˜์˜ค๊ณ  ์žˆ๋Š”๋ฐ ๋ง์ด์ฃ ใ…Žใ…Ž ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์„ค๋งˆํ•˜๊ณ  ๊ณ ์น˜์ง€ ์•Š๊ณ  ์žˆ๋˜ ๋ถ€๋ถ„์„ ๋ฐ”๊ฟ”๋ณด์•˜๋Š”๋ฐ, ๋“œ๋””์–ด! ํ†ต๊ณผ๊ฐ€ ๋˜์–ด ๋ฟŒ๋“ฏํ•จ์— ๊ธ€๋กœ ๋‚จ๊น๋‹ˆ๋‹ค.

โœจ๋ณธ๊ฒฉ์ ์œผ๋กœ

๋ฌธ์ œ

์˜ค๋Š˜ ๋‚ ์งœ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ฌธ์ž์—ด today : YYYY.MM.DD
์•ฝ๊ด€์˜ ์œ ํšจ๊ธฐ๊ฐ„์„ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด terms : ์•ฝ๊ด€์ข…๋ฅ˜(A ~ Z) ์œ ํšจ๊ธฐ๊ฐ„ ( 1 ~ 100)
์ˆ˜์ง‘๋œ ๊ฐœ์ธ์ •๋ณด์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด privacies : ๋‚ ์งœ(YYYY.MM.DD) ์•ฝ๊ด€์ข…๋ฅ˜

ํŒŒ๊ธฐํ•ด์•ผ ํ•  ๊ฐœ์ธ์ •๋ณด์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด์— ๋‹ด์•„ return

์ฃผ์˜ํ•  ์ : 1 ≤ DD ≤ 28

์ฒ˜์Œ์— ์ฃผ์˜ํ•  ์ ์„ ๋ชป ๋ณธ ์ฑ„๋กœ ๊ณ„์‚ฐํ•œ๋‹ค๊ณ  ํ‹€๋ฆฌ๊ณ , 12๊ฐœ์›” ํ›„์— ์ผ์ž๊ฐ€ -1์ด ๋˜๋Š” ์ ์—์„œ ํ‹€๋ฆฌ๊ณ .. ์—ญ์‹œ ๋ฌธ์ œ๋ฅผ ์ž˜ ์ฝ์–ด๋ด์•ผ๊ฒ ๋‹ค๊ณ  ์˜ค๋Š˜๋„ ๋‹ค์งํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด ๊ณ„ํš

  • ์œ ํšจ๊ธฐ๊ฐ„ / 12 = ๋…„
  • ์œ ํšจ๊ธฐ๊ฐ„ % 12 = ์›”
  • ์œ ํšจ๊ธฐ๊ฐ„ -> 00.00์˜ ์ˆซ์ž๋กœ ์น˜ํ™˜
  • privacies์˜ YYYY.MM + 00.00 + ".DD" ์™€ today ๋น„๊ต
  • ์—ฐ๋„, ์›”, ๋‚ ์งœ ์ˆœ์„œ๋Œ€๋กœ ๋น„๊ตํ•˜๋ฉด์„œ break ๊ฑธ๊ธฐ

ํ’€์ด

  • ์ž…๋ ฅ๊ฐ’์„ ํŽธํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

์ž…๋ ฅ์œผ๋กœ today, terms, privacies๊ฐ€ ๋“ค์–ด์˜ค๋Š”๋ฐ, terms๊ฐ€ ๋ฐฐ์—ด์ด ์•„๋‹Œ ์•ž์— ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ์„ ํ‚ค๋กœ ๊ฐ€์ง„ ๊ฐ์ฒด๋กœ ํ•˜๋ฉด ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฏ€๋กœ, terms๋ฅผ ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

    terms = terms.map(t => t.split(" "));

    const obj = {};
    terms.forEach(t =>{
        obj[t[0]] = +t[1];
    })

๊ทธ๋ฆฌ๊ณ  today๋„ ๋น„๊ต๋ฅผ ์œ„ํ•ด ๋‹ค์Œ์ฒ˜๋Ÿผ ์ชผ๊ฐœ๋Š” ์ž‘์—…์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

    const [ty, tm, td] = today.split(".").map(t => +t);

์ด์ œ privacies๋ฅผ ๋Œ๋ฉด์„œ ๋‚ ์งœ๋ฅผ ๊ณ„์‚ฐํ•ด์ค๋‹ˆ๋‹ค.

 privacies.forEach((p,i)=>{
        const [year, month, day] = p[0].split(".").map(r => +r);
        const value = obj[p[1]];
        const idx = value % 12;
        let aday = day - 1;
        let amonth = month + idx;
        let ayear = year + Math.floor(value / 12);

        if(aday === 0){
            aday = 28;
            amonth -= 1;
        }
        if(amonth > 12){
            ayear = ayear + Math.floor(amonth / 12);
            amonth = amonth % 12 === 0 ? 12 : amonth % 12;
        }
        if(amonth <= 0){
            amonth = amonth + 12;
            ayear -= 1;
        }
    })

์—ฌ๋Ÿฌ if ๋ฌธ๋“ค์ด ๋ณด์ด์ง€๋งŒ ๋„˜์–ด๊ฐ€์ฃผ์„ธ์š”... while๋กœ ํ–ˆ๋‹ค๊ฐ€ ๋” ๋ณต์žกํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐํ•œ ๋‚ ์งœ์™€ ์˜ค๋Š˜ ๋‚ ์งœ๋ฅผ ๋น„๊ตํ•ด์ค๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋น„๊ต์™€ ๋”๋ถˆ์–ด ์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ•ฉ์ณ์„œ ๋ณด์—ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

function solution(today, terms, privacies) {
    const [ty, tm, td] = today.split(".").map(t => +t);

    terms = terms.map(t => t.split(" "));

    const obj = {};
    terms.forEach(t =>{
        obj[t[0]] = +t[1];
    })

    privacies = privacies.map(p => p.split(" "));

    const trash = [];

    privacies.forEach((p,i)=>{
        const [year, month, day] = p[0].split(".").map(r => +r);
        const value = obj[p[1]];
        const idx = value % 12;
        let aday = day - 1;
        let amonth = month + idx;
        let ayear = year + Math.floor(value / 12);

        if(aday === 0){
            aday = 28;
            amonth -= 1;
        }
        if(amonth > 12){
            ayear = ayear + Math.floor(amonth / 12);
            amonth = amonth % 12 === 0 ? 12 : amonth % 12;
        }
        if(amonth <= 0){
            amonth = amonth + 12;
            ayear -= 1;
        }

        //์˜ค๋Š˜๋‚ ์งœ์™€ ๋น„๊ตํ•˜๊ธฐ
         const isBigDay = td < aday;
         const isBigMonth = tm < amonth;
         const isBigYear = ty < ayear;
         switch(true){
             case isBigYear:
                 break;
             case isBigMonth:
                 if(ty === ayear) break;
             case isBigDay:
                 if(tm === amonth) break;
             default:
                 if(ty === ayear && tm === amonth && td === aday){}
                 else trash.push(i + 1)
         }
    })
    return trash;
}

 

๋ฌธ์ œ๋Š”... ์•„๋ฌด๋ฆฌ ๋ฐ˜๋ก€๋ฅผ ์ถ”๊ฐ€ํ•ด๋„ ๋งˆ์น˜ ๋ฒฝ์ด ์žˆ๋Š” ๊ฒƒ๋งˆ๋ƒฅ ํ†ต๊ณผ๊ฐ€ ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค..

์ถ”๊ฐ€ํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋“ค - ์ค‘๊ฐ„์— ํ•จ์ •์ด ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋น„๊ตํ•˜๋Š” ๋ถ€๋ถ„์ด ๋ฌธ์ œ๊ฒ ๊ตฌ๋‚˜..ํ•˜๊ณ  ๋ฏธ๋ค„๋‘๋˜ ์ˆ™์ œ๋ฅผ ๊บผ๋ƒˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์ˆซ์ž๋กœ ์—ฐ๋„, ๋‹ฌ, ์ผ์„ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ๊ฐ‘์ž๊ธฐ ์ข‹์€ ์•„์ด๋””์–ด๊ฐ€ ๋– ์˜ฌ๋ž๋Š”๋ฐ, ๋ฐ”๋กœ, ๋ฌธ์ž์—ด๋กœ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

//๊ธฐ์กด ์ฝ”๋“œ
 const isBigDay = td < aday;
 const isBigMonth = tm < amonth;
 const isBigYear = ty < ayear;
 switch(true){
     case isBigYear:
         break;
     case isBigMonth:
         if(ty === ayear) break;
     case isBigDay:
         if(tm === amonth) break;
     default:
         if(ty === ayear && tm === amonth && td === aday){}
         else trash.push(i + 1)
 }
 
 //๋ฌธ์ž์—ด๋กœ ๋น„๊ตํ•˜๊ธฐ
const after =`${ayear}.${String(amonth).padStart(2, "0")}.${String(aday).padStart(2, "0")}`;
const between = [today, after];
between.sort();
if(between[1] === today && today != after) trash.push(i+1)

ํ›จ์”ฌ ๋” ๊ฐ„๋‹จํ•ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ .. ๋ฌธ์ œ๋„ ํ†ต๊ณผํ–ˆ์Šต๋‹ˆ๋‹คใ…Žใ…Ž

 

โœ”๏ธ๊ฒฐ๋ก 

๋น„๊ต ์กฐ๊ฑด์ด ์ €ํ•œํ… ์ข€ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ๋งค๋ฒˆ ์ƒ๊ฐํ•  ๋•Œ๋งˆ๋‹ค ๋” ๋ณต์žกํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—” ํ˜•๋ณ€ํ™˜์„ ํ†ตํ•ด์„œ ์‰ฝ๊ฒŒ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊นจ๋‹ฌ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

728x90