Kiosk 10: The Elf Code

This challenge was a cool set of javascript challenges in which you have to direct an elf to the next stage by grabbing all the lollipops and bypassing the evil munchkin challenges. Since each one required a set of code, I will simply post the code I used to get through each stage. You can submit commands to the elf by running elf.moveUp(5) for example, to move the elf 5 boxes in the 'up' direction.

As the stages got more and more difficult, it involved several challenges posed by switches and munchkins that wouldn't let you pass until you solved their challenge.

To make it even more difficult, each stage would restrict you more and more by how many 'elf commands' you can issue. For example, you can't issue something like:

elf.moveUp(1);
elf.moveLeft(1);
elf.moveUp(1);
elf.moveLeft(1);
elf.moveUp(1);
elf.moveLeft(1);
// etc

...to do something like move in a diagonal direction if you were only allowed to run 4 elf commands total! To fix that, you had to use loops to lower the amount of times you would issue a move command. So to move diagonal up 3 and left 3, you could do something like:

for (var i=0;i<3;i++) {
  elf.moveUp(1);
  elf.moveLeft(1);
}

I have replicated the first code block by only issuing 2 elf commands rather than 6!

Other restrictions included the amount of code in general you could write, like you couldn't exceed 5 lines of code or something. In this case I used a Javascript Minifier to compress code into a barely-legible format but was still executable. I included some of my minified code below.

This kiosk was located in the corner of the Dining Room next to Ribb Bonbowford:

Elf Code

Anyway, here are the solutions!

Stage 1: The Beginning

Stage 1

elf.moveLeft(10);
elf.moveUp(10);

Stage 2: Trigger The Yeeter

Stage 2

elf.moveLeft(6);
elf.pull_lever(elf.get_lever(0)+2);
elf.moveLeft(4);
elf.moveUp(10);

Stage 3: Move To Loopiness

Stage 3

elf.moveTo(lollipop[0])
elf.moveTo(lollipop[1])
elf.moveTo(lollipop[2])
elf.moveUp(1)

Stage 4: Up Down Loopiness

Stage 4

for (i=0; i<=2; i++){
    elf.moveLeft(3);
    elf.moveUp(20);
    elf.moveLeft(3);
    elf.moveDown(20);
}

Stage 5: Move To Madness

Stage 5

Two separate blocks, one easy to read and another minified:

elf.moveTo(lollipop[1]);
elf.moveTo(lollipop[0]);
var res_array = [];
elf.ask_munch(0).forEach(function(item) {
    if (Number.isInteger(item)) {
        res_array.push(item);
    }
});
elf.tell_munch(res_array);
elf.moveUp(2)

Minified:

elf.moveTo(lollipop[1]),elf.moveTo(lollipop[0]);var res_array=[];elf.ask_munch(0).forEach(function(e){Number.isInteger(e)&&res_array.push(e)}),elf.tell_munch(res_array),elf.moveUp(2)

Stage 6: Two Paths, Your Choice

Stage 6

for (i = 0; i < 4; i++) {
  elf.moveTo(lollipop[i]);
}
elf.moveTo(munchkin[0]);
var hohoho = elf.ask_munch(0);
elf.tell_munch(Object.keys(hohoho).find(key => hohoho[key] === 'lollipop'))
elf.moveUp(10);

Minified:

for(i=0;i<4;i++)elf.moveTo(lollipop[i]);elf.moveTo(munchkin[0]);var hohoho=elf.ask_munch(0);elf.tell_munch(Object.keys(hohoho).find(o=>"lollipop"===hohoho[o])),elf.moveUp(10)

Stage 7: Yeeter Swirl

Stage 7

var state = 0;
for (i = 0; i < 8; i++) 0 == state ? elf.moveDown(i + 1) : 1 == state ? elf.moveLeft(i + 1) : 2 == state ? elf.moveUp(i + 1) : 3 == state && elf.moveRight(i + 1), ++state >= 4 && (state = 0), elf.pull_lever(i);
elf.moveUp(2), elf.moveLeft(4);

function recurseSum(e, r = 0) {
  if ("object" == typeof e) e.forEach(e => r = recurseSum(e, r));
  else if (Number.isInteger(e)) return e + r;
  return r
}
elf.tell_munch(recurseSum);
elf.moveUp(2);

Minified:

var state=0;for(i=0;i<8;i++)0==state?elf.moveDown(i+1):1==state?elf.moveLeft(i+1):2==state?elf.moveUp(i+1):3==state&&elf.moveRight(i+1),++state>=4&&(state=0),elf.pull_lever(i);elf.moveUp(2),elf.moveLeft(4);function recurseSum(e,r=0){if("object"==typeof e)e.forEach(e=>r=recurseSum(e,r));else if(Number.isInteger(e))return e+r;return r}
elf.tell_munch(recurseSum);elf.moveUp(2)

Stage 8: For Loop Finale

Stage 8

function findKeyFromValue(arr) {
    for (var i=0; i<arr.length; i++) {
        var retval = Object.keys(arr[i]).find(key => arr[i][key] === 'lollipop');
        if (retval) return retval;
    }
};
var num = 0;
var goRight = true;
var leverCount = 0;
for (var i = 1; i <= 11; i = i + 2) {
  goRight ? elf.moveRight(i) : elf.moveLeft(i);
  goRight = !goRight;
  num += elf.get_lever(leverCount);
  elf.pull_lever(num);
  elf.moveUp(2);
  leverCount++;
}
elf.tell_munch(findKeyFromValue);
elf.moveRight(12);

Minified:

function findKeyFromValue(e){for(var l=0;l<e.length;l++){var o=Object.keys(e[l]).find(o=>"lollipop"===e[l][o]);if(o)return o}}for(var num=0,goRight=!0,leverCount=0,i=1;i<=11;i+=2)goRight?elf.moveRight(i):elf.moveLeft(i),goRight=!goRight,num+=elf.get_lever(leverCount),elf.pull_lever(num),elf.moveUp(2),leverCount++;elf.tell_munch(findKeyFromValue),elf.moveRight(12);