1
require 'set'
2
3
# input = DATA.read
4
input = File.read('11.input')
5
6
paths = {}
7
ipaths = {}
8
9
known_terminators = Set.new
10
11
input.strip.lines.each do |line|
12
from, to = line.strip.split(": ")
13
from = from.intern
14
to = to.split(" ").map(&:intern)
15
paths[from] = to
16
to.each do |t|
17
ipaths[t] ||= []
18
ipaths[t] << from
19
end
20
end
21
22
$count = 0
23
24
def traverse(paths, spot, seen_dac, seen_fft)
25
$count += 1
26
paths[spot].each do |next_spot|
27
if next_spot == :out
28
$count += 1 if seen_dac && seen_fft
29
else
30
traverse(paths, next_spot, seen_dac || next_spot == :dac, seen_fft || next_spot == :fft)
31
end
32
end
33
$count -= 1
34
end
35
36
traverse(paths, :svr, false, false)
37
p $count
38
39
__END__
40
svr: aaa bbb
41
aaa: fft
42
fft: ccc
43
bbb: tty
44
tty: ccc
45
ccc: ddd eee
46
ddd: hub
47
hub: fff
48
eee: dac
49
dac: fff
50
fff: ggg hhh
51
ggg: out
52
hhh: out
53