r/adventofcode • • Dec 08 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 8 Solutions -๐ŸŽ„-

--- Day 8: I Heard You Like Registers ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hintsยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

23 Upvotes

348 comments sorted by

View all comments

1

u/COOLGAMETUBE Dec 08 '17

JavaScript // === Day 08 === // Copy the code into the // console of your input page let vm={}; vm.input=document.body.innerText; vm.data={}; vm.requiredAllocation=0; function getVal(x) {if(Number.isNaN(Number(x))) return ((vm.data[x]==null)?vm.data[x]=0:vm.data[x]); else return x;} for(let d of vm.input.split("\n")) if(d!="") { let c = d.split(" ");

    // Error Handling
    if(c.length>0) {
        if(c.length<=3||c[3]=="if") {
            if((">:<:>=:<=:==:!=").split(":").indexOf(c[5]) == -1) {
                console.log("Unknown operator: "+c[5]);
                break;
            } else {
                if(("inc:dec").split(":").indexOf(c[1]) == -1) {
                    console.log("Unknown action: "+c[1]);
                    break;
                }
            }
        } else {
            console.log("Condition without IF error! "+(c.length<=3||c[3]=="if"));
            break;
        }
    } else {
        console.log("No input error!");
        break;
    }

    // Parse    
    let v = c[0];
    let a = c[1];
    let p = c[2];

    if(!(c.length>3&&c[3]=="if")||(new Function("return "+getVal(c[4])+c[5]+c[6]))()) {
        let i = getVal(p); if(a=="dec") i=(-i);
        vm.data[v]=Number(getVal(v))+Number(i);
        if(getVal(v)>vm.requiredAllocation)     vm.requiredAllocation=vm.data[v];
    }
}

// Results

console.log((function(){
    let l=null;
    for(let o in vm.data) if(l==null||vm.data[l]<vm.data[o]) l=o;
    return ("Largest register: "+l+" = "+vm.data[l]+"\nRequired allocation: "+vm.requiredAllocation);
})());

1

u/MyGoodStrayCatFriend Dec 09 '17

Hey bub your preformatted text broke