Puzzles from adventofcode.com 2015

Day 1: Not Quite Lisp


In [5]:
DIRECTIONS = {'(': 1, ')': -1}
def floor_counter(text):
    floor = 0
    for c in text:
        floor += DIRECTIONS.get(c, 0)
    return floor

In [9]:
# Test some examples
print(")())()) = {}".format(floor_counter(")())())")))


)())()) = -3

In [16]:
# Real puzzle
with open('day1/input.txt', 'rt') as fd:
    text = fd.read()
    print("First puzzle solution is {}".format(floor_counter(text)))


First puzzle solution is 74

In [17]:
def basement_position(text):
    floor = 0
    position = 0
    for c in text:
        position += 1
        floor += DIRECTIONS.get(c, 0)
        if floor == -1:
            return position
    return None

In [18]:
# Test some examples
print("()()) = {}".format(basement_position("()())")))


()()) = 5

In [19]:
# Real puzzle
with open('day1/input.txt', 'rt') as fd:
    text = fd.read()
    print("Second puzzle solution is {}".format(basement_position(text)))


Second puzzle solution is 1795