day 14
This commit is contained in:
82
2021/14-1.py
Normal file
82
2021/14-1.py
Normal file
@@ -0,0 +1,82 @@
|
||||
# with open("./2021/14.test") as input_file:
|
||||
|
||||
with open("./2021/14.input") as input_file:
|
||||
input_data = input_file.read()
|
||||
|
||||
start_polymer = input_data.split("\n\n")[0]
|
||||
|
||||
polymer_rules = input_data.split("\n\n")[1].split("\n")
|
||||
|
||||
rules_dict = {}
|
||||
for one_rule in polymer_rules:
|
||||
rules_dict[one_rule.split(" -> ")[0]] = one_rule.split(" -> ")[1]
|
||||
|
||||
print(rules_dict)
|
||||
|
||||
counter = 1
|
||||
output_dict = {}
|
||||
|
||||
for i in range(1, len(start_polymer)):
|
||||
substring = start_polymer[i-1:i+1]
|
||||
|
||||
if substring not in output_dict.keys():
|
||||
output_dict[substring] = 0
|
||||
output_dict[substring] += 1
|
||||
|
||||
print(output_dict)
|
||||
|
||||
while counter <= 10:
|
||||
temp_dict = {}
|
||||
for one_key, one_value in output_dict.items():
|
||||
new_start = one_key[0] + rules_dict[one_key]
|
||||
new_end = rules_dict[one_key] + one_key[1]
|
||||
|
||||
if new_start not in temp_dict.keys():
|
||||
temp_dict[new_start] = 0
|
||||
temp_dict[new_start] += one_value
|
||||
|
||||
if new_end not in temp_dict.keys():
|
||||
temp_dict[new_end] = 0
|
||||
temp_dict[new_end] += one_value
|
||||
output_dict = {}
|
||||
output_dict = temp_dict
|
||||
counter += 1
|
||||
|
||||
final_dict = {}
|
||||
for one_key, one_value in output_dict.items():
|
||||
for i in range(len(one_key) -1):
|
||||
if one_key[i] not in final_dict.keys():
|
||||
final_dict[one_key[i]] = 0
|
||||
final_dict[one_key[i]] += one_value
|
||||
final_dict[input_data.split("\n\n")[0][-1]] += 1
|
||||
print(output_dict)
|
||||
print(final_dict)
|
||||
print(max(final_dict.values()) - min(final_dict.values()))
|
||||
|
||||
while counter <= 40:
|
||||
temp_dict = {}
|
||||
for one_key, one_value in output_dict.items():
|
||||
new_start = one_key[0] + rules_dict[one_key]
|
||||
new_end = rules_dict[one_key] + one_key[1]
|
||||
|
||||
if new_start not in temp_dict.keys():
|
||||
temp_dict[new_start] = 0
|
||||
temp_dict[new_start] += one_value
|
||||
|
||||
if new_end not in temp_dict.keys():
|
||||
temp_dict[new_end] = 0
|
||||
temp_dict[new_end] += one_value
|
||||
output_dict = {}
|
||||
output_dict = temp_dict
|
||||
counter += 1
|
||||
|
||||
final_dict = {}
|
||||
for one_key, one_value in output_dict.items():
|
||||
for i in range(len(one_key) -1):
|
||||
if one_key[i] not in final_dict.keys():
|
||||
final_dict[one_key[i]] = 0
|
||||
final_dict[one_key[i]] += one_value
|
||||
final_dict[input_data.split("\n\n")[0][-1]] += 1
|
||||
print(output_dict)
|
||||
print(final_dict)
|
||||
print(max(final_dict.values()) - min(final_dict.values()))
|
||||
Reference in New Issue
Block a user