-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
46 lines (32 loc) · 1.63 KB
/
day1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Advent of code
# Day 1: Not Quite Lisp
'''
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.
For example:
(()) and ()() both result in floor 0.
((( and (()(()( both result in floor 3.
))((((( also results in floor 3.
()) and ))( both result in floor -1 (the first basement level).
))) and )())()) both result in floor -3.
To what floor do the instructions take Santa?
--- Part Two ---
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.
For example:
) causes him to enter the basement at character position 1.
()()) causes him to enter the basement at character position 5.
What is the position of the character that causes Santa to first enter the basement?
'''
with open('input/day1.txt') as f:
instructions = f.read()
floor = 0
basement_pos = None
for pos, val in enumerate(instructions):
if val == '(':
floor += 1
elif val == ')':
floor -= 1
if floor == -1 and basement_pos is None:
basement_pos = pos + 1
print('Floor: {}, Position when first reaches basement: {}'.format(floor, basement_pos))