[py] Port 2015_01

This commit is contained in:
Joscha 2022-12-06 20:05:38 +01:00
parent c1fbf1eb5f
commit 01e2920a67
3 changed files with 31 additions and 36 deletions

View file

@ -1,36 +0,0 @@
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)

View file

@ -2,6 +2,7 @@ import sys
import argparse
from pathlib import Path
from .y2015 import d01
from .y2016 import d01, d02, d03, d04, d05
from .y2017 import d01, d02, d03, d04, d05, d06, d07, d08, d09
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
@ -10,6 +11,7 @@ from .y2021 import d14
from .y2022 import d01, d02, d03, d04, d05, d06
DAYS = {
"2015_01": y2015.d01.solve,
"2016_01": y2016.d01.solve,
"2016_02": y2016.d02.solve,
"2016_03": y2016.d03.solve,

29
py/aoc/y2015/d01.py Normal file
View file

@ -0,0 +1,29 @@
# 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}")