Solve 2015/01
This commit is contained in:
parent
f1be074b5f
commit
e212447c2e
2 changed files with 37 additions and 0 deletions
1
2015/01/input.txt
Normal file
1
2015/01/input.txt
Normal file
File diff suppressed because one or more lines are too long
36
2015/01/solve.py
Normal file
36
2015/01/solve.py
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def load_steps(filename):
|
||||||
|
with open(filename, "r") as f:
|
||||||
|
return f.read()[:-1]
|
||||||
|
|
||||||
|
# PART 1
|
||||||
|
|
||||||
|
def count_floors(steps):
|
||||||
|
return steps.count("(") - steps.count(")")
|
||||||
|
|
||||||
|
# PART 2
|
||||||
|
|
||||||
|
def find_basement_char(steps):
|
||||||
|
pos = 0
|
||||||
|
step_nr = 0
|
||||||
|
for step in steps:
|
||||||
|
step_nr += 1
|
||||||
|
if step == "(":
|
||||||
|
pos += 1
|
||||||
|
if step == ")":
|
||||||
|
pos -= 1
|
||||||
|
if pos < 0:
|
||||||
|
return step_nr
|
||||||
|
|
||||||
|
def main(filename):
|
||||||
|
steps = load_steps(filename)
|
||||||
|
print(f"Solutions for {filename}")
|
||||||
|
floor = count_floors(steps)
|
||||||
|
print(f"Part 1: {floor}")
|
||||||
|
step_nr = find_basement_char(steps)
|
||||||
|
print(f"Part 2: {step_nr}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
for filename in sys.argv[1:]:
|
||||||
|
main(filename)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue