Solution to problem 1 in Python
This commit is contained in:
parent
e29787b84c
commit
432e3fc963
73
src/Year_2015/P1.py
Normal file
73
src/Year_2015/P1.py
Normal file
@ -0,0 +1,73 @@
|
||||
# --- Day 1: Not Quite Lisp ---
|
||||
|
||||
# Santa was hoping for a white Christmas, but his weather machine's "snow"
|
||||
# function is powered by stars, and he's fresh out! To save Christmas, he needs
|
||||
# you to collect fifty stars by December 25th.
|
||||
|
||||
# Collect stars by helping Santa solve puzzles. Two puzzles will be made
|
||||
# available on each day in the Advent calendar; the second puzzle is unlocked
|
||||
# when you complete the first. Each puzzle grants one star. Good luck!
|
||||
|
||||
# Here's an easy puzzle to warm you up.
|
||||
|
||||
# Santa is trying to deliver presents in a large apartment building, but he
|
||||
# can't find the right floor - the directions he got are a little confusing.
|
||||
# He starts on the ground floor (floor 0) and then follows the instructions
|
||||
# one character at a time.
|
||||
|
||||
# An opening parenthesis, (, means he should go up one floor, and a closing
|
||||
# parenthesis, ), means he should go down one floor.
|
||||
|
||||
# The apartment building is very tall, and the basement is very deep; he will
|
||||
# never find the top or bottom floors.
|
||||
|
||||
# For example:
|
||||
|
||||
# (()) and ()() both result in floor 0.
|
||||
# ((( and (()(()( both result in floor 3.
|
||||
# ))((((( also results in floor 3.
|
||||
# ()) and ))( both result in floor -1 (the first basement level).
|
||||
# ))) and )())()) both result in floor -3.
|
||||
|
||||
# To what floor do the instructions take Santa?
|
||||
|
||||
with open("files/P1.txt") as f:
|
||||
directions = [line for line in f.read().strip().split()]
|
||||
|
||||
|
||||
def part_1() -> None:
|
||||
up = int(directions[0].count("("))
|
||||
down = int(directions[0].count(")"))
|
||||
print(f"The floor is {up - down}")
|
||||
|
||||
|
||||
# --- Part Two ---
|
||||
|
||||
# Now, given the same instructions, find the position of the first character
|
||||
# that causes him to enter the basement (floor -1). The first character in the
|
||||
# instructions has position 1, the second character has position 2, and so on.
|
||||
|
||||
# For example:
|
||||
|
||||
# ) causes him to enter the basement at character position 1.
|
||||
# ()()) causes him to enter the basement at character position 5.
|
||||
|
||||
# What is the position of the character that causes Santa to first enter the
|
||||
# basement?
|
||||
|
||||
|
||||
def part_2() -> None:
|
||||
floor = 0
|
||||
for idx, char in enumerate(directions[0], start=1):
|
||||
if char == "(":
|
||||
floor += 1
|
||||
elif char == ")":
|
||||
floor -= 1
|
||||
if floor == -1:
|
||||
print(f"The position is {idx}")
|
||||
break
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
part_2()
|
Loading…
Reference in New Issue
Block a user