diff --git a/19/input.txt b/19/input.txt new file mode 100644 index 0000000..806774f --- /dev/null +++ b/19/input.txt @@ -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 + +CRnCaSiRnBSiRnFArTiBPTiTiBFArPBCaSiThSiRnTiBPBPMgArCaSiRnTiMgArCaSiThCaSiRnFArRnSiRnFArTiTiBFArCaCaSiRnSiThCaCaSiRnMgArFYSiRnFYCaFArSiThCaSiThPBPTiMgArCaPRnSiAlArPBCaCaSiRnFYSiThCaRnFArArCaCaSiRnPBSiRnFArMgYCaCaCaCaSiThCaCaSiAlArCaCaSiRnPBSiAlArBCaCaCaCaSiThCaPBSiThPBPBCaSiRnFYFArSiThCaSiRnFArBCaCaSiRnFYFArSiThCaPBSiThCaSiRnPMgArRnFArPTiBCaPRnFArCaCaCaCaSiRnCaCaSiRnFYFArFArBCaSiThFArThSiThSiRnTiRnPMgArFArCaSiThCaPBCaSiRnBFArCaCaPRnCaCaPMgArSiRnFYFArCaSiThRnPBPMgAr diff --git a/19/solve.rb b/19/solve.rb new file mode 100644 index 0000000..e96c9e5 --- /dev/null +++ b/19/solve.rb @@ -0,0 +1,46 @@ +def get_molecules(replacements, molecule) + molecules = [] + replacements.each do |pair| + index = 0 + loop do + index = molecule.index(pair.first, index) + break unless index + molecules << molecule[0...index] + pair[1] + molecule[(index + pair[0].length)..-1] + index += pair[0].length + end + end + molecules +end + +replacements = [] +medicine_molecule = '' +File.readlines(ARGV[0]).each do |line| + if line.match(/^(\w+) => (\w+)$/) + replacements << Regexp.last_match[1..2] + else + medicine_molecule = line.strip unless /\S/ !~ line + end +end + +# part 1 +puts "Part 1: #{get_molecules(replacements, medicine_molecule).uniq.length}" + +# part 2 +replacements.map! { |e| [e[1], e[0]] } # collapsing final molecule => 'e' +steps = 0 +q = [[0, medicine_molecule]] # [steps, molecule] +loop do + q.sort! { |a, b| a[1].length - b[1].length } + current = q.shift + get_molecules(replacements, current[1]).uniq.each do |mol| + if mol == 'e' + steps = current[0] + 1 + break + else + q << [current[0] + 1, mol] + end + end + break if q.empty? + break unless steps == 0 +end +puts "Part 2: #{steps}" diff --git a/19/test_1.txt b/19/test_1.txt new file mode 100644 index 0000000..1798c27 --- /dev/null +++ b/19/test_1.txt @@ -0,0 +1,5 @@ +H => HO +H => OH +O => HH + +HOH diff --git a/19/test_2.txt b/19/test_2.txt new file mode 100644 index 0000000..fdc20b0 --- /dev/null +++ b/19/test_2.txt @@ -0,0 +1,5 @@ +H => HO +H => OH +O => HH + +HOHOHO diff --git a/19/test_3.txt b/19/test_3.txt new file mode 100644 index 0000000..5075b42 --- /dev/null +++ b/19/test_3.txt @@ -0,0 +1,3 @@ +H => OO + +H2O diff --git a/19/test_4.txt b/19/test_4.txt new file mode 100644 index 0000000..70905c4 --- /dev/null +++ b/19/test_4.txt @@ -0,0 +1,7 @@ +e => H +e => O +H => HO +H => OH +O => HH + +HOHOHO