Skip to content

Instantly share code, notes, and snippets.

@sarahzrf
Created December 9, 2021 18:21
Show Gist options
  • Save sarahzrf/c14064ebe6dc88b240de49bf86f6146a to your computer and use it in GitHub Desktop.
Save sarahzrf/c14064ebe6dc88b240de49bf86f6146a to your computer and use it in GitHub Desktop.
def neighbors(grid, x, y)
ns = []
ns << grid[y][x - 1] if x > 0
ns << grid[y][x + 1] if x + 1 < grid[y].length
ns << grid[y - 1][x] if y > 0
ns << grid[y + 1][x] if y + 1 < grid.length
ns
end
map = $<.map {|l| l.chomp.chars.map(&:to_i)}
risk = 0
map.each_with_index do |row, y|
row.each_with_index do |height, x|
ns = neighbors(map, x, y)
next unless height < ns.min
risk += height + 1
end
end
print risk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment