-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem28-solved.rb
155 lines (135 loc) · 2.56 KB
/
problem28-solved.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
# Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
# 21 22 23 24 25
# 20 7 8 9 10
# 19 6 1 2 11
# 18 5 4 3 12
# 17 16 15 14 13
# It can be verified that the sum of the numbers on the diagonals is 101.
# What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
class Map
attr_accessor :size, :map, :center
def initialize(size)
# Create map
@size = size
@map = []
for x in 0..@size - 1
@map[x] = []
for y in 0..@size - 1
@map[x][y] = 0
end
end
# Find center
@center = size / 2
self.update(@center, @center, 1)
end
def check(x, y)
if @map[y][x] == 0
return false
else
return true
end
end
def update(x, y, value)
@map[y][x] = value
end
def diagonal_sum
diagonals = []
# First diagonal - Top left to bottom right
for x in 0..size-1
diagonals << @map[x][x]
diagonals << @map[x][size - x - 1]
end
return diagonals.uniq!
end
def display
for y in 0..size - 1
puts @map[y].join(' ')
end
puts "- " * @size
end
end
# Create iterator class
class Tortue
attr_accessor :map, :x, :y, :next_x, :next_y, :direction, :value, :alive
def initialize(map, x, y)
@map = map
@x = x
@y = y
@next_x = x
@next_y = y
@direction = "EAST"
@value = 0
@alive = true
end
# Sense a wall, update next position
def sense
case @direction
when "EAST"
return @map.check(@x, @y-1)
when "NORTH"
return @map.check(@x-1, @y)
when "WEST"
return @map.check(@x, @y+1)
when "SOUTH"
return @map.check(@x+1, @y)
end
end
def move
case @direction
when "EAST"
@next_x = @x + 1
when "NORTH"
@next_y = @y - 1
when "WEST"
@next_x = @x - 1
when "SOUTH"
@next_y = @y + 1
end
@value += 1
@map.update(@x, @y, value)
@x = @next_x
@y = @next_y
end
def turn
case @direction
when "EAST"
@direction = "NORTH"
when "NORTH"
@direction = "WEST"
when "WEST"
@direction = "SOUTH"
when "SOUTH"
@direction = "EAST"
end
end
def alive?
return @value < @map.size ** 2
end
def next
if self.sense
#puts "Continue..."
self.move
else
#puts "Turn..."
self.turn
self.move
end
end
def display
puts "[ #{@x} , #{@y} ] - Moving #{@direction} - Value: #{@value}"
end
end
# Generate map
# map = Map.new(5)
map = Map.new(1001)
map.display()
# Generate iterator
tortue = Tortue.new(map, map.center, map.center)
while tortue.alive? do
tortue.next
#map.display
#tortue.display
end
# Sum diagonals
puts map.diagonal_sum.inspect
puts map.diagonal_sum.reduce(:+)