-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16a.rb
143 lines (119 loc) · 3.87 KB
/
day16a.rb
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Node = Struct.new(:name, :rate, :neighbors)
nodes = {}
File.read(ARGV[0]).lines.each do |line|
matches = line.match(/Valve (..) has flow rate=(\d*); tunnels? leads? to valves? (.*)$/)
neighbors = matches[3].split(', ') unless matches.nil?
nodes[matches[1]] = Node.new(matches[1], matches[2].to_i, neighbors) unless matches.nil?
end
#puts nodes.inspect
keys = nodes.keys
dist = {}
keys.each do |k1|
keys.each do |k2|
dist[[k1, k2]] = 1000000
dist[[k1, k2]] = 0 if k1 == k2
end
end
keys.each do |k|
nodes[k].neighbors.each do |n|
dist[[k, n]] = 1
end
end
keys.each do |k|
keys.each do |i|
keys.each do |j|
if dist[[i, k]] + dist[[k, j]] < dist[[i,j]]
dist[[i,j]] = dist[[i,k]] + dist[[k,j]]
end
end
end
end
#puts dist.inspect
def flow(order, nodes, dist)
result = 0
current = "AA"
time = 1
order.each do |node|
# puts "going from #{current} to #{node}"
time += dist[[current, node]] + 1 #account for opening time
result += nodes[node][:rate] * (31 - time)
break if time >= 31
# puts "result = #{result} after #{node} at time #{time}"
current = node
end
return result
end
#1651
#puts flow(["DD", "BB", "JJ", "HH", "EE", "CC"], nodes, dist)
keys = nodes.keys
flowkeys = keys.select {|k| nodes[k][:rate] > 0 }
require 'set'
Option = Struct.new(:nodes, :elnodes, :pressure, :opened)
best = {}
def isbest(best, opt)
key = [opt[:opened].to_a.sort, opt[:nodes].last, opt[:elnodes].last]
if best[key].nil? or best[key] < opt[:pressure]
best[key] = opt[:pressure]
return true
end
return false
end
options = [Option.new(['AA'], ['AA'], 0, Set.new())]
(1..26).each do |tick|
nextopts = []
options.each do |opt|
pressure = opt.pressure + opt[:opened].sum {|x| nodes[x][:rate] }
current = opt[:nodes].last
elcurrent = opt[:elnodes].last
# No more opening, just let it play out
if opt[:opened].size == flowkeys.size
nopt = Option.new(opt[:nodes].dup, opt[:elnodes].dup, pressure, opt[:opened].dup)
nextopts << nopt if isbest(best, nopt)
next
end
#both open
if nodes[current][:rate] > 0 && !opt[:opened].include?(current) &&
nodes[elcurrent][:rate] > 0 && !opt[:opened].include?(elcurrent)
nextopened = opt[:opened].dup
nextopened.add(current)
nextopened.add(elcurrent)
nopt = Option.new(opt[:nodes].dup, opt[:elnodes].dup, pressure, nextopened)
nextopts << nopt if isbest(best, nopt)
end
# i move, elephant opens
if nodes[elcurrent][:rate] > 0 && !opt[:opened].include?(elcurrent)
nextopened = opt[:opened].dup
nextopened.add(elcurrent)
neighbors = nodes[current][:neighbors]
neighbors.each do |neighbor|
nopt = Option.new(opt[:nodes].dup.append(neighbor), opt[:elnodes].dup, pressure, nextopened)
nextopts << nopt if isbest(best, nopt)
end
end
# i open, elephant moves
if nodes[current][:rate] > 0 && !opt[:opened].include?(current)
nextopened = opt[:opened].dup
nextopened.add(current)
neighbors = nodes[elcurrent][:neighbors]
neighbors.each do |neighbor|
nopt = Option.new(opt[:nodes].dup, opt[:elnodes].dup.append(neighbor), pressure, nextopened)
nextopts << nopt if isbest(best, nopt)
end
end
# both move
neighbors = nodes[current][:neighbors]
elneighbors = nodes[elcurrent][:neighbors]
neighbors.each do |neighbor|
elneighbors.each do |elneighbor|
nopt = Option.new(opt[:nodes].dup.append(neighbor), opt[:elnodes].dup.append(elneighbor), pressure, opt[:opened].dup)
nextopts << nopt if isbest(best, nopt)
end
end
end
options = nextopts
options = options.sort {|a,b| a[:pressure] <=> b[:pressure] }.last(10000)
#puts "#{tick}: #{options.size}"
#options.last(10).each { |x| puts x.inspect }
#puts best.inspect if tick < 6
end
puts options.map {|x| x[:pressure]}.max