Damien Sedgwick

Posted on

# Day 1: Not Quite Lisp (Part 2)

We are back and today we are going to be solving part 2 of day 1: "Not Quite Lisp" from Advent of Code 2015.

Lets take a look at the problem.

``````--- Part Two ---

Now, given the same instructions, find the position of the
first character that causes him to enter the basement
(floor -1).

The first character in the instructions has position 1,
the second character has position 2, and so on.

For example:

) causes him to enter the basement at character position 1.
()()) causes him to enter the basement at character position 5.

What is the position of the character that causes Santa to
first enter the basement?
``````

So this is going to be quite straight forward. We already have all of the pieces we need to be able to complete this challenge.

All we need to do whilst iterating over our string slice of characters, is check whether or not the floor value is `-1` and if so, return the index for the character.

Now we only want to do this in the first instance so we probably want to check whether or not the basement floor has already been visited.

Lets modify our existing code so that our main function now looks like this.

``````func main() {
var result = 0
var basementHit = false

slice := strings.Split(input, "")

for i, v := range slice {
switch v {
case "(":
result = IncrementByOne(result)
case ")":
result = DecrementByOne(result)
}

if result == -1 && basementHit == false {
basementHit = true
// we `+1` because the problem states that
// the first char is at index 1
fmt.Println(i + 1)
}
}

fmt.Println(result)
}
``````

So for this particular problem and input data, my console tells me that the first time Santa enters the basement is on character 1797.

If we punch that in to the answer box we get a success message!

``````That's the right answer! You are one gold star
closer to powering the weather machine.
``````

So that is the first day for Advent of Code 2015 completed. Usually as the days go on, the problems get more difficult but we will do our best to continue through them and answer as many of them as possible.