#Skip to menu

Day 1, Year 2015: Not Quite Lisp

First read the problem description.
def count_parenthesis(s):
    c = 0
    for e in s:
        if e == '(':
            c += 1
        if e == ')':
            c -= 1
    return c

def floor_position(s, f):
    c = 0
    i = 0
    for e in s:
        if e == '(':
            c += 1
            i += 1
        if e == ')':
            c -= 1
            i += 1
        if c == f:
            break
    if c != f:
        raise Exception('Floor not reached')
    return i
assert count_parenthesis('(())') == 0
assert count_parenthesis('()()') == 0
assert count_parenthesis('(((') == 3
assert count_parenthesis('))(((((') == 3
assert count_parenthesis('())') == -1
assert count_parenthesis(')())())') == -3
import import_ipynb
import helper
s = helper.read_file("2015_1.txt")
count_parenthesis(s)
280
assert floor_position(')', -1) == 1
assert floor_position('()())', -1) == 5
floor_position(s, -1)
1797

Source code of the solution(s):