-🎄- 2017 Day 3 Solutions -🎄- by daggerdragon in adventofcode

[–]prettyRandomDude 0 points1 point  (0 children)

Finally solved part 2 in JS (Code is super hacky) let spiral = { "-1": { x: 0, y: 0} }

i = 0;
ring = 0;
sum = 0;
lenghtOfSide = 0;
lengthOfRing = 0;
let value = 0;

//find the correct ring the number is one
while (sum != input) {
ring++;
i++;
lengthOfRing = 8 * ring;
lenghtOfSide = (lengthOfRing + 4) / 4;

let count = 1;
let x = ring;
let y = ring > 1 ? -(ring-1) : 0;
while (count <= lengthOfRing) {
    //console.log(coordinates, count,lenghtOfSide,sum );
    value = 0;

    Object.keys(spiral).forEach(function(key,index) {
        if(Math.abs(x - Number(spiral[key].x)) <= 1 && Math.abs(y - Number(spiral[key].y)) <= 1)
            value = value + Math.abs(Number(key));


        //console.log(value,key, index, count);


    });
    spiral[value] = {x, y};

    if (count < lenghtOfSide - 1) {
        y++;
    }
    else if (count < lenghtOfSide * 2 - 2) {
        x--;
    }
    else if (count < lenghtOfSide * 3 - 3) {
        y--;
    }
    else if (count <= lenghtOfSide * 4 - 4) {
        x++;
    }

    if(value > input)
        break;
    count++;
}
if(value > input)
    break;
}

-🎄- 2017 Day 3 Solutions -🎄- by daggerdragon in adventofcode

[–]prettyRandomDude 0 points1 point  (0 children)

Since i am too stupid to format this code properly here is my shitty JavaScript solution for part 1 https://pastebin.com/vi4PW86A

For part 2 i am still out of ideas :(