Skip to content

Instantly share code, notes, and snippets.

@antimon2
Created September 28, 2012 12:41
Show Gist options
  • Select an option

  • Save antimon2/3799578 to your computer and use it in GitHub Desktop.

Select an option

Save antimon2/3799578 to your computer and use it in GitHub Desktop.
Project Euler 68
def project_euler_68
res = 0
arr = (1..9).to_a
arr.combination(5) do |(*inner)|
s = inner.inject(&:+)
next unless s % 5 == 0
t = s.div(5) + 11
outer = (arr - inner) << 10
o_min = outer.min
inner.combination(2) do |a, b|
next unless a + b + o_min == t
(inner - [a, b]).permutation(3) do |c, d, e|
r = [o_min, t-b-c, t-c-d, t-d-e, t-e-a]
if (outer - r).empty?
res = [
res,
r.zip([a,b,c,d,e],[b,c,d,e,a]).join.to_i,
r.rotate.reverse.zip([b,a,e,d,c],[a,e,d,c,b]).join.to_i
].max
end
end
end
end
res
end
if $0 == __FILE__
p project_euler_68
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment