Day 1, Year 2015: Not Quite Lisp
First read the problem description.
def count_parenthesis(s):
= 0
c for e in s:
if e == '(':
+= 1
c if e == ')':
-= 1
c return c
def floor_position(s, f):
= 0
c = 0
i for e in s:
if e == '(':
+= 1
c += 1
i if e == ')':
-= 1
c += 1
i 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
= helper.read_file("2015_1.txt") s
count_parenthesis(s)
280
assert floor_position(')', -1) == 1
assert floor_position('()())', -1) == 5
-1) floor_position(s,
1797
Source code of the solution(s):