Skip to content

Instantly share code, notes, and snippets.

@joeletizia
Created February 6, 2013 02:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeletizia/4719640 to your computer and use it in GitHub Desktop.
Save joeletizia/4719640 to your computer and use it in GitHub Desktop.
A solution (at least I think it's right...) to http://challenge.signpost.com/leveltwo The Ransom Glad you've made it this far, intrepid friend of Max. To tell you a little about ourselves, our goal is to end winter in Chicago. We've read our Ries and plan to start small, with an MVP. We currently have the resources to build a structure covering …
require 'open-uri'
def adj_sum(multi,x,y)
sum = multi[x][y].to_i
if x > 0
sum += multi[x-1][y].to_i
end
if x < 999
sum += multi[x+1][y].to_i
end
if y > 0
sum += multi[x][y-1].to_i
end
if y < 17
sum += multi[x][y+1].to_i
end
sum
end
data = open("http://challenge.signpost.com/static/density.txt")
lines = Array.new
data.each_line {|line| lines << line.split(' ')}
max = 0
line_num = 0
for k in (0...1000)
for j in (0...18)
total_people = adj_sum(lines,k,j)
max = total_people unless max > total_people
end
end
puts "max is #{max}"
@smholloway
Copy link

I arrived at the same result using JavaScript--and was told the answer was Wrong.

You should be able to run this from the console in Chrome Developer Tools on the density page:

var data = document.getElementsByTagName('pre')[0].innerHTML.split(' ');

var getAdjacent = function(data, index) {
  var top = data[index-18] - 0 || 0,
    bottom = data[index+18] - 0 || 0,
    left = data[index-1] - 0 || 0,
    right = data[index+1] - 0 || 0,
    center = data[index] - 0 || 0;
  return top + bottom + left + right + center;
}

var max = 0;
for (var i = 0, l = data.length; i < l; i++) {
  var sum = getAdjacent(data, i);
  max = Math.max(max, sum);
}

console.log(max);

@joeletizia
Copy link
Author

Wow, didn't notice you wrote here, Seth. Thanks.

I figured I was right. Thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment