πŸ’‘λ¬Έμ œ 뢄석 μš”μ•½

πŸ’‘μ•Œκ³ λ¦¬μ¦˜ 섀계

πŸ’‘μ½”λ“œ

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

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

const n = parseInt(input, 10); 

function isValid(sequence) {
    const len = sequence.length;

    // 같은 μˆ«μžκ°€ 3번 이상 연속인지 확인
    if (len >= 3 && sequence[len - 1] === sequence[len - 2] && sequence[len - 2] === sequence[len - 3]) {
        return false;
    }

    // λΆ€λΆ„ μˆ˜μ—΄ 쀑 1, 2, 3이 연속인지 확인
    if (len >= 3) {
        const lastThree = sequence.slice(-3);
        if (lastThree[0] !== lastThree[1] && lastThree[1] !== lastThree[2] && lastThree[0] !== lastThree[2]) {
            return false;
        }
    }

    return true;
}

function findGoodSequence(n, sequence = []) {
    if (sequence.length === n) {
        return sequence.join('');
    }

    for (let i = 1; i <= 3; i++) {
        sequence.push(i);
        if (isValid(sequence)) {
            const result = findGoodSequence(n, sequence);
            if (result) {
                return result;
            }
        }
        sequence.pop();
    }

    return null;
}

console.log(findGoodSequence(n));

πŸ’‘μ‹œκ°„ λ³΅μž‘λ„