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(")())())")))
In [16]:
# Real puzzle
with open('day1/input.txt', 'rt') as fd:
text = fd.read()
print("First puzzle solution is {}".format(floor_counter(text)))
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("()())")))
In [19]:
# Real puzzle
with open('day1/input.txt', 'rt') as fd:
text = fd.read()
print("Second puzzle solution is {}".format(basement_position(text)))