Compare commits
3 Commits
2ca0106ec0
...
4a0b5f95fa
| Author | SHA1 | Date |
|---|---|---|
|
|
4a0b5f95fa | |
|
|
893b20fb6f | |
|
|
7524187fe5 |
|
|
@ -0,0 +1 @@
|
|||
[ ] - day 19 part 2
|
||||
|
|
@ -27,4 +27,27 @@ Your puzzle input describes all of the possible replacements and, at the bottom,
|
|||
|
||||
Your puzzle answer was 535.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
--- Part Two ---
|
||||
Now that the machine is calibrated, you're ready to begin molecule fabrication.
|
||||
|
||||
Molecule fabrication always begins with just a single electron, e, and applying replacements one at a time, just like the ones during calibration.
|
||||
|
||||
For example, suppose you have the following replacements:
|
||||
|
||||
e => H
|
||||
e => O
|
||||
H => HO
|
||||
H => OH
|
||||
O => HH
|
||||
If you'd like to make HOH, you start with e, and then make the following replacements:
|
||||
|
||||
e => O to get O
|
||||
O => HH to get HH
|
||||
H => OH (on the second H) to get HOH
|
||||
So, you could make HOH after 3 steps. Santa's favorite molecule, HOHOHO, can be made in 6 steps.
|
||||
|
||||
How long will it take to make the medicine? Given the available replacements and the medicine molecule in your puzzle input, what is the fewest number of steps to go from e to the medicine molecule?
|
||||
|
||||
Your puzzle answer was 212.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
def convert_to_dict(conversion_list: list[str]) -> dict:
|
||||
converted_dict: dict[str:set] = {}
|
||||
for conversion in conversion_list:
|
||||
|
|
@ -10,38 +9,62 @@ def convert_to_dict(conversion_list: list[str]) -> dict:
|
|||
return converted_dict
|
||||
|
||||
|
||||
def convert_one_molek_num_pos_versions(conversion_dict: dict[str:set], input_molek: str) -> int:
|
||||
def convert_to_list() -> list[str]:
|
||||
result = ''
|
||||
for index, letter in enumerate(input_molek):
|
||||
if letter.isupper() and index != 0:
|
||||
result += ' '
|
||||
result += letter
|
||||
return result.split()
|
||||
def convert_to_list(im) -> list[str]:
|
||||
result = ''
|
||||
for index, letter in enumerate(im):
|
||||
if letter.isupper() and index != 0:
|
||||
result += ' '
|
||||
result += letter
|
||||
return result.split()
|
||||
|
||||
|
||||
def run_all_possible_conversions(conversion_table: dict, input_molek: list) -> set[str]:
|
||||
combinations: set = set()
|
||||
converted_list = convert_to_list()
|
||||
for index, molek in enumerate(converted_list):
|
||||
if molek not in conversion_dict:
|
||||
for index, molek in enumerate(input_molek):
|
||||
if molek not in conversion_table:
|
||||
continue
|
||||
for product in conversion_dict[molek]:
|
||||
inserted_molek = converted_list.copy()
|
||||
for product in conversion_table[molek]:
|
||||
inserted_molek = input_molek.copy()
|
||||
inserted_molek[index] = product
|
||||
combinations.add("".join(inserted_molek))
|
||||
return combinations
|
||||
|
||||
return len(combinations)
|
||||
|
||||
def convert_one_molek_num_pos_versions(conversion_dict: dict[str:set], input_molek: str) -> int:
|
||||
converted_list = convert_to_list(input_molek)
|
||||
return len(run_all_possible_conversions(conversion_dict, converted_list))
|
||||
|
||||
|
||||
def steps_to_medicine(conversion_dict: dict[str:set], product, debug_print=False, s_molek="e") -> int:
|
||||
results, iterations = set(s_molek), 0
|
||||
while product not in results:
|
||||
iterations += 1
|
||||
new_results = set()
|
||||
for result in results:
|
||||
[new_results.add(i) for i in run_all_possible_conversions(conversion_dict, convert_to_list(result))]
|
||||
results = new_results
|
||||
if debug_print:
|
||||
output_file.write(str(iterations) + " : \n")
|
||||
output_file.writelines(str(results))
|
||||
output_file.write("\n")
|
||||
|
||||
return iterations
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_input, test_molek = ["H => HO", "H => OH", "O => HH"], "HOH"
|
||||
test_input, test_molek = ["H => HO", "H => OH", "O => HH", "e => O", "e => H"], "HOH"
|
||||
assert convert_one_molek_num_pos_versions(convert_to_dict(test_input), test_molek) == 4
|
||||
|
||||
assert steps_to_medicine(convert_to_dict(test_input), test_molek) == 3
|
||||
assert steps_to_medicine(convert_to_dict(test_input), "HOHOHO") == 6
|
||||
print("All test passed")
|
||||
|
||||
puzzle_input = open("input.txt", "r").read().splitlines()
|
||||
output_file = open("output.txt", "w")
|
||||
starting_molek = puzzle_input.pop()
|
||||
puzzle_input.pop()
|
||||
converted_dict = convert_to_dict(puzzle_input)
|
||||
print("solution: ", convert_one_molek_num_pos_versions(converted_dict, starting_molek))
|
||||
|
||||
print("Part2: ")
|
||||
print("solution: ", )
|
||||
print("solution: ", steps_to_medicine(converted_dict, starting_molek, debug_print=True))
|
||||
|
|
|
|||
Loading…
Reference in New Issue