
Powercoding
AIO Solutions
"Do not go where the path may lead. Instead, go where there is no path and leave a trail"
Ralph Waldo Emerson

About Powercoding

Introducing Powercoding
We are a team of coders.
Our team currently consists of four people. We use Zoom to discuss current problems we are working on such as Informatics competition challenges and other related stuff. We generally use python to solve these problems, mostly consisting of reading a file, extracting data and manipulating the data to write into an output file as an answer to a problem.
AIO 2020 Problem 1
We came up with two solutions.
​
baublesIn = open("baublesin.txt","r") #Open files
baublesOut = open("baublesout.txt","w")
myInput = baublesIn.read().split()
oR = int(myInput[0]) #Asign values
oB = int(myInput[1])
spare = int(myInput[2])
kR = int(myInput[3])
kB = int(myInput[4])
sR = oR + spare
sB = oB + spare
total = oR + oB + spare - kR - kB
print("sR - kR:", sR- kR, "; sB - kB: ", sB-kB, "; total: ", total)
destruct = min(sR - kR, sB - kB, total) #Finding which colour to destruct
print("destruct: ", destruct)
if int(destruct) < 0: #Writing answer
baublesOut.write(str(0) + "\n")
else:
baublesOut.write(str(int(destruct + 1)) + "\n")
print(str(destruct + 1))
baublesOut.close()
baublesIn.close()
We also have:
# Input file-------------------------------------------
in_filename = 'baublesin.txt'
# Set up input()---------------------------------------
in_file = open(in_filename, 'r')
def input():
return in_file.readline()
# Code-------------------------------------------------
line = input()
RO, BO, S, RP, BP = map(int, line.split())
total = 0
if RO+S < RP or BO+S < BP or RO+BO+S < RP+BP:
total = 0
elif RO == BO and RP == BP:
if BO >= BP:
total = S+BO-BP+1
elif BO < BP:
total = S+BO-BP
# elif BO == BP:
# total = S+1
else:
total = min(S+BO-BP+1, S+RO-RP+1)
outfile.write(str(total))
# Close opened file------------------------------------
in_file.close()
out_file.close()
AIO 2020 Problem 2
We came up with three solutions.
cookiesin = open("cookiesin.txt","r")
cookiesout = open("cookiesout.txt","w")
myInput = cookiesin.read().split() #Asigning values
day = int(myInput[0])
fstFacProd = int(myInput[1])
scdFacCost = int(myInput[2])
scdFacProd = int(myInput[3])
trdFacCost = int(myInput[4])
trdFacProd = int(myInput[5])
myFac = [{'name': 'fstFac', 'cost': 0, 'produce': fstFacProd}]
remainingFac = [
{'name':'scdFac', 'cost': scdFacCost, 'produce':scdFacProd},
{'name':'trdFac', 'cost': trdFacCost, 'produce':trdFacProd}]
potentialFac = []
fac = len(myFac)
cookies = 0
def checkFac(): # Making Functions
for f in remainingFac:
if f['cost'] <= cookies:
potentialFac.append(f)
def buyFac(num, rday):
global cookies
print("buyFac, remained day =", rday, "; cookie = ", cookies)
if num == 1:
cost = potentialFac[0]['cost']
prod = potentialFac[0]['produce']
if prod * rday >= cost:
remainingFac.remove(potentialFac[0])
myFac.append(potentialFac[0])
cookies = cookies - cost
print("I bought another factory that produces ", prod, " cookies per day and costed ", cost, " cookies.")
if num == 2:
cost0 = potentialFac[0]['cost']
prod0 = potentialFac[0]['produce']
cost1 = potentialFac[1]['cost']
prod1 = potentialFac[1]['produce']
sum0 = prod0 * rday - cost0
sum1 = prod1 * rday - cost1
if sum0 >= sum1:
remainingFac.remove(potentialFac[0])
myFac.append(potentialFac[0])
cookies = cookies - cost0
print("I bought another factory that produces ", prod0, " cookies per day and costed ", cost0, " cookies.")
else:
myFac.append(potentialFac[1])
remainingFac.remove(potentialFac[1])
cookies = cookies - cost1
print("I bought another factory that produces ", prod1, " cookies per day and costed ", cost1, " cookies.")
for x in range(day): # The work stuff
for i in range(len(myFac)):
cookies = cookies + myFac[i]['produce']
lenMyFac = len(myFac)
print("\n" + "day: ", x, "; my factories: ", lenMyFac, "; remain fac: ", len(remainingFac), "; cookies: ", cookies)
potentialFac = []
checkFac()
numPotentialFac = len(potentialFac)
if numPotentialFac > 0:
buyFac(numPotentialFac, day - x)
lenMyFac = len(myFac)
print("day = ", x, "; I have ", lenMyFac, "factories");
print(str(cookies)) # Saving the number
cookiesout.write(str(cookies))
cookiesin.close()
cookiesout.close()
Second Solution
The second solution:
# Filenames--------------------------------------------
in_filename = 'cookiesin.txt'
out_filename = 'cookiesout.txt'
# Set up input()---------------------------------------
in_file = open(in_filename, 'r')
def input():
return in_file.readline()
out_file = open(out_filename, 'w')
# Set up function remainder()-----------------------------
def remainder(days_need, factory, rate):
if factory%rate:
days_need += 1
return days_need
# Code-------------------------------------------------
lines = []
for line in range(3):
lines.append(input().strip().split())
D, C0 = map(int, lines[0])
P1, C1 = map(int, lines[1])
P2, C2 = map(int, lines[2])
total_F0 = D*C0
plan_one = total_F0
dn = P1//C0
dn = remainder(dn, P1, C0)
dr = D-dn
plan_two_1 = total_F0 + dr*C1 - P1
dn = P2//C0
dn = remainder(dn, P2, C0)
dr = D-dn
plan_two_2 = total_F0 + dr*C2 - P2
dn1 = P1//C0
dn1 = remainder(dn1, P1, C0)
r = dn1*C0 - P1
dn2 = (P2-r)//(C0+C1)
dn2 = remainder(dn2, P2-r, C0+C1)
plan_three_1 = total_F0 + (D-dn1)*C1 + (D-dn1-dn2)*C2 - P1 - P2
dn2 = P2//C0
dn2 = remainder(dn2, P2, C0)
r = dn2*C0 - P2
dn1 = (P1-r)//(C0+C2)
dn1 = remainder(dn1, P1-r, C0+C2)
plan_three_2 = total_F0 + (D-dn2)*C2 + (D-dn2-dn1)*C1 - P1 - P2
maximum = max(plan_one, plan_two_1, plan_two_2, plan_three_1, plan_three_2)
out_file.write(str(maximum))
# Close files------------------------------------------
in_file.close()
out_file.close()
Third Solution
Our third solution:
#variables
import sys
sys.setrecursionlimit(1000000000)
D = "number of days"
C0 = "production rate of factory 0"
P1 = "cost of factory 1"
C1 = "production rate of factory 1"
P2 = "cost of factory 2"
C2 = "production rate of factory 2"
answer = "my answer"
#open files
input_file = open("cookiesin.txt", "r")
output_file = open("cookiesout.txt", "w")
#input
input_line = input_file.readline().strip()
D, C0 = map(int, input_line.split())
input_line = input_file.readline().strip()
P1, C1 = map(int, input_line.split())
input_line = input_file.readline().strip()
P2, C2 = map(int, input_line.split())
#code
#not buying anything
noBuy = C0 * D
#only buy fac 2.
money = 0
cookies = C0
day = D
while money < P2:
day -= 1
money += cookies
money -= P2
cookies += C2
twoBuy = day * cookies + money
#only buy fac 1.
money = 0
cookies = C0
day = D
while money < P1:
day -= 1
money += cookies
money -= P1
cookies += C1
oneBuy = day * cookies + money
#buy fac 2 first.
money = 0
cookies = C0
day = D
while money < P2:
day -= 1
money += cookies
money -= P2
cookies += C2
while money < P1:
day -= 1
money += cookies
money -= P1
cookies += C1
twoFirst = day * cookies + money
#buy fac 1 first.
money = 0
cookies = C0
day = D
while money < P1:
day -= 1
money += cookies
money -= P1
cookies += C1
while money < P2:
day -= 1
money += cookies
money -= P2
cookies += C2
oneFirst = day * cookies + money
answer = max(oneBuy, twoBuy, twoFirst, oneFirst, noBuy)
# Write the answer
output_file.write("\n" % (answer))
# close the files
input_file.close()
output_file.close()
AIO 2020 Problem 3
We came up with two solutions.
ghostin = open("ghostin.txt","r") # Opening Files
ghostout = open("ghostout.txt","w")
line = ghostin.readline().strip()
numGhost, travelMeter = map(int, line.split())
ghosts = [list(map(int, ghostin.readline().strip().split())) for _ in range(numGhost)]
ghostFlag = [1] * numGhost
maxTotal = 0
for np in range(numGhost):
if ghostFlag[np] == 0:
continue;
gPosition = ghosts[np][0]
gTime = ghosts[np][1]
ghostFlag[np] = 0
total = 1
## print("ghost: ", np, "; position: ", gPosition, "; time: ", gTime)
for nn in range(np + 1, numGhost, 1):
if ghostFlag[nn] == 0:
continue;
if ghosts[nn][0] == gPosition and ghosts[nn][1] == gTime:
total = total + 1
ghostFlag[nn] = 0
continue;
## print("candidate ghost: ", nn, "; position: ", ghostPosition[nn], "; time: ", ghostTime[nn])
if (ghosts[nn][0] - gPosition) ==( (ghosts[nn][1] - gTime)/travelMeter):
total = total + 1
ghostFlag[nn] = 0
## print("match: ", total - 1)
if total > maxTotal: # Finding the largest list
maxTotal = total
ghostout.write(str(maxTotal) + "\n") #Writing out answer
#print(maxTotal)
ghostin.close()
ghostout.close()
Second Solution
Our second solution:
# Filenames--------------------------------------------
in_file = open("ghostin.txt", "r")
out_file = open("ghostout.txt", "w")
# Code-------------------------------------------------
line = in_file.readline().strip()
N, K = map(int, line.split())
ghosts = [list(map(int, in_file.readline().strip().split())) for _ in range(N)]
print(ghosts)
start_times = {}
for ghost in ghosts:
X, T = ghost
S = T-(X*K)
if S not in start_times:
start_times[S] = 1
else:
start_times[S] += 1
out_file.write(str(max(start_times.values())))
# Close files------------------------------------------
in_file.close()
out_file.close()
AIO 2020 Problem Four
We have three solutions
#!/usr/bin/env python
import sys
sys.setrecursionlimit(1000000000)
# sizes stores the size of each bin in sizes dictionary.
sizes = {}
answer = None
# open the input and output files.
input_file = open("tennisin.txt", "r")
output_file = open("tennisout.txt", "w")
# read the value of bins and balls
myInput = input_file.read().split()
bins = int(myInput[0])
balls = int(myInput[1])
myInput.pop(0)
myInput.pop(0)
# read the size of each bin into a dictionary (Thank you for showing me this.)
bin = 1
for i in range(bins):
sizes[bin] = int(myInput[0])
myInput.pop(0)
bin += 1
# code
bin = 1
while balls > 0:
if sizes[bin] > 0:
sizes[bin] -= 1
balls = balls - 1
bin += 1
if bin > bins:
bin = 1
answer = bin - 1
print(answer)
# write the answer to the output file.
output_file.write(str(answer) + "\n")
# close the input/output files.
input_file.close()
output_file.close()
And
We also have:
tennisin = open("tennisin.txt","r")
tennisout = open("tennisout.txt","w")
line = tennisin.readline().strip()
numBasket, numBalls = map(int, line.split())
#print(numBasket, numBalls)
baskets = list(map(int, tennisin.readline().strip().split()))
lTuple = []
N = len(baskets)
[list(map(lTuple.append, [(baskets[i], i)])) for i in range(N)]
#print(lTuple)
def sortFunc(num):
return num[0]
lTuple.sort(key = sortFunc)
print(lTuple)
ballRem = numBalls
cTop = 0
answer = -1
for index in range(N):
if cTop == lTuple[index][0]:
continue
basketRem = N - index
capacity = (lTuple[index][0] - cTop) * basketRem
if capacity < ballRem:
ballRem = ballRem - capacity
cTop = lTuple[index][0]
elif capacity == ballRem:
answer = lTuple[N-1][1] + 1
break
else:
lastBasket = ballRem % basketRem
if lastBasket == 0:
answer = lTuple[N-1][1] + 1
break
else:
realIndex = lastBasket - 1 + index
answer = lTuple[realIndex][1] + 1
break
print(answer)
tennisout.write(str(answer) + "\n")
tennisin.close()
tennisout.close()
Lastly:
Our third one:
in_file = open("tennisin.txt", "r")
out_file = open("tennisout.txt", "w")
# Code-------------------------------------------------
line = in_file.readline().strip()
B, N = map(int, line.split())
bins = list(map(int, in_file.readline().strip().split()))
i = 0
B_next = B
result = 0
while N > 0:
avg = max(N//B_next, 1)
for i in range(B):
if bins[i] > 0:
s = min(bins[i], avg)
bins[i] -= s
N -= s
if bins[i] == 0:
B_next -= 1
if N == 0:
result = i + 1
break
out_file.write(str(result))
# Close files------------------------------------------
in_file.close()
out_file.close()