[2022][ruby][16.2]

main
Alpha Chen 2 years ago
parent b32bf143f9
commit 6db3326726
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -36,21 +36,20 @@ Move = Data.define(:reward, :target, :path) do
end end
State = Data.define(:net, :current, :max, :turn, :pressure, :opened) do State = Data.define(:net, :current, :max, :turn, :pressure, :opened) do
def best_moves def best_moves(best)
best_moves = []
best_state = self best_state = self
best[opened] = [best.fetch(opened, pressure), pressure].max
moves.each do |move| moves.each do |move|
next_state = applied(move) next_state = applied(move)
next_state, next_moves = next_state.best_moves; next_state = next_state.best_moves(best);
next_moves << move
if next_state.pressure > best_state.pressure if next_state.pressure > best_state.pressure
best_moves = next_moves
best_state = next_state best_state = next_state
end end
end end
[best_state, best_moves] best_state
end end
def applied(move) def applied(move)
@ -60,7 +59,7 @@ State = Data.define(:net, :current, :max, :turn, :pressure, :opened) do
max, max,
turn + move.cost, turn + move.cost,
pressure + move.reward, pressure + move.reward,
opened + [move.target], Set[*opened, move.target],
) )
end end
@ -84,6 +83,21 @@ end
network = Network.new(valves.to_h { [_1.name, _1] }) network = Network.new(valves.to_h { [_1.name, _1] })
state = State.new(network, "AA", 30, 0, 0, []) # part 1
best_state, moves = state.best_moves # state = State.new(network, "AA", 30, 0, 0, [])
pp best_state, moves # best_state = state.best_moves({})
# pp best_state
# part 2
state = State.new(network, "AA", 26, 0, 0, [])
best = {}
state.best_moves(best)
pp best
.transform_keys(&:to_set)
.to_a
.combination(2)
.select { _1[0].disjoint?(_2[0]) }
.map { _1[1] + _2[1] }
.max

Loading…
Cancel
Save