In [82]:
import numpy as np
import pandas as pd
import time as time
import operator
import matplotlib.pyplot as plt
%matplotlib inline
from IPython.display import display, Image, clear_output
from scipy.spatial.distance import pdist, squareform
In [86]:
filepath = '/Users/alfredogarbuno/github-repos/hashcode-2015/data/right_angle.in'
f = open(filepath, "r+")
dimstr = f.readline().split()
paint = np.empty([int(dimstr[0]), int(dimstr[1])])
for i in range(int(dimstr[0])):
paint[i] = np.array(list(f.readline().split()[0].replace(".", "0").replace("#", "1")), np.int32)
f.close()
In [87]:
plt.figure(figsize=(10,10))
plt.imshow(paint)
fig = plt.gca()
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
In [88]:
dim = np.array(dimstr, np.int32)
print dim
In [90]:
density = paint.sum() / dim.prod()
print density
In [105]:
minsize = int(dim.min() * density)
minsize = 12
print minsize
In [106]:
indices = np.arange(1,dim.prod()+1).reshape(dim)
print indices
In [107]:
# Generate the indices, in the test example we can only have sqp(1) so we lose 2 rows and 2 columns, first and last
# in both cases.
row, col = np.indices((dim[0]-2*minsize, dim[1]-2*minsize))
centers = indices[row+minsize, col+minsize]
centers
Out[107]:
In [108]:
# Test case, let's take the center in cell 9
# We access that center by knowing it is in the second row, so the first row in the center matrix
# And we know that it is the first column
def discrete_kernel(center, minsize):
dist = np.empty((2*minsize+1,2*minsize+1))
decay = np.logspace(0,1,minsize+1 )
for xstep in range(-minsize,minsize+1):
for ystep in range(-minsize,minsize+1):
dist[1+xstep][1+ystep] = 1 * paint[center[0]+xstep][center[1]+ystep]
return (dist.sum()+0.0)/np.array(dist.shape).prod()
In [109]:
# Computes the densities of zeroes for every possible center
cands = []
cands = [ (ycoord, discrete_kernel([row+minsize, ycoord%dim[1]-1], minsize)) for row in range(centers.shape[0])
for ycoord in centers[row]]
print len(cands)
In [110]:
# Given a threshold filter the centers most probable to a successful square paint
threshold = 0.6
cands = [(key, value) for (key,value) in cands if value > threshold]
cands = sorted(cands, key=lambda element: (-element[1], element[0]))
#cands = sorted(cands, key=operator.itemgetter( 1))
#cands.reverse()
In [120]:
print cands[0][1]
print (1-cands[0][1])*(2*minsize+1)**2
print (cands[0][1])*(2*minsize+1)**2/(2*minsize + 1 )**2
In [98]:
def paint_square(center, minsize):
for xstep in range(-minsize,minsize+1):
for ystep in range(-minsize,minsize+1):
paint[center[0]+xstep][center[1]+ystep] = 0.0
In [99]:
plt.figure(figsize=(10,10))
plt.imshow(paint)
fig = plt.gca()
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
In [79]:
cands[0][0]
Out[79]:
In [80]:
fig=plt.figure()
n = len(cands)
i = 0
paints = 1
plt.figure(figsize=(10,10))
plt.imshow(paint)
clear_output(wait=True)
display(plt.gcf())
time.sleep(2)
for cand in cands:
center = ( cand[0] // dim[1], cand[0] % dim[1] - 1)
if cand[0] == cands[0][0]:
paint_square(center, minsize)
plt.imshow(paint)
clear_output(wait=True)
display(plt.gcf())
time.sleep(1)
else:
if discrete_kernel(center, minsize) > threshold:
paint_square(center, minsize)
paints += 1
plt.imshow(paint)
clear_output(wait=True)
display(plt.gcf())
time.sleep(0.001)
clear_output(wait=True)
In [81]:
paints
Out[81]:
In [104]:
cands[1000]
Out[104]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: