Skip to content

Instantly share code, notes, and snippets.

@danshultz
Created June 5, 2012 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danshultz/2875363 to your computer and use it in GitHub Desktop.
Save danshultz/2875363 to your computer and use it in GitHub Desktop.
Project Euler Problem 67 Solution in Multiple Languages
fs = require('fs')
triangle = fs.readFileSync('../triangle.txt', 'utf-8')
.split('\r\n')
.map((x) -> x.split(' ').map((y) -> +y))
.reverse()
.splice(1)
reduction = (prev, current) ->
current.map (x, i) ->
a = Math.max.apply(Math, [x + prev[i], x + prev[i + 1]])
console.log triangle.slice(1).reduce(reduction, triangle[0])
triangle = File.open("../triangle.txt").map{|line|
line.split.map(&:to_i)
}.reverse
puts triangle.slice(1, triangle.length).reduce(triangle.first) { |prev, new|
index = -1
new.map { |x|
index += 1
[x + prev[index], x + prev[index + 1]].max
}
}.max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment