Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 19/input.txt
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions 19/solve.rb
Original file line number Diff line number Diff line change
@@ -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}"
5 changes: 5 additions & 0 deletions 19/test_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
H => HO
H => OH
O => HH

HOH
5 changes: 5 additions & 0 deletions 19/test_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
H => HO
H => OH
O => HH

HOHOHO
3 changes: 3 additions & 0 deletions 19/test_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
H => OO

H2O
7 changes: 7 additions & 0 deletions 19/test_4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
e => H
e => O
H => HO
H => OH
O => HH

HOHOHO