Solution to problem 7 part 1 in Python
This commit is contained in:
parent
96d2454faa
commit
7c688d4162
80
src/Year_2018/P7.py
Normal file
80
src/Year_2018/P7.py
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
# --- Day 7: The Sum of Its Parts ---
|
||||||
|
|
||||||
|
# You find yourself standing on a snow-covered coastline; apparently, you
|
||||||
|
# landed a little off course. The region is too hilly to see the North Pole
|
||||||
|
# from here, but you do spot some Elves that seem to be trying to unpack
|
||||||
|
# something that washed ashore. It's quite cold out, so you decide to risk
|
||||||
|
# creating a paradox by asking them for directions.
|
||||||
|
|
||||||
|
# "Oh, are you the search party?" Somehow, you can understand whatever Elves
|
||||||
|
# from the year 1018 speak; you assume it's Ancient Nordic Elvish. Could the
|
||||||
|
# device on your wrist also be a translator? "Those clothes don't look very
|
||||||
|
# warm; take this." They hand you a heavy coat.
|
||||||
|
|
||||||
|
# "We do need to find our way back to the North Pole, but we have higher
|
||||||
|
# priorities at the moment. You see, believe it or not, this box contains
|
||||||
|
# something that will solve all of Santa's transportation problems - at least,
|
||||||
|
# that's what it looks like from the pictures in the instructions." It doesn't
|
||||||
|
# seem like they can read whatever language it's in, but you can: "Sleigh kit.
|
||||||
|
# Some assembly required."
|
||||||
|
|
||||||
|
# "'Sleigh'? What a wonderful name! You must help us assemble this 'sleigh' at
|
||||||
|
# once!" They start excitedly pulling more parts out of the box.
|
||||||
|
|
||||||
|
# The instructions specify a series of steps and requirements about which steps
|
||||||
|
# must be finished before others can begin (your puzzle input). Each step is
|
||||||
|
# designated by a single letter. For example, suppose you have the following
|
||||||
|
# instructions:
|
||||||
|
|
||||||
|
# Step C must be finished before step A can begin.
|
||||||
|
# Step C must be finished before step F can begin.
|
||||||
|
# Step A must be finished before step B can begin.
|
||||||
|
# Step A must be finished before step D can begin.
|
||||||
|
# Step B must be finished before step E can begin.
|
||||||
|
# Step D must be finished before step E can begin.
|
||||||
|
# Step F must be finished before step E can begin.
|
||||||
|
|
||||||
|
# Visually, these requirements look like this:
|
||||||
|
|
||||||
|
# -->A--->B--
|
||||||
|
# / \ \
|
||||||
|
# C -->D----->E
|
||||||
|
# \ /
|
||||||
|
# ---->F-----
|
||||||
|
|
||||||
|
# Your first goal is to determine the order in which the steps should be
|
||||||
|
# completed. If more than one step is ready, choose the step which is first
|
||||||
|
# alphabetically. In this example, the steps would be completed as follows:
|
||||||
|
|
||||||
|
# Only C is available, and so it is done first.
|
||||||
|
# Next, both A and F are available. A is first alphabetically, so it is
|
||||||
|
# done next.
|
||||||
|
# Then, even though F was available earlier, steps B and D are now also
|
||||||
|
# available, and B is the first alphabetically of the three.
|
||||||
|
# After that, only D and F are available. E is not available because only
|
||||||
|
# some of its prerequisites are complete. Therefore, D is completed next.
|
||||||
|
# F is the only choice, so it is done next.
|
||||||
|
# Finally, E is completed.
|
||||||
|
|
||||||
|
# So, in this example, the correct order is CABDFE.
|
||||||
|
|
||||||
|
# In what order should the steps in your instructions be completed?
|
||||||
|
|
||||||
|
import networkx as nx
|
||||||
|
|
||||||
|
with open("files/P7.txt") as f:
|
||||||
|
steps = [line for line in f.read().strip().split("\n")]
|
||||||
|
|
||||||
|
|
||||||
|
def part_1() -> None:
|
||||||
|
G = nx.DiGraph()
|
||||||
|
for step in steps:
|
||||||
|
parent, child = step[5:6], step[36:37]
|
||||||
|
G.add_edge(parent, child)
|
||||||
|
order = "".join(nx.lexicographical_topological_sort(G))
|
||||||
|
|
||||||
|
print(f"The correct order will be {order}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
part_1()
|
Loading…
Reference in New Issue
Block a user