Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

hur1can3 commented Nov 11, 2013

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

@yannick1974

This comment has been minimized.

Copy link
Owner Author

yannick1974 commented Dec 10, 2013

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
You can’t perform that action at this time.