Advent of Code - Day 1

by Uzair Mehmood

HTML

<label for="instructions">
  Paste your input instruction here
</label>
<hr />
<textarea id="instructions" rows="10" cols="50"></textarea>
<p></p>

JavaScript

$('#instructions').change(function () {
  var instructions = $('#instructions').val();
  var floor = 0;
  var basementChar = 0;
  var found = false;

  $.each(instructions.split(''), function (i, text) {

    if (text == '(') {
      floor++;
    } else if (text == ')') {
      floor--;
    }

    if (!found) {
      if (floor == -1) {
        basementChar = i + 1;
        found = true;
      }
    }

  });

  $('p').html('Instructions take santa to floor ' + floor + '<br />Santa first goes to basement at char no. ' + basementChar);
});