aboutsummaryrefslogtreecommitdiff
path: root/day19.rb
blob: e89547c97467341ca2900c2cc87100b6b0a66d08 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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