# a function to create a random map as simulated input for testing:
def get_map(x_size, y_size, min_height, max_height):
import random
return [[random.randint(min_height, max_height) for x in range(x_size)] for y in range(y_size)]
# a function to nicely print the map for debug and visualization
def print_map(hmap):
print(*hmap, sep="\n")
# calculate approximate land area where the height is greater than zero
# map is a list of lists, tile_size is in m², min_level is the sea level
def calc_land_area(hmap, tile_size=100, min_level=0):
land_tiles = sum(len([tile for tile in row if tile>min_level]) for row in hmap)
return tile_size * land_tiles
try:
hmap = get_map(5, 5, 0, 3)
print_map(hmap)
print("land area:", calc_land_area(hmap), "m²")
except Exception as e:
print(e)
IyBhIGZ1bmN0aW9uIHRvIGNyZWF0ZSBhIHJhbmRvbSBtYXAgYXMgc2ltdWxhdGVkIGlucHV0IGZvciB0ZXN0aW5nOgpkZWYgZ2V0X21hcCh4X3NpemUsIHlfc2l6ZSwgbWluX2hlaWdodCwgbWF4X2hlaWdodCk6CiAgICBpbXBvcnQgcmFuZG9tCiAgICByZXR1cm4gW1tyYW5kb20ucmFuZGludChtaW5faGVpZ2h0LCBtYXhfaGVpZ2h0KSBmb3IgeCBpbiByYW5nZSh4X3NpemUpXSBmb3IgeSBpbiByYW5nZSh5X3NpemUpXQoKIyBhIGZ1bmN0aW9uIHRvIG5pY2VseSBwcmludCB0aGUgbWFwIGZvciBkZWJ1ZyBhbmQgdmlzdWFsaXphdGlvbgpkZWYgcHJpbnRfbWFwKGhtYXApOgogICAgcHJpbnQoKmhtYXAsIHNlcD0iXG4iKQogICAgCgojIGNhbGN1bGF0ZSBhcHByb3hpbWF0ZSBsYW5kIGFyZWEgd2hlcmUgdGhlIGhlaWdodCBpcyBncmVhdGVyIHRoYW4gemVybwojIG1hcCBpcyBhIGxpc3Qgb2YgbGlzdHMsIHRpbGVfc2l6ZSBpcyBpbiBtwrIsIG1pbl9sZXZlbCBpcyB0aGUgc2VhIGxldmVsCmRlZiBjYWxjX2xhbmRfYXJlYShobWFwLCB0aWxlX3NpemU9MTAwLCBtaW5fbGV2ZWw9MC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