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()))
|
||||
102
2021/14.input
Normal file
102
2021/14.input
Normal file
@@ -0,0 +1,102 @@
|
||||
NNSOFOCNHBVVNOBSBHCB
|
||||
|
||||
HN -> S
|
||||
FK -> N
|
||||
CH -> P
|
||||
VP -> P
|
||||
VV -> C
|
||||
PB -> H
|
||||
CP -> F
|
||||
KO -> P
|
||||
KN -> V
|
||||
NO -> K
|
||||
NF -> N
|
||||
CO -> P
|
||||
HO -> H
|
||||
VH -> V
|
||||
OV -> C
|
||||
VS -> F
|
||||
PK -> H
|
||||
OS -> S
|
||||
BF -> S
|
||||
SN -> P
|
||||
NK -> N
|
||||
SV -> O
|
||||
KB -> O
|
||||
ON -> O
|
||||
FN -> H
|
||||
FO -> N
|
||||
KV -> S
|
||||
CS -> C
|
||||
VO -> O
|
||||
SP -> O
|
||||
VK -> H
|
||||
KP -> S
|
||||
SK -> N
|
||||
NC -> B
|
||||
PN -> N
|
||||
HV -> O
|
||||
HS -> C
|
||||
CN -> N
|
||||
OO -> V
|
||||
FF -> B
|
||||
VC -> V
|
||||
HK -> K
|
||||
CC -> H
|
||||
BO -> H
|
||||
SC -> O
|
||||
HH -> C
|
||||
BV -> P
|
||||
OB -> O
|
||||
FC -> H
|
||||
PO -> C
|
||||
FV -> C
|
||||
BK -> F
|
||||
HB -> B
|
||||
NH -> P
|
||||
KF -> N
|
||||
BP -> H
|
||||
KK -> O
|
||||
OH -> K
|
||||
CB -> H
|
||||
CK -> C
|
||||
OK -> H
|
||||
NN -> F
|
||||
VF -> N
|
||||
SO -> K
|
||||
OP -> F
|
||||
NP -> B
|
||||
FS -> S
|
||||
SH -> O
|
||||
FP -> O
|
||||
SF -> V
|
||||
HF -> N
|
||||
KC -> K
|
||||
SB -> V
|
||||
FH -> N
|
||||
SS -> C
|
||||
BB -> C
|
||||
NV -> K
|
||||
OC -> S
|
||||
CV -> N
|
||||
HC -> P
|
||||
BC -> N
|
||||
OF -> K
|
||||
BH -> N
|
||||
NS -> K
|
||||
BN -> F
|
||||
PC -> C
|
||||
CF -> N
|
||||
HP -> F
|
||||
BS -> O
|
||||
PF -> S
|
||||
PV -> B
|
||||
KH -> K
|
||||
VN -> V
|
||||
NB -> N
|
||||
PH -> V
|
||||
KS -> B
|
||||
PP -> V
|
||||
PS -> C
|
||||
VB -> N
|
||||
FB -> N
|
||||
61
2021/14.py
Normal file
61
2021/14.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# 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
|
||||
while counter <= 10:
|
||||
output_data = ""
|
||||
|
||||
for i in range(1, len(start_polymer)):
|
||||
substring = start_polymer[i-1:i+1]
|
||||
|
||||
output_data += substring[0] + rules_dict[substring]
|
||||
|
||||
output_data += start_polymer[-1]
|
||||
|
||||
start_polymer = output_data
|
||||
|
||||
counter += 1
|
||||
|
||||
output_dict = {}
|
||||
for i in range(len(start_polymer)):
|
||||
if start_polymer[i] not in output_dict.keys():
|
||||
output_dict[start_polymer[i]] = 0
|
||||
output_dict[start_polymer[i]] += 1
|
||||
|
||||
print(output_dict)
|
||||
print(max(output_dict.values()) - min(output_dict.values()))
|
||||
|
||||
while counter <= 40:
|
||||
output_data = ""
|
||||
|
||||
for i in range(1, len(start_polymer)):
|
||||
substring = start_polymer[i-1:i+1]
|
||||
|
||||
output_data += substring[0] + rules_dict[substring]
|
||||
|
||||
output_data += start_polymer[-1]
|
||||
|
||||
start_polymer = output_data
|
||||
print(counter, end='\r')
|
||||
counter += 1
|
||||
|
||||
output_dict = {}
|
||||
for i in range(len(start_polymer)):
|
||||
if start_polymer[i] not in output_dict.keys():
|
||||
output_dict[start_polymer[i]] = 0
|
||||
output_dict[start_polymer[i]] += 1
|
||||
|
||||
print(output_dict)
|
||||
print(max(output_dict.values()) - min(output_dict.values()))
|
||||
|
||||
18
2021/14.test
Normal file
18
2021/14.test
Normal file
@@ -0,0 +1,18 @@
|
||||
NNCB
|
||||
|
||||
CH -> B
|
||||
HH -> N
|
||||
CB -> H
|
||||
NH -> C
|
||||
HB -> C
|
||||
HC -> B
|
||||
HN -> C
|
||||
NN -> C
|
||||
BH -> H
|
||||
NC -> B
|
||||
NB -> B
|
||||
BN -> B
|
||||
BB -> N
|
||||
BC -> B
|
||||
CC -> N
|
||||
CN -> C
|
||||
Reference in New Issue
Block a user