Day 1
This commit is contained in:
commit
792f135f69
4 changed files with 50 additions and 0 deletions
1
day1/in1
Normal file
1
day1/in1
Normal file
File diff suppressed because one or more lines are too long
1
day1/in2
Normal file
1
day1/in2
Normal file
File diff suppressed because one or more lines are too long
30
day1/so1.py
Normal file
30
day1/so1.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
from functools import reduce
|
||||||
|
from operator import add
|
||||||
|
|
||||||
|
def floor(plan: str):
|
||||||
|
return reduce(add, [1 if e == '(' else -1 for e in plan])
|
||||||
|
|
||||||
|
def test():
|
||||||
|
assert floor("(())") == 0
|
||||||
|
assert floor("()()") == 0
|
||||||
|
|
||||||
|
assert floor("(((") == 3
|
||||||
|
assert floor("(()(()(") == 3
|
||||||
|
|
||||||
|
assert floor("))(((((") == 3
|
||||||
|
|
||||||
|
assert floor("())") == -1
|
||||||
|
assert floor("))(") == -1
|
||||||
|
|
||||||
|
assert floor(")))") == -3
|
||||||
|
assert floor(")())())") == -3
|
||||||
|
|
||||||
|
def solve():
|
||||||
|
with open("in1") as puzzle:
|
||||||
|
r = puzzle.read()
|
||||||
|
|
||||||
|
return floor(r)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(f"Floor: {solve()}")
|
18
day1/so2.py
Normal file
18
day1/so2.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
def basement_pos(plan: str):
|
||||||
|
f = 0
|
||||||
|
for i,e in enumerate(plan):
|
||||||
|
f = f+1 if e == '(' else f-1
|
||||||
|
if f == -1:
|
||||||
|
return i+1
|
||||||
|
|
||||||
|
def test():
|
||||||
|
assert basement_pos(")") == 1
|
||||||
|
assert basement_pos("()())") == 5
|
||||||
|
|
||||||
|
def solve():
|
||||||
|
with open("in2") as puzzle:
|
||||||
|
r = puzzle.read()
|
||||||
|
return basement_pos(r)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(f"Basement position: {solve()}")
|
Loading…
Reference in a new issue