Update lock file

This commit is contained in:
2022-07-04 20:05:15 +02:00
parent 1d86a3af2b
commit 27a15005ee
2 changed files with 140 additions and 40 deletions

100
src/Year_2017/P11.py Normal file
View File

@@ -0,0 +1,100 @@
# --- Day 11: Hex Ed ---
# Crossing the bridge, you've barely reached the other side of the stream when
# a program comes up to you, clearly in distress. "It's my child process," she
# says, "he's gotten lost in an infinite grid!"
# Fortunately for her, you have plenty of experience with infinite grids.
# Unfortunately for you, it's a hex grid.
# The hexagons ("hexes") in this grid are aligned such that adjacent hexes can
# be found to the north, northeast, southeast, south, southwest, and northwest:
# \ n /
# nw +--+ ne
# / \
# -+ +-
# \ /
# sw +--+ se
# / s \
# You have the path the child process took. Starting where he started, you need
# to determine the fewest number of steps required to reach him. (A "step"
# means to move from the hex you are in to any adjacent hex.)
# For example:
# ne,ne,ne is 3 steps away.
# ne,ne,sw,sw is 0 steps away (back where you started).
# ne,ne,s,s is 2 steps away (se,se).
# se,sw,se,sw,sw is 3 steps away (s,s,sw).
with open("files/P11.txt") as f:
steps = [step for step in f.read().strip().split(",")]
def part_1() -> None:
x, y, z = 0, 0, 0
for step in steps:
if step == "n":
y += 1
z -= 1
elif step == "ne":
x += 1
z -= 1
elif step == "se":
y -= 1
x += 1
elif step == "s":
y -= 1
z += 1
elif step == "sw":
x -= 1
z += 1
elif step == "nw":
y += 1
x -= 1
res = max(map(abs, [x, y, z]))
print(f"There are {res} steps required to reach the child")
# --- Part Two ---
# How many steps away is the furthest he ever got from his starting position?
def part_2() -> None:
x, y, z = 0, 0, 0
furthest = 0
for step in steps:
if step == "n":
y += 1
z -= 1
elif step == "ne":
x += 1
z -= 1
elif step == "se":
y -= 1
x += 1
elif step == "s":
y -= 1
z += 1
elif step == "sw":
x -= 1
z += 1
elif step == "nw":
y += 1
x -= 1
res = max(map(abs, [x, y, z]))
furthest = max(furthest, res)
print(f"The furthest distance is {furthest} steps from origin")
if __name__ == "__main__":
part_1()
part_2()