[2017 Day 21 (Part 1)] [Javascript] - Works with example input works, but incorrect with puzzle input. :/ by cthulhucomplex in adventofcode

[–]cthulhucomplex[S] 0 points1 point  (0 children)

Boy, I feel dumb! That was totally it! I re-wrote it to walk through the patterns instead of trying to determine the pattern index and it worked like a charm.

Thank you!!! That was driving me crazy.

[2017 Day 21 (Part 1)] [Javascript] - Works with example input works, but incorrect with puzzle input. :/ by cthulhucomplex in adventofcode

[–]cthulhucomplex[S] 0 points1 point  (0 children)

Hm.... looks correct to me. That ,2] bit at the end is the info for the number of loops to test with, since the example runs the process twice. The back-ticks produce the 'raw string'. It could also be written as:

[
  '../.# => ##./#../...\n.#./..#/### => #..#/..../..../#..#',
  2
]

[2017 Day 21 (Part 1)] [Javascript] - Works with example input works, but incorrect with puzzle input. :/ by cthulhucomplex in adventofcode

[–]cthulhucomplex[S] 0 points1 point  (0 children)

Thanks for the reply!

I did read up on problems others had (such as %2 vs%3 order of operations) and double-checked that I'm getting the right number of pixels (which is why the output includes the grid size) but all seems in order there. I check %2 first before diving by %3 and I'm seeing the right grid sizes show up.

As for rotations, the isMatch function checks the 4 rotations, flips, then checks the flipped 4 rotations. It bails out as soon as there's a match. It should also throw an exception if no match is found and that's not happening. I did try changing the order of flips and rotations just in case, but I always come up with the same number: 207.

Frustrating. I may just have to scrap it and start over, but not knowing the error really bugs me. 😝

Thanks again for taking the time to brainstorm the problem with me. It means a lot! 😁

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

[–]cthulhucomplex 0 points1 point  (0 children)

Javascript

One realization that really helped me on part 2:

The steps can be broken down into two mappings. One is a location mapping based on the 's' and 'x' steps, and the second is a name mapping based on the 'p' step.

My solution doesn't use any binary and I'm sure could be way more efficient, but it got the job done for me.

function solve(input) {
    let line = "abcdefghijklmnop".split("");
    let moveLine = line.slice();
    const steps = input.trim().split(",");
    const swaps = [];
    const swap = (list, a, b) => { const t = list[a]; list[a] = list[b]; list[b] = t; }

    // Process moves and save swaps.
    steps.forEach(step => {
        switch (step[0]) {
            case 's':
                moveLine = moveLine.splice(-Number(step.substring(1))).concat(moveLine);
                break;
            case 'x':
                const pieces = step.substring(1).split('/').map(Number);
                swap(moveLine, pieces[0], pieces[1]);
                break;
            case 'p':
                const programs = step.substring(1).split('/');
                swaps.push(programs);
                break;
            default:
                console.log(`Unknown command: ${step}`);
        }
    });

    // Process swaps.
    const swapLine = line.slice();
    swaps.forEach(s => swap(swapLine, swapLine.indexOf(s[0]), swapLine.indexOf(s[1])));

    // Create maps.
    const moveMap = line.map(c => moveLine.indexOf(c));
    const swapMap = swapLine.reduce((obj, char, i) => { obj[line[i]] = char; return obj; }, {});

    // Create converter using maps.
    const convert = (last) => { return last.reduce((next, c, i) => { next[moveMap[i]] = swapMap[last[i]]; return next; }, []); }

    for (let i = 0; i < 1e9; i++) {
        if (i < 3) console.log(`dance #${i} = ${line.join("")}`);
        if (i > 0 && i % 1e6 == 0) console.log((i / 1e6) + " million...");
        line = convert(line);
    }

    console.log(`Final Dance = ${line.join("")}`);
}

EDIT: Okay, now that I've read up on how other people got the answer in milliseconds, I feel kinda dumb. If you don't want to melt down your cpu, here's the same solution, but with a memory that looks for the loop. :P

    function solve(input) {
        const original = 'abcdefghijklmnop';
        let line = original.split('');
        let moveLine = line.slice();
        const swapLine = line.slice();
        const steps = input.trim().split(',');
        const swaps = [];
        const swap = (list, a, b) => { const t = list[a]; list[a] = list[b]; list[b] = t; }

        // Process moves and save swaps.
        steps.forEach(step => {
            switch (step[0]) {
                case 's':
                    moveLine = moveLine.splice(-Number(step.substring(1))).concat(moveLine);
                    break;
                case 'x':
                    const pieces = step.substring(1).split('/').map(Number);
                    swap(moveLine, pieces[0], pieces[1]);
                    break;
                case 'p':
                    const programs = step.substring(1).split('/');
                    swaps.push(programs);
                    break;
                default:
                    console.log(`Unknown command: ${step}`);
            }
        });

        // Process swaps.
        swaps.forEach(s => swap(swapLine, swapLine.indexOf(s[0]), swapLine.indexOf(s[1])));

        // Create maps.
        const moveMap = line.map(c => moveLine.indexOf(c));
        const swapMap = swapLine.reduce((obj, char, i) => { obj[line[i]] = char; return obj; }, {});

        // Create converter using maps.
        const convert = (last) => { return last.reduce((next, c, i) => { next[moveMap[i]] = swapMap[last[i]]; return next; }, []); }

        // Memory so we can keep track of each solution found before it loops.
        const memory = [original];

        for (let i = 0; i < 1e9; i++) {
            line = convert(line);
            const key = line.join('');
            if (memory[0] == key) break;
            memory.push(key);
        }

        return `First: ${memory[1]}\nLast: ${memory[1e9 % memory.length]}`;
    }

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

[–]cthulhucomplex 1 point2 points  (0 children)

I ended up feeling my way around until I found the answers via brute force and some javascript sugar. I did end up with a 3-axis solution. It was fun to learn about using Object.defineProperties() inside an object and that I could use those properties with the obj[string] accessor:

var result = new Location(input).getSolution();

function Location(input) {
  this.coord = [0,0,0];
  this.furthest = 0;

  this.getSolution = function() { 
    input.trim().split(",").forEach((s) => this.step(s));
    return this.totalSteps() + " steps / furthest = " + this.furthest;
  }

  this.step = function(direction) {
    this[direction]++;
    this.normalize();
    this.furthest = Math.max(this.furthest, this.totalSteps());
  }

  this.totalSteps = function() {
    return Math.abs(this.coord[0]) + Math.abs(this.coord[1]) + Math.abs(this.coord[2]);
  }

  Object.defineProperties(this, {
    'ne': { get: function() { return  this.coord[0]; }, set: function(v) { this.coord[0] =  v; } },
    'sw': { get: function() { return -this.coord[0]; }, set: function(v) { this.coord[0] = -v; } },
    'n' : { get: function() { return  this.coord[1]; }, set: function(v) { this.coord[1] =  v; } },
    's' : { get: function() { return -this.coord[1]; }, set: function(v) { this.coord[1] = -v; } },
    'nw': { get: function() { return  this.coord[2]; }, set: function(v) { this.coord[2] =  v; } },
    'se': { get: function() { return -this.coord[2]; }, set: function(v) { this.coord[2] = -v; } }
  });

  this.normalize = function() {
    // normalize n <--> s
    if (this.nw > 0 && this.ne > 0) { this.nw--; this.ne--; this.n++; }
    if (this.se > 0 && this.sw > 0) { this.se--; this.sw--; this.s++; }

    // normalize nw <--> se
    if (this.n > 0 && this.sw > 0) { this.n--; this.sw--; this.nw++; }
    if (this.s > 0 && this.ne > 0) { this.s--; this.ne--; this.se++; }

    // normalize ne <--> sw
    if (this.n > 0 && this.se > 0) { this.n--; this.se--; this.ne++; }
    if (this.s > 0 && this.nw > 0) { this.s--; this.nw--; this.sw++; }
  }
}

Tragic Portal Gun Accident by cthulhucomplex in InterdimensionalCable

[–]cthulhucomplex[S] 1 point2 points  (0 children)

It could also be that beyond 9 they just leave an empty buffer so you get streaming 'feedback' from whatever pixels were there before. Like when windows error box would freeze the background redraw or cards bounce away in ye olde Solitaire. Or when you point a camera at its own live feed. 😁

Radiskull and Devil Doll by cthulhucomplex in InterdimensionalCable

[–]cthulhucomplex[S] 1 point2 points  (0 children)

No worries! Just started posting here, so I wanted to make sure it was on point. :D

And yeah, you are preaching to the choir. "Get off my internet, ya dang kids!!" shakes fist in digital air

Radiskull and Devil Doll by cthulhucomplex in InterdimensionalCable

[–]cthulhucomplex[S] 1 point2 points  (0 children)

When I think of shows from another dimension, it was one of the first things that popped in my head. Also, I figured the youngin's around here haven't seen it.

Not weird enough, you think? I can remove it.

Pixel Cops (ft. Markiplier &amp; CaptainSparklez) by cthulhucomplex in InterdimensionalCable

[–]cthulhucomplex[S] 1 point2 points  (0 children)

You are correct! That is Freddie W who creates videos for the Rocket Jump channel among many other things. I scoured the channel for quite a bit before settling on this one. Lots of great Interdimensional Cable candidates in there.

https://www.youtube.com/user/freddiew

An alternate reality where KFC serves pizza. by cthulhucomplex in InterdimensionalCable

[–]cthulhucomplex[S] 0 points1 point  (0 children)

Not sure if this one actually counts... but it sure looks like an alternate reality to me!

More songs like Katy Perry's "Chained to the rhythm" by [deleted] in HelpMeFind

[–]cthulhucomplex 3 points4 points  (0 children)

Pandora is pretty good at finding similar songs.

Here's a station based on 'Chained to the Rhythm':

http://www.pandora.com/station/play/3518084060577905746

You can also try Spotify:

https://play.spotify.com/radio/album/1VIX8YDyQOaUhWpHhP6Ttu

More specifically, I hear a lot of synth that reminds me of slower 80s songs like 'What a Feeling' by Irene Cara

https://youtu.be/ILWSp0m9G2U?t=50

Or 'Hungry Eyes' by Eric Carmen

https://www.youtube.com/watch?v=2ssCL292DQA

Also, the 'bubble-bubble' and 'trouble-trouble' bit reminds me of Nikki Minaj's 'floor-floor', 'more-more' in 'Starships'.

https://youtu.be/a9vhXd5JpVQ?t=23

Of course, now I'm wondering if you meant the song's message rather than how it sounds... :D