Compare commits
No commits in common. "4a0b5f95fafc278d365e90b0bba7e4fafb3db955" and "2ca0106ec02f3329f4e9e7a3b93076885e6fe9a5" have entirely different histories.
4a0b5f95fa
...
2ca0106ec0
|
|
@ -1 +0,0 @@
|
||||||
[ ] - day 19 part 2
|
|
||||||
|
|
@ -27,27 +27,4 @@ Your puzzle input describes all of the possible replacements and, at the bottom,
|
||||||
|
|
||||||
Your puzzle answer was 535.
|
Your puzzle answer was 535.
|
||||||
|
|
||||||
--- Part Two ---
|
The first half of this puzzle is complete! It provides one gold star: *
|
||||||
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,3 +1,4 @@
|
||||||
|
|
||||||
def convert_to_dict(conversion_list: list[str]) -> dict:
|
def convert_to_dict(conversion_list: list[str]) -> dict:
|
||||||
converted_dict: dict[str:set] = {}
|
converted_dict: dict[str:set] = {}
|
||||||
for conversion in conversion_list:
|
for conversion in conversion_list:
|
||||||
|
|
@ -9,62 +10,38 @@ def convert_to_dict(conversion_list: list[str]) -> dict:
|
||||||
return converted_dict
|
return converted_dict
|
||||||
|
|
||||||
|
|
||||||
def convert_to_list(im) -> list[str]:
|
def convert_one_molek_num_pos_versions(conversion_dict: dict[str:set], input_molek: str) -> int:
|
||||||
result = ''
|
def convert_to_list() -> list[str]:
|
||||||
for index, letter in enumerate(im):
|
result = ''
|
||||||
if letter.isupper() and index != 0:
|
for index, letter in enumerate(input_molek):
|
||||||
result += ' '
|
if letter.isupper() and index != 0:
|
||||||
result += letter
|
result += ' '
|
||||||
return result.split()
|
result += letter
|
||||||
|
return result.split()
|
||||||
|
|
||||||
|
|
||||||
def run_all_possible_conversions(conversion_table: dict, input_molek: list) -> set[str]:
|
|
||||||
combinations: set = set()
|
combinations: set = set()
|
||||||
for index, molek in enumerate(input_molek):
|
converted_list = convert_to_list()
|
||||||
if molek not in conversion_table:
|
for index, molek in enumerate(converted_list):
|
||||||
|
if molek not in conversion_dict:
|
||||||
continue
|
continue
|
||||||
for product in conversion_table[molek]:
|
for product in conversion_dict[molek]:
|
||||||
inserted_molek = input_molek.copy()
|
inserted_molek = converted_list.copy()
|
||||||
inserted_molek[index] = product
|
inserted_molek[index] = product
|
||||||
combinations.add("".join(inserted_molek))
|
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__':
|
if __name__ == '__main__':
|
||||||
test_input, test_molek = ["H => HO", "H => OH", "O => HH", "e => O", "e => H"], "HOH"
|
test_input, test_molek = ["H => HO", "H => OH", "O => HH"], "HOH"
|
||||||
assert convert_one_molek_num_pos_versions(convert_to_dict(test_input), test_molek) == 4
|
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")
|
print("All test passed")
|
||||||
|
|
||||||
puzzle_input = open("input.txt", "r").read().splitlines()
|
puzzle_input = open("input.txt", "r").read().splitlines()
|
||||||
output_file = open("output.txt", "w")
|
|
||||||
starting_molek = puzzle_input.pop()
|
starting_molek = puzzle_input.pop()
|
||||||
puzzle_input.pop()
|
puzzle_input.pop()
|
||||||
converted_dict = convert_to_dict(puzzle_input)
|
converted_dict = convert_to_dict(puzzle_input)
|
||||||
print("solution: ", convert_one_molek_num_pos_versions(converted_dict, starting_molek))
|
print("solution: ", convert_one_molek_num_pos_versions(converted_dict, starting_molek))
|
||||||
|
|
||||||
print("Part2: ")
|
print("Part2: ")
|
||||||
print("solution: ", steps_to_medicine(converted_dict, starting_molek, debug_print=True))
|
print("solution: ", )
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue