Solution to problem 2 in Python
This commit is contained in:
parent
c1e70b921c
commit
fe5ae6a4d0
@ -45,7 +45,7 @@
|
||||
# Your puzzle input is the instructions from the document you found at the
|
||||
# front desk. What is the bathroom code?
|
||||
|
||||
from typing import Optional, Tuple
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
with open("files/P2.txt") as f:
|
||||
instructions = [line for line in f.read().strip().split()]
|
||||
@ -117,5 +117,132 @@ def part_1() -> None:
|
||||
print(f"The secret code is {secret_code}")
|
||||
|
||||
|
||||
# --- Part Two ---
|
||||
|
||||
# You finally arrive at the bathroom (it's a several minute walk from the lobby
|
||||
# so visitors can behold the many fancy conference rooms and water coolers on
|
||||
# this floor) and go to punch in the code. Much to your bladder's dismay, the
|
||||
# keypad is not at all like you imagined it. Instead, you are confronted with
|
||||
# the result of hundreds of man-hours of bathroom-keypad-design meetings:
|
||||
|
||||
# 1
|
||||
# 2 3 4
|
||||
# 5 6 7 8 9
|
||||
# A B C
|
||||
# D
|
||||
|
||||
# You still start at "5" and stop when you're at an edge, but given the same
|
||||
# instructions as above, the outcome is very different:
|
||||
|
||||
# You start at "5" and don't move at all (up and left are both edges),
|
||||
# ending at 5.
|
||||
# Continuing from "5", you move right twice and down three times (through
|
||||
# "6", "7", "B", "D", "D"), ending at D.
|
||||
# Then, from "D", you move five more times (through "D", "B", "C", "C",
|
||||
# "B"), ending at B.
|
||||
# Finally, after five more moves, you end at 3.
|
||||
|
||||
# So, given the actual keypad layout, the code would be 5DB3.
|
||||
|
||||
# Using the same instructions in your puzzle input, what is the correct
|
||||
# bathroom code?
|
||||
|
||||
|
||||
def move_right_(position: Union[int, str]) -> Union[int, str]:
|
||||
num_dict: dict[Union[int, str], Union[int, str]] = {
|
||||
1: 1,
|
||||
2: 3,
|
||||
3: 4,
|
||||
4: 4,
|
||||
5: 6,
|
||||
6: 7,
|
||||
7: 8,
|
||||
8: 9,
|
||||
9: 9,
|
||||
"A": "B",
|
||||
"B": "C",
|
||||
"C": "C",
|
||||
"D": "D",
|
||||
}
|
||||
return num_dict[position]
|
||||
|
||||
|
||||
def move_down_(position: Union[int, str]) -> Union[int, str]:
|
||||
num_dict: dict[Union[int, str], Union[int, str]] = {
|
||||
1: 3,
|
||||
2: 6,
|
||||
3: 7,
|
||||
4: 8,
|
||||
5: 5,
|
||||
6: "A",
|
||||
7: "B",
|
||||
8: "C",
|
||||
9: 9,
|
||||
"A": "A",
|
||||
"B": "D",
|
||||
"C": "C",
|
||||
"D": "D",
|
||||
}
|
||||
return num_dict[position]
|
||||
|
||||
|
||||
def move_left_(position: Union[int, str]) -> Union[int, str]:
|
||||
num_dict: dict[Union[int, str], Union[int, str]] = {
|
||||
1: 1,
|
||||
2: 2,
|
||||
3: 2,
|
||||
4: 3,
|
||||
5: 5,
|
||||
6: 5,
|
||||
7: 6,
|
||||
8: 7,
|
||||
9: 8,
|
||||
"A": "A",
|
||||
"B": "A",
|
||||
"C": "B",
|
||||
"D": "D",
|
||||
}
|
||||
return num_dict[position]
|
||||
|
||||
|
||||
def move_up_(position: Union[int, str]) -> Union[int, str]:
|
||||
num_dict: dict[Union[int, str], Union[int, str]] = {
|
||||
1: 1,
|
||||
2: 2,
|
||||
3: 1,
|
||||
4: 4,
|
||||
5: 5,
|
||||
6: 2,
|
||||
7: 3,
|
||||
8: 4,
|
||||
9: 9,
|
||||
"A": 6,
|
||||
"B": 7,
|
||||
"C": 8,
|
||||
"D": "B",
|
||||
}
|
||||
return num_dict[position]
|
||||
|
||||
|
||||
def part_2() -> None:
|
||||
secret = []
|
||||
for instruction in instructions:
|
||||
pos: Union[int, str] = 5
|
||||
for button in instruction:
|
||||
if button == "D":
|
||||
pos = move_down_(pos)
|
||||
elif button == "U":
|
||||
pos = move_up_(pos)
|
||||
elif button == "R":
|
||||
pos = move_right_(pos)
|
||||
elif button == "L":
|
||||
pos = move_left_(pos)
|
||||
secret.append(pos)
|
||||
|
||||
secret_code = "".join(map(str, (char for char in secret)))
|
||||
print(f"The bathroom code is {secret_code}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
part_2()
|
||||
|
Loading…
Reference in New Issue
Block a user