-
Notifications
You must be signed in to change notification settings - Fork 0
/
p061.rb
149 lines (136 loc) · 2.9 KB
/
p061.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
# recursively find the next value in a group that not used yet
def findSequence(mHash, prefix)
if $sequence.length == 6
if prefix.to_s == $sequence['triangle'].to_s[0,2]
return true
end
else
prefixNums = availableNumbers(mHash, prefix)
prefixNums.each do |key, value|
value.each do |entry|
$sequence[key] = entry
if findSequence(mHash, entry % 100)
return true
else
$sequence.delete(key)
end
end
end
end
return false
end
def availableNumbers(mHash, prefix)
keys = $sequence.keys
prefixNums = {}
mHash.each do |key,value|
value.each do |entry|
unless $sequence.key? key
if entry.to_s[0,2] == prefix.to_s
if prefixNums.key? key
prefixNums[key] << entry
else
prefixNums[key] = [entry]
end
end
end
end
end
return prefixNums
end
# First, find all 4 digit numbers for each category
mHash = {}
mHash['triangle'] = []
mHash['square'] = []
mHash['pentagonal'] = []
mHash['hexagonal'] = []
mHash['heptagonal'] = []
mHash['octagonal'] = []
n = 0
#triangle
while true
n += 1
newNum = n * (n + 1) / 2
if newNum.to_s.length < 4
next
elsif newNum.to_s.length > 4
break
else
mHash['triangle'] << newNum
end
end
n = 0
while true
n += 1
newNum = n * n
if newNum.to_s.length < 4
next
elsif newNum.to_s.length > 4
break
else
mHash['square'] << newNum
end
end
n = 0
while true
n += 1
newNum = n * (3*n - 1) / 2
if newNum.to_s.length < 4
next
elsif newNum.to_s.length > 4
break
else
mHash['pentagonal'] << newNum
end
end
n = 0
while true
n += 1
newNum = n * (2*n - 1)
if newNum.to_s.length < 4
next
elsif newNum.to_s.length > 4
break
else
mHash['hexagonal'] << newNum
end
end
n = 0
while true
n += 1
newNum = n * (5*n - 3) / 2
if newNum.to_s.length < 4
next
elsif newNum.to_s.length > 4
break
else
mHash['heptagonal'] << newNum
end
end
n = 0
while true
n += 1
newNum = n * (3*n - 2)
if newNum.to_s.length < 4
next
elsif newNum.to_s.length > 4
break
else
mHash['octagonal'] << newNum
end
end
$sequence = {}
# walk once over triangle. One of the numbers must be there
mHash['triangle'].each do |triangleNum|
$sequence['triangle'] = triangleNum
if (findSequence(mHash, triangleNum % 100))
break
else
$sequence.delete('triangle')
end
end
puts $sequence.inspect
sum = 0
$sequence.each do |key,value|
sum += value
end
puts "sum: #{sum}"