[py] Simplify 2015_01

This commit is contained in:
Joscha 2022-12-06 20:21:28 +01:00
parent d028ee1bbf
commit aaa074d66d

View file

@ -1,29 +1,11 @@
# 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 solve(inputstr):
steps = inputstr.strip()
floor = count_floors(steps)
print(f"Part 1: {floor}")
step_nr = find_basement_char(steps)
print(f"Part 2: {step_nr}")
steps = [1 if c == "(" else -1 for c in inputstr.strip()]
print(f"Part 1: {sum(steps)}")
at = 0
for i, step in enumerate(steps):
at += step
if at < 0:
break
print(f"Part 2: {i + 1}")