Skip to content

Instantly share code, notes, and snippets.

@RKushnir
Created January 6, 2012 19:28
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 RKushnir/1572017 to your computer and use it in GitHub Desktop.
Save RKushnir/1572017 to your computer and use it in GitHub Desktop.
Count the ships
field = [
[1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, ],
[1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, ],
[1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ],
[0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, ],
[1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, ],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, ],
]
count = 0
field.each do |row|
prev = 0
0.upto(row.size - 2) do |i|
count += ~prev & row[i] & row[i + 1]
prev = row[i]
end
end
puts count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment