Skip to content

Instantly share code, notes, and snippets.

@ejallday
Last active December 17, 2015 03:38
Show Gist options
  • Select an option

  • Save ejallday/5544156 to your computer and use it in GitHub Desktop.

Select an option

Save ejallday/5544156 to your computer and use it in GitHub Desktop.
Chris Pine's Civilization problem
# These are just to make the map easier to read
# "M" is visually more dense than "o"
M = 'land'
o = 'water'
world = [[o,o,o,o,o,M,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,o,o,M,o,o,M,M,o],
[o,o,o,M,o,M,o,o,o,M,o],
[o,o,o,M,o,M,M,o,o,o,o],
[o,o,o,o,M,M,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,M],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,o,o,o]]
def continent_size(world, x, y)
if world[y][x] != 'land'
# Either it's water or we already
# counted it, but either way, we don't
# want to count it now
return 0
end
# So first we count this title...
size = 1
world[y][x] = 'counted land'
# ...then we count all of the
# neighboring eight tiles (and,
# of course, their neighbors by
# way of recursion)
size += continent_size(world, x-1, y-1)
size += continent_size(world, x , y-1)
size += continent_size(world, x+1, y-1)
size += continent_size(world, x-1, y )
size += continent_size(world, x+1, y )
size += continent_size(world, x-1, y+1)
size += continent_size(world, x , y+1)
size += continent_size(world, x+1, y+1)
size
end
#puts continent_size(world, 5, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment