-
Notifications
You must be signed in to change notification settings - Fork 3
/
3-sequential-lee.rb
103 lines (77 loc) · 2.63 KB
/
3-sequential-lee.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
# Solves a board using Lee but sequentially.
require_relative 'lib/lee'
board_filename, output_filename, expansions_dir, *rest = ARGV
raise 'no input filename' unless board_filename
raise 'too many arguments' unless rest.empty?
if expansions_dir
system 'rm', '-rf', expansions_dir
Dir.mkdir expansions_dir
end
board = Lee.read_board(board_filename)
obstructed = Lee::Matrix.new(board.height, board.width)
board.pads.each do |pad|
obstructed[pad.y, pad.x] = 1
end
depth = Lee::Matrix.new(board.height, board.width)
def expand(board, obstructed, depth, route)
start_point = route.a
end_point = route.b
# From benchmarking - we're better of allocating a new cost-matrix each time rather than zeroing
cost = Lee::Matrix.new(board.height, board.width)
cost[start_point.y, start_point.x] = 1
wavefront = [start_point]
loop do
new_wavefront = []
wavefront.each do |point|
point_cost = cost[point.y, point.x]
Lee.adjacent(board, point).each do |adjacent|
next if obstructed[adjacent.y, adjacent.x] == 1 && adjacent != route.b
current_cost = cost[adjacent.y, adjacent.x]
new_cost = point_cost + Lee.cost(depth[adjacent.y, adjacent.x])
if current_cost == 0 || new_cost < current_cost
cost[adjacent.y, adjacent.x] = new_cost
new_wavefront.push adjacent
end
end
end
raise 'stuck' if new_wavefront.empty?
break if cost[end_point.y, end_point.x] > 0 && cost[end_point.y, end_point.x] < new_wavefront.map { |marked| cost[marked.y, marked.x] }.min
wavefront = new_wavefront
end
cost
end
def solve(board, route, cost)
start_point = route.b
end_point = route.a
solution = [start_point]
loop do
adjacent = Lee.adjacent(board, solution.last)
lowest_cost = adjacent
.reject { |a| cost[a.y, a.x].zero? }
.min_by { |a| cost[a.y, a.x] }
solution.push lowest_cost
break if lowest_cost == end_point
end
solution.reverse
end
def lay(depth, solution)
solution.each do |point|
depth[point.y, point.x] += 1
end
end
solutions = {}
board.routes.each do |route|
cost = expand(board, obstructed, depth, route)
solution = solve(board, route, cost)
if expansions_dir
Lee.draw board, solutions.values, [[cost.keys, solution]], File.join(expansions_dir, "expansion-#{route.object_id}.svg")
end
lay depth, solution
solutions[route] = solution
end
raise 'invalid solution' unless Lee.solution_valid?(board, solutions)
cost, depth = Lee.cost_solutions(board, solutions)
puts "routes: #{board.routes.size}"
puts "cost: #{cost}"
puts "depth: #{depth}"
Lee.draw board, solutions.values, output_filename if output_filename