-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_euler_82.rb
163 lines (136 loc) · 3.85 KB
/
project_euler_82.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Path sum: three ways
# Problem 82
# NOTE: This problem is a more challenging version of Problem 81.
#
# The minimal path sum in the 5 by 5 matrix below, by starting in any cell in the left column and finishing in any cell in the right column, and only moving up, down, and right, is indicated in red and bold; the sum is equal to 994.
#
# 131 673 234 103 18
# 201 96 342 965 150
# 630 803 756 422 111
# 537 699 497 121 956
# 805 732 524 37 331
#
# Find the minimal path sum, in matrix.txt (right click and "Save Link/Target As..."), a 31K text file containing a 80 by 80 matrix, from the left column to the right column.
require 'singleton'
class MatrixAnalyzer
include Singleton
attr_accessor :content
attr_reader :d, :h
def set_d
if @d.nil?
(0...content.size).each do |i|
(0...content.size).each do |j|
@d = content[i][j] if @d.nil? || @d > content[i][j]
end
end
end
@d
end
# Heuristic matrix(based on Manhattan distance)
def set_h
if @h.nil?
@h = []
(0...content.size).each do |i|
@h[i] ||= []
(0...content.size).each do |j|
dx = 0
dy = (j - content.size + 1).abs
h[i][j] = d * (dx + dy)
end
end
end
@h
end
end
class Node
attr_reader :i, :j, :matrix, :prev, :g, :f
def initialize(i, j, prev_node = nil)
@matrix = MatrixAnalyzer.instance
@i, @j = i, j
@prev = prev
set_g
set_f
end
def prev=(prev)
@prev = prev
set_g
set_f
end
# f = g + h
def set_f
@f = g.to_i + matrix.h[i][j]
end
def set_g
@g =
if prev.nil?
matrix.content[i][j]
else
prev.g + matrix.content[i][j]
end
end
def print_trace(sum = 0)
# puts "i[#{ self.i }][#{ self.j }], val=#{ matrix.content[self.i][self.j] }" \
# " g=#{ self.g } f=#{ self.f }"
if prev.nil?
puts "sum = #{ sum + matrix.content[self.i][self.j] }"
return
end
prev.print_trace(sum + matrix.content[self.i][self.j])
end
end
class AStar
def find_path(matrix, start, goal)
open_nodes = start
until open_nodes.empty?
current = open_nodes.min_by { |node| node.f }
goal.each do |goal_node|
if current.i == goal_node[0] && current.j == goal_node[1]
return current
end
end
open_nodes -= [current]
closed_nodes << current
# go right, top or bottom
neighbors = []
if current.i - 1 >= 0 && closed_nodes_exclude?(current.i - 1, current.j)
neighbors << Node.new(current.i - 1, current.j)
end
if matrix[current.i][current.j + 1] && closed_nodes_exclude?(current.i, current.j + 1)
neighbors << Node.new(current.i, current.j + 1)
end
if matrix[current.i + 1] && closed_nodes_exclude?(current.i + 1, current.j)
neighbors << Node.new(current.i + 1, current.j)
end
neighbors.each do |neighbor|
neighbor.prev = current
if open_nodes.detect { |node| node.i == neighbor.i && node.j == neighbor.j }.nil?
open_nodes << neighbor
end
end
end
puts 'Unable to find path'
end
def closed_nodes_exclude?(i, j)
closed_nodes.detect { |node| node.i == i && node.j == j }.nil?
end
def closed_nodes
@closed_nodes ||= []
end
end
# input = [
# [131, 673, 234, 103, 18],
# [201, 96, 342, 965, 150],
# [630, 803, 746, 422, 111],
# [537, 699, 497, 121, 956],
# [805, 732, 524, 37, 331],
# ]
file = File.read('project_euler_82_matrix.txt')
input = file.split("\n").map { |row| row.split(',').map(&:to_i) }
m = MatrixAnalyzer.instance
m.content = input
m.set_d
m.set_h
start_nodes = (0...input.size).inject([]) { |memo, i| memo << Node.new(i, 0) }
goal_nodes = (0...input.size).inject([]) { |memo, i| memo << [i, input.size - 1] }
klass = AStar.new
klass.find_path(m.content, start_nodes, goal_nodes).print_trace