blob: cd3ec342769c9397c2338d75d6f767da41977ee5 (
plain)
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
|
#!/usr/bin/python
def solve_partn(partn, data):
floor = 0
for i, d in enumerate(data):
if d == "(":
floor += 1
elif d == ")":
floor -= 1
if partn == 2:
if floor < 0:
return i + 1
if partn == 1:
return floor
else: # partn == 2
return -1 # only reached if he never goes underground
def main():
# Read instructions from input text file
with open("input.txt", "r") as f:
data = f.read()
print("Part 1 solution:", solve_partn(1, data)) # 74 for me
print("Part 2 solution:", solve_partn(2, data)) # 1795 for me
if __name__ == "__main__":
main()
|