As I mentioned elsewhere, the learning curve for Hacked is probably a bit steep for new coders. I didn’t just want to give you the answers outright. I’ve included hints to help where you might get stuck, all wrapped in spoiler tags. Teach a man (or woman) to fish! I’ve also included comments in the code blocks, which you can’t do in the app, but may help those who are just starting out with coding. Ignore them when typing them into the hackpad.

Note that while I tend to use the keyword “return” for better readability/understanding, you don’t always need it when the final action is to output your answer, which will save you some keystrokes and score you more points.

[toc heading_levels=”2″]

Add one

[spoiler title=”Hint 1″]The pattern is simple, add one to each item in the list[/spoiler]
[spoiler title=”Hint 2″]You could manipulate the input by adding/removing things[/spoiler]
[spoiler title=”Hint 3″]Or, you could create a new list altogether[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]foreach var_a in input {
input.push(var_a + 1).remove(0);
}
return input;
[/code]

[/spoiler]
[spoiler title=”Alternative Code”]

[code lang=”hack”]var_b = [];
foreach var_a in input {
var_b.push(var_a + 1);
}
return var_b;
[/code]

[/spoiler]

Positivity

[spoiler title=”Hint 1″]For each item, true if a positive number, or false if not.[/spoiler]
[spoiler title=”Hint 2″]You’ve been given a map method, which is what they expect you to use.[/spoiler]
[spoiler title=”Hint 3″]As is often the case, there’s an alternative solution: push true/false into a list depending on whether each input item is more or less than x.[/spoiler]
[spoiler title=”Hint 4″]While 0 is usually considered neutral (or at best, non-negative), in this challenge 0 is positive.[/spoiler]
[spoiler title=”Expected Code”]

[code lang=”hack”]function f1: var_a {
return var_a > -1;
}
return input.map(f1);

or just:

input.map(function var_a -> (var_a > -1));
[/code]

[/spoiler]
[spoiler title=”Alternative Code”]

[code lang=”hack”]var_b = [];
foreach var_a in input {
var_b.push(var_a > -1);
}[/code]

[/spoiler]

Nearest to [0,0]

[spoiler title=”Sorry…”]No hint yet, and I think this could be simpler, might come back to this one.[/spoiler]
[spoiler title=”Code”]

[code lang=”hack”]function f1: var_a {
return var_a[0] + 1 * var_a[1] + 1;
}
foreach var_a in input {
if var_b == 0 || var_b > f1(var_a) {
var_c = var_a;
var_b = f1(var_a);
}
}
return var_c;
[/code]

[/spoiler]

12 thoughts to “Hacked app code solutions – Chapter 7 – Nuclear Plant

  • Dave Appleton

    Nearest to 0,0
    Hint: Nearest to 0,0 should be Cartesian distance I.e. Sqrt( x^2 + y^2)

    My f1 takes two pairs var_a and var_b

    Distance to 0,0 is the sqrt of pow(var_a[0],2) + pow(var_a[1],2) but since you are comparing you can ignore the sqrt
    My function returns the compare of that for var_a and var_b

    Then
    [hack]
    var_m = input[0]
    Foreach var_d in input {
    If f1(var_m,var_d) {
    Var_m = var_d
    }
    Return var_m
    [/hack]

    Reply
  • Kahn

    I add each number in set and revert sort em to get the last element

    [hack]
    function f1: input {
    foreach var_a in input {
    var_b = var_b + var_a;
    }
    }
    input.sort_with(function var_a, var_b -> f1(var_a) > f1(var_b)).pop;
    [/hack]

    Reply
  • stepph

    As Dave said, “nearest to (0,0)”
    is cartesian distance, with square root ignored.

    Define:

    function f1 : var_a (
    return var_a[0]*var_a[0]+var_a[1]* var_a[1]
    )

    that calculate distance on var_a, that is a list of two elements (cartesian coordinates of a point)

    Then sort input with this distance:

    input.sort_with(function var_a, var_b -> f1(var_a) < f1(var_b) )

    and return the first ( the nearest):

    return input[0]

    Reply
  • Sochima Biereagu

    Add One (best solution):

    [code]
    //inline functions

    return input.map(function var_a -> var_a + 1);
    [/code]

    Reply
  • Sochima Biereagu

    The .map() isnt there so you go to your hack pad via the ‘create game menu’, write the code, hold to copy it, paste it in the solution page by holding the hackpad, and voila!

    Reply
    • Andrew

      Hah, well that’s clever. That might also explain how people got higher scores on earlier levels!

      Reply
  • Sochima Biereagu

    Nearest to [0, 0]: Good solution

    hint: Check and return the array with the minimum sum of items.

    [hack]
    var_a = input[0];
    foreach var_b in input {
    if (var_b[0] + var_b[1]) < (var_a[0] + var_a[1]) {
    var_a = var_b
    }
    }

    return var_a;
    [/hack]

    Reply
    • Slartibartfast

      I see this is basically the same code as in chapter 3-1 (jailbreak-max). But how can this work with 3 items in the list?

      Reply
  • Filipemtx

    I don’t have map even beating the Power Plant chapter, why is that?

    Reply
    • Filipemtx

      I found map on the “.” tab 😀
      Forgot to read the tutorial

      Reply
  • Nerey

    Code to Nearest to [0,0] will give wrong answer if there is [0,0] in input

    Reply
  • Slartibartfast

    why is this wrong? It returns 9, instead of [4,7,9]?

    foreach var_a in Input {
    var_a + 1;
    }

    Reply

Leave a comment

Your email address will not be published. Required fields are marked *