Compare commits

...

3 Commits

3 changed files with 65 additions and 18 deletions

1
2015/TODO.md Normal file
View File

@ -0,0 +1 @@
[ ] - day 19 part 2

View File

@ -27,4 +27,27 @@ Your puzzle input describes all of the possible replacements and, at the bottom,
Your puzzle answer was 535. 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: **

View File

@ -1,4 +1,3 @@
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:
@ -10,38 +9,62 @@ def convert_to_dict(conversion_list: list[str]) -> dict:
return converted_dict return converted_dict
def convert_one_molek_num_pos_versions(conversion_dict: dict[str:set], input_molek: str) -> int: def convert_to_list(im) -> list[str]:
def convert_to_list() -> list[str]: result = ''
result = '' for index, letter in enumerate(im):
for index, letter in enumerate(input_molek): if letter.isupper() and index != 0:
if letter.isupper() and index != 0: result += ' '
result += ' ' result += letter
result += letter return result.split()
return result.split()
def run_all_possible_conversions(conversion_table: dict, input_molek: list) -> set[str]:
combinations: set = set() combinations: set = set()
converted_list = convert_to_list() for index, molek in enumerate(input_molek):
for index, molek in enumerate(converted_list): if molek not in conversion_table:
if molek not in conversion_dict:
continue continue
for product in conversion_dict[molek]: for product in conversion_table[molek]:
inserted_molek = converted_list.copy() inserted_molek = input_molek.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"], "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 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: ", ) print("solution: ", steps_to_medicine(converted_dict, starting_molek, debug_print=True))