AoC 2015 Puzzle 1

2015 Puzzle 1: Not Quite Lisp

The very first puzzle in the Advent of Code. The start of this year long journey.

Puzzle description, part 1

Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th.

Collect stars by helping Santa solve puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

Here's an easy puzzle to warm you up.

Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.

An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.

The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.

Solution

The input to this puzzle is a single line of characters. We can take this String and create an array of characters using toCharArray(). This returns an array with each character in its own index.

While looping over the array we check to see if the input is a ( or a ). Each time we increment or decrement the floor counter.

This check can also be done using a switch statement if you would want to do so.

At the end the number of floors is returned to provide the answer to the puzzle.

private int countFloors(String input) {
    int floor = 0;
    char[] tokens = input.toCharArray();
    for (Character next : tokens) {

        if (next.equals('(')) {
            ++floor;
        } else if (next.equals(')')) {
            --floor;
        } else {
            System.out.println("Found an unexpected token: " + next);
        }
    }

    return floor;
}

Puzzle, part 2

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.

Solution

The same logic applies to this puzzle. The String is split up into characters and the result is looped. Now an extra check is made to see if floor -1 is hit. If so, the current position is returned, which has been tracked over every loop.

An alternative implementation would use a traditional for (int i=0...) loop, where i would then be the position of the current character.

private int hitBasement(String input) {
    int floor = 0;
    int pos = 1;

    char[] tokens = input.toCharArray();
    for (Character next : tokens) {
        if (next.equals('(')) {
            ++floor;
        } else if (next.equals(')')) {
            --floor;
        } else {
            System.out.println("Found an unexpected token: " + next);
        }

        if (floor == -1)
            return pos;

        ++pos;
    }

    return -1;
}

Conclusion

The starter puzzle was very straightforward and warms up for the coming days. Lets see what Puzzle 2 brings.