Created
June 5, 2012 14:30
-
-
Save danshultz/2875363 to your computer and use it in GitHub Desktop.
Project Euler Problem 67 Solution in Multiple Languages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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