Wie löse ich das Rätsel in Python? We will find one Cipher plate in the workshop of the…
- User
Wie löse ich das Rätsel in Python? We will find one Cipher plate in the workshop of the key maker. It is that building over there." Cosmo says.
As you enter the workshop, you find a room with a collection of special hammers. Cosmo explains:
"These are the hammers used for forging keys. The keys here inside the temple have unique shapes, unlike the ones we found outside - but there are again six different shapes, so let's assign them capital letters A to F.
When crafting a new key, the key maker always starts with a base segment A. They utilize different hammers to transform segments into other segments. Each hammer is designed to work on a specific segment and convert it into two new segments. Here is the complete collection of the hammers available:
- A -> BC
- A -> CB
- B -> DD
- B -> BD
- C -> CD
- C -> FE
- D -> AF
- D -> FA
Let's consider an example of how the key maker could forge a key: Starting with A, they would use the first hammer to transform the A into BC. Then, employing the third hammer, they would convert the B into DD, resulting in the key DDC. Continuing this process, applying the seventh hammer to the second D would yield the key DAFC."
The list of hammers is given in hammer_collection.txt.
As you further explore the building, you find another chest, and Cosmo confirms that the cipher plate is inside. Next to the chest, there is a notebook full with hand-written numbers and symbols.
- Assistant
To solve this riddle in Python, we need to use the information provided and analyze the hammer collection to determine the sequence of transformations required to convert the initial segment "A" into the final cipher plate.
First, we read the hammer_collection.txt file to obtain the list of hammers:
pythonwith open("hammer_collection.txt", "r") as file: hammers = file.read().split("\n")Next, we can create a dictionary to map each segment to its possible transformations:
pythontransformations = {} for hammer in hammers: segment, transformation = hammer.split(" -> ") if segment not in transformations: transformations[segment] = [] transformations[segment].append(transformation) - User
how to optimse it. when too long?
- Assistant
If the initial segment is too long and there are many possible transformations, the recursive approach described above may take a long time to compute. In such cases, we can use memoization to optimize the solution and avoid redundant calculations.
We can modify the
find_cipher_platefunction to store the results of previously computed segments in a memoization dictionary. This way, if we encounter the same segment again, we can directly retrieve its previously computed result instead of recalculating it.Here's an optimized version of the code:
pythonmemo = {} def find_cipher_plate(segment): if segment == "A": return segment elif segment in memo: return memo[segment] elif segment in transformations: for transformation in transformations[segment]: result = find_cipher_plate(transformation) if result: memo[segment] = segment + result return memo[segment] return "" cipher_plate = find_cipher_plate("A") print("The cipher plate is:", cipher_plate)