Skip to content

Instantly share code, notes, and snippets.

@erssebaggala
Created February 28, 2018 19:01
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 erssebaggala/90c8334576cc6450dd904d9a091f43d4 to your computer and use it in GitHub Desktop.
Save erssebaggala/90c8334576cc6450dd904d9a091f43d4 to your computer and use it in GitHub Desktop.
Hash_Code_Pizza_Practice_2018_Try_01
import os
import sys
input_file = sys.argv[1]
def get_all_factors(n):
factors = []
for i in range(1,n+1):
if n%i == 0:
factors.append(i)
return factors
R = 0
C = 0
L = 0
H = 0
(r,c) = (0,0)
pizza = []
slices = []
areas = range(2,7,1)
masks = []
with open(input_file, "r") as f:
data = f.readlines()
line_count = 0
for line in data:
line_count += 1
words = line.split()
if line_count == 1:
(R,C,L,H) = map( lambda x: int(x), words)
continue
pizza.append( list(line.rstrip()) )
# print pizza
# print( "R:{} C:{} L:{} H:{}".format(R,C,L,H) )
# print pizza[ len(pizza)-1 ]
continue_checking = True
# for area in range(H,2*L-1,-1):
for area in [H]:
factors_of_area = get_all_factors(area)
# print("Area={} Factors={}".format(area,factors_of_area))
while continue_checking == True:
for f in factors_of_area:
rows = f
cols = area/f
# print("rows: {} cols: {}".format(rows, cols))
if rows > R and cols > C:
continue_checking = False
break
if rows > R or cols > C: continue
num_of_tomatoes = 0
num_of_mushrooms=0
(rr,cc) = (r,c)
cell_checked = False
if r + rows > R or c + cols > C:
continue_checking = False
break
# Check the mask
for r_i in range(r, r+rows,1):
for c_i in range(c, c+cols, 1):
(rr,cc) = (r_i,c_i)
# Skip cells that have already by checked
if pizza[r_i][c_i] == 'X':
continue
if pizza[r_i][c_i] == 'T':
num_of_tomatoes += 1
else:
num_of_mushrooms += 1
# print("c: {} r: {}".format(c, r))
if rr > R or cc > C :
continue_checking = False
if num_of_tomatoes < L or num_of_mushrooms < L:
# print("n and n")
# print("c: {} r: {} a:".format(c, r))
# print("cc: {} rr: {} a:".format(cc, rr))
# print("----")
continue
else:
for r_i in range(r, rows, 1):
for c_i in range(c, cols, 1):
pizza[r_i][c_i] = 'X'
slices.append((r, c, rr, cc))
# print("{} {} {} {}".format(r, c, rr, cc))
(r, c) = (rr+1, cc+1)
break
continue_checking = False
# for p in pizza: print p
print ("{}".format(len(slices)))
for s in slices:
print("{} {} {} {}".format(s[0],s[1],s[2], s[3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment