-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem68.rb
85 lines (77 loc) · 2.15 KB
/
problem68.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
#!/bin/env ruby
require 'euler_helper.rb'
def range
range = 1..10
end
def find used = []
range.to_a.except(used).inject([]) do |acc1, p1|
acc1 += range.to_a.except(used, p1).inject([]) do |acc2, p2|
acc2 += range.to_a.except(used, p1, p2).map{|x| [p2]+[x]}
end.map {|x| [p1]+x.to_a}
end
end
def nxt triple, used = []
# puts triple.inspect
# puts used.inspect
# puts used.except(triple.last)
find(used.except(triple.last)).select {|triple2| triple.last == triple2[1]}
end
def nxt_try triple, n, used = []
variants = nxt(triple, used).select {|arr| arr.sum == triple.sum}
if n == 0
variants
else
variants.inject([]) do |acc, triple1|
acc += nxt_try(triple1, n-1,used+triple1).inject([]) do |acc2, triple2|
unless tripl2.empty?
acc2 += triple2
else
acc2
end
end.map {|s| [triple1] + s}
acc
end
end
end
def solve2
triples = range.inject([]) do |acc, center|
inits = find.select {|arr| arr[1] == center }
unless inits.empty?
acc += inits.inject([]) do |acc1, init|
r= recur(init, 3, [],[],center).inject(:+).inject(:+).inject(:+).select{|a| a.size == 5}.select{|a| a.last.last==a.first[1]} rescue []
#puts r.inspect
unless r.empty?
acc1 += r
else
acc1
end
end
else
acc
end
end
end
# nxt_try([4,2,3], 2).each {|a| puts a.inspect }
#solve2.each{|k,v| puts "k: #{k} #{v.inspect}" }
def recur triple, n, used = [], stack = [], enddig = nil
#puts 'st'+stack.inspect
#puts 'u'+used.inspect
stack.push triple
used += triple
if n == 0
variants = nxt(triple, used.except(enddig)).select {|arr| arr.sum == triple.sum}
if variants.empty?
return []
else
return variants.map{|v| stack +[v] }
end
else
variants = nxt(triple, used).select {|arr| arr.sum == triple.sum}
variants.map {|v| recur(v, n-1, used+v, stack.clone, enddig) }
end
end
puts solve2.inspect
rr = solve2.map{|a| a.map{|x| x.join.to_i}}
rr.delete_if{|arr| rr.detect {|arr2| (arr & arr2).size==arr.size && arr.first != arr.min} }.inspect
#
puts rr.select{|arr| arr.join.size == 16}.map {|arr| arr.join.to_i}.max