Advent of Code 2015 day 19 part 1: solved

This commit is contained in:
Fabian Tessmer 2025-04-04 21:33:06 +02:00
parent 5f5189af22
commit 2ca0106ec0
3 changed files with 122 additions and 0 deletions

45
2015/day19/input.txt Normal file
View File

@ -0,0 +1,45 @@
Al => ThF
Al => ThRnFAr
B => BCa
B => TiB
B => TiRnFAr
Ca => CaCa
Ca => PB
Ca => PRnFAr
Ca => SiRnFYFAr
Ca => SiRnMgAr
Ca => SiTh
F => CaF
F => PMg
F => SiAl
H => CRnAlAr
H => CRnFYFYFAr
H => CRnFYMgAr
H => CRnMgYFAr
H => HCa
H => NRnFYFAr
H => NRnMgAr
H => NTh
H => OB
H => ORnFAr
Mg => BF
Mg => TiMg
N => CRnFAr
N => HSi
O => CRnFYFAr
O => CRnMgAr
O => HP
O => NRnFAr
O => OTi
P => CaP
P => PTi
P => SiRnFAr
Si => CaSi
Th => ThCa
Ti => BP
Ti => TiTi
e => HF
e => NAl
e => OMg
CRnCaCaCaSiRnBPTiMgArSiRnSiRnMgArSiRnCaFArTiTiBSiThFYCaFArCaCaSiThCaPBSiThSiThCaCaPTiRnPBSiThRnFArArCaCaSiThCaSiThSiRnMgArCaPTiBPRnFArSiThCaSiRnFArBCaSiRnCaPRnFArPMgYCaFArCaPTiTiTiBPBSiThCaPTiBPBSiRnFArBPBSiRnCaFArBPRnSiRnFArRnSiRnBFArCaFArCaCaCaSiThSiThCaCaPBPTiTiRnFArCaPTiBSiAlArPBCaCaCaCaCaSiRnMgArCaSiThFArThCaSiThCaSiRnCaFYCaSiRnFYFArFArCaSiRnFYFArCaSiRnBPMgArSiThPRnFArCaSiRnFArTiRnSiRnFYFArCaSiRnBFArCaSiRnTiMgArSiThCaSiThCaFArPRnFArSiRnFArTiTiTiTiBCaCaSiRnCaCaFYFArSiThCaPTiBPTiBCaSiThSiRnMgArCaF

30
2015/day19/problem.txt Normal file
View File

@ -0,0 +1,30 @@
--- Day 19: Medicine for Rudolph ---
Rudolph the Red-Nosed Reindeer is sick! His nose isn't shining very brightly, and he needs medicine.
Red-Nosed Reindeer biology isn't similar to regular reindeer biology; Rudolph is going to need custom-made medicine. Unfortunately, Red-Nosed Reindeer chemistry isn't similar to regular reindeer chemistry, either.
The North Pole is equipped with a Red-Nosed Reindeer nuclear fusion/fission plant, capable of constructing any Red-Nosed Reindeer molecule you need. It works by starting with some input molecule and then doing a series of replacements, one per step, until it has the right molecule.
However, the machine has to be calibrated before it can be used. Calibration involves determining the number of molecules that can be generated in one step from a given starting point.
For example, imagine a simpler machine that supports only the following replacements:
H => HO
H => OH
O => HH
Given the replacements above and starting with HOH, the following molecules could be generated:
HOOH (via H => HO on the first H).
HOHO (via H => HO on the second H).
OHOH (via H => OH on the first H).
HOOH (via H => OH on the second H).
HHHH (via O => HH).
So, in the example above, there are 4 distinct molecules (not five, because HOOH appears twice) after one replacement from HOH. Santa's favorite molecule, HOHOHO, can become 7 distinct molecules (over nine replacements: six from H, and three from O).
The machine replaces without regard for the surrounding characters. For example, given the string H2O, the transition H => OO would result in OO2O.
Your puzzle input describes all of the possible replacements and, at the bottom, the medicine molecule for which you need to calibrate the machine. How many distinct molecules can be created after all the different ways you can do one replacement on the medicine molecule?
Your puzzle answer was 535.
The first half of this puzzle is complete! It provides one gold star: *

47
2015/day19/solution.py Normal file
View File

@ -0,0 +1,47 @@
def convert_to_dict(conversion_list: list[str]) -> dict:
converted_dict: dict[str:set] = {}
for conversion in conversion_list:
starting_material, product = conversion.split(' => ')
if starting_material not in converted_dict:
converted_dict[starting_material] = {product}
else:
converted_dict[starting_material].add(product)
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()
combinations: set = set()
converted_list = convert_to_list()
for index, molek in enumerate(converted_list):
if molek not in conversion_dict:
continue
for product in conversion_dict[molek]:
inserted_molek = converted_list.copy()
inserted_molek[index] = product
combinations.add("".join(inserted_molek))
return len(combinations)
if __name__ == '__main__':
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
print("All test passed")
puzzle_input = open("input.txt", "r").read().splitlines()
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: ", )