about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--day1945
-rw-r--r--day19.k14
-rw-r--r--day19.rb31
3 files changed, 90 insertions, 0 deletions
diff --git a/day19 b/day19
new file mode 100644
index 0000000..806774f
--- /dev/null
+++ b/day19
@@ -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/day19.k b/day19.k
new file mode 100644
index 0000000..4626560
--- /dev/null
+++ b/day19.k
@@ -0,0 +1,14 @@
+/ use k-3.2
+ss:{1 _' (&y=y,x) _ y,x}; / split x at y
+l:{ss[x;" "]@0 2}' -2 _ 0:"day19";
+s:*|0:"day19";
+
+ssall:{[x;y;z]{[n](n#x),z,(n+#y) _ x}' x _ss y} / all substitutions
+
+#?,/{ssall[s;x@0;x@1]}'l / part 1
+
+step1:{[s],/{ssall[s;x@1;x@0]}'l}
+lim:{:[100<#x;100#x;x]}
+c:0
+{c+:1;r:?,/step1'x;lim@r@<#:'r}/[#:;,s];
+c-1 / part 2
diff --git a/day19.rb b/day19.rb
new file mode 100644
index 0000000..e89547c
--- /dev/null
+++ b/day19.rb
@@ -0,0 +1,31 @@
+f = File.readlines("day19")
+
+r = f.pop.chomp
+f.pop
+
+$m = f.map { |x| x.chomp.split(" => ") }
+
+def ssall(t, s, r)
+  z = []
+  t.scan(s) { z << $`+r+$' }
+  z.uniq
+end
+
+p $m.map { |s,r_| ssall(r, s, r_) }.flatten.uniq.size
+
+
+a = [r]
+c = 0
+until a.include?("e") || a.empty?
+  c += 1
+  a = a.map { |t|
+    $m.map { |s,r|
+      ssall(t, r, s)
+    }
+  }.flatten.uniq
+  ms = a.map{|t|t.size}.min
+  a.delete_if { |t| t.size > ms }
+  a = a.first(100)
+end
+
+p c