-
Notifications
You must be signed in to change notification settings - Fork 7
/
Practice_Question_Code.py
134 lines (94 loc) · 3.51 KB
/
Practice_Question_Code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# coding: utf-8
# ## Google Hashcode 2021, Practice Problem
# ### Even More Pizza
#
# #### By <a href="https://chaudharyhamdan.me/">Chaudhary Hamdan </a>
# In[1]:
# Import of libraries
import os
import random
# In[2]:
# Reading file names in the Input directory
files = os.listdir('Input_Files/')
l = len(files)
# Cleaning file names
for i in range(l):
files[i] = files[i][:-3]
# In[3]:
# Function to compute and return Answers.
def solve(m,t2,t3,t4,ing,shuff):
i,score = 0, 0
# Shuffling Data to get optimized result
random.shuffle(shuff)
tt,ttt,tttt = [],[],[]
# While we have some pizzas left
while i < m:
# If 2 member team is still left and we have pizza to deliver them
if i+2 <= m and t2 > 0:
t2 -= 2 # Updating after delivering it to a team
tt.append([shuff[i],shuff[i+1]]) # Appending to the answer
ss = len(set(ing[shuff[i]]+ing[shuff[i+1]])) # Unique Ingredients delivered
i += 2 # 2 pizzas Delivered.
elif i+3 <= m and t3 > 0:
t3 -= 3
ttt.append([shuff[i],shuff[i+1],shuff[i+2]])
ss = len(set(ing[shuff[i]]+ing[shuff[i+1]]+ing[shuff[i+2]]))
i += 3
elif i+4 <= m and t4 > 0:
t4 -= 4
tttt.append([shuff[i],shuff[i+1],shuff[i+2],shuff[i+3]])
ss = len(set(ing[shuff[i]]+ing[shuff[i+1]]+ing[shuff[i+2]]+ing[shuff[i+3]]))
i += 4
# If no team is left or we don't have sufficient pizzas, Break loop
else:
break
# Score Calculation
score += ss**2
# Returning the vals computed
return len(tt),len(ttt),len(tttt),tt,ttt,tttt,score
# In[4]:
# Loop to iterate through all input files
for i in range(l):
# Opening files
with open('Input_Files/'+files[i]+'.in', 'r') as f:
# Reading contents
content = f.readlines()
ingredients = []
m, t2, t3, t4 = [int(x) for x in content[0].split()]
for j in range(1,m+1):
# Ingredients in the pizza
ingredient = content[j].split()[1:]
ingredients.append((ingredient))
bestscore = 0
besttwo,bestthree,bestfour = [],[],[]
vals = list(range(m))
# Get best score out of 1000 (increase for better answers) iterations.
for _ in range(10000):
l2,l3,l4,two,three,four,score = solve(m,2*t2,3*t3,4*t4,ingredients,vals)
# Comparing with the best score till now.
if score > bestscore:
bestscore,besttwo,bestthree,bestfour = score,two,three,four
# Writing the best results to output files in Output Directory.
with open('Output_Files/'+files[i]+'.out', 'w') as f:
f.write(str(l2+l3+l4)+'\n')
# Writing in proper format as mentioned in the question
for ii in range(l2):
f.write('2 ')
for j in range(2):
f.write(str(besttwo[ii][j])+' ')
f.write('\n')
for ii in range(l3):
f.write('3 ')
for j in range(3):
f.write(str(bestthree[ii][j])+' ')
f.write('\n')
for ii in range(l4):
f.write('4 ')
for j in range(4):
f.write(str(bestfour[ii][j])+' ')
f.write('\n')
# Printing to Terminal to get the progress.
print("Done ",files[i],"\nScore :",bestscore)
# Printing for 'THE END'.
print("All Done")
# ### Thank You