DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 5: Sunny with a Chance of Asteroids

Collapse
 
mellen profile image
Matt Ellen • Edited

I wrote lots of bugs and ended up with ugly code. Sorry for this mess:

function process()
{
    let input = 5; //input = 1; /* for part 1 */
    const pretext = document.getElementsByTagName('pre')[0].innerHTML;
    let instructions = pretext.split(',');
    let instructionLength = 0;
    for(let i = 0; i < instructions.length; i += instructionLength)
    {
        let inst = instructions[i].padStart(5, '0');
        if(inst === '00099')
        {
            break;
        }

        let opcode = inst.slice(-2);
        let parameterModes = inst.substr(0,3).split('').reverse();

        if(['01', '02', '07', '08'].indexOf(opcode) > -1)
        {
            instructionLength = 4;

            let values = [0,0];

            for(let pi = 0; pi < values.length; pi++)
            {
                values[pi] = parseInt(instructions[i+pi+1], 10);
                if(parameterModes[pi] === '0')
                {
                    values[pi] = parseInt(instructions[values[pi]], 10);
                }
            }

            let answerPos = parseInt(instructions[i+3], 10);

            if(opcode === '01')
            {
                instructions[answerPos] = (values[0] + values[1]).toString();
            }
            else if(opcode === '02')
            {
                instructions[answerPos] = (values[0] * values[1]).toString();
            }
            else if(opcode === '07')
            {
                instructions[answerPos] = values[0] < values[1] ? '1' : '0';
            }
            else if(opcode === '08')
            {
                instructions[answerPos] = values[0] === values[1] ? '1' : '0';
            }
        }
        else if(opcode === '05' || opcode == '06')
        {
            instructionLength = 3;
            let parameterModes = inst.substr(0,3).split('').reverse();
            let value = parseInt(instructions[i+1]);
            if(parameterModes[0] == 0)
            {
                value = parseInt(instructions[value], 10);
            }
            let point = parseInt(instructions[i+2]);
            if(parameterModes[1] == 0)
            {
                point = parseInt(instructions[point], 10);
            }

            if(opcode === '05' && value !== 0)
            {
                i = point;
                instructionLength = 0;
            }

            if(opcode === '06' && value === 0)
            {
                i = point;
                instructionLength = 0;
            }

        }
        else
        {
            instructionLength = 2;
            let position = parseInt(instructions[i+1], 10);

            if(opcode === '03')
            {
                instructions[position] = input.toString();
            }
            else if(opcode === '04')
            {
                console.log(instructions[position]);
            }
        }
    }

    return instructions[0];
}

I like how this can still be used to solve day 2 😁

Collapse
 
jbristow profile image
Jon Bristow

Ooh, now I have to load day 2’s code. 🤩