Skip to content

Instantly share code, notes, and snippets.

@yannick1974
Created November 6, 2013 11:26
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 yannick1974/7334581 to your computer and use it in GitHub Desktop.
Save yannick1974/7334581 to your computer and use it in GitHub Desktop.
import numpy as np
from matplotlib import pyplot as plt
def waterfill(walls):
land = np.zeros((len(walls), max(walls)), dtype=int)
# Put the walls
for index, value in enumerate(walls):
land[index, 0:value] = 2
# Drop water
land[land == 0] = 1
# Remove flowed-out water
for height in range(0, max(walls))[::-1]:
left_max = land[:, height].argmax()
right_max = len(walls) - land[:, height][::-1].argmax()
land[:left_max, height] = 0
land[right_max:, height] = 0
return len(land[land == 1])
def waterfill_with_plots(walls):
land = np.zeros((len(walls), max(walls)), dtype=int)
fig = plt.figure()
# Put the walls
for index, value in enumerate(walls):
land[index, 0:value] = 2
dry_land = np.copy(land)
dry_fig = fig.add_subplot(1, 2, 1)
dry_fig.imshow(np.transpose(dry_land),
origin="lower",
interpolation="nearest")
# Drop water
land[land == 0] = 1
# Remove flowed-out water
for height in range(0, max(walls))[::-1]:
left_max = land[:, height].argmax()
right_max = len(walls) - land[:, height][::-1].argmax()
land[:left_max, height] = 0
land[right_max:, height] = 0
wet_fig = fig.add_subplot(1, 2, 2)
wet_fig.imshow(np.transpose(land),
origin="lower",
interpolation="nearest")
fig.suptitle("{} filled blocks".format(len(land[land == 1])))
fig.show()
@hur1can3
Copy link

The graph is wrong for [7, 1, 1, 1, 7, 1, 1, 1, 1, 7], but the solution is wright 42

@yannick1974
Copy link
Author

Well, the graph looks good to me...

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