Skip to content

Commit

Permalink
Added 2014 Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent 1077661 commit 14103ce
Show file tree
Hide file tree
Showing 11 changed files with 586 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 2014/Junior/J1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# CCC 2014 Junior 1: Triangle Times
#
# Straight forward else if structure
#

file = open("j1.6.in", "r")
a = int(file.readline())
b = int(file.readline())
c = int(file.readline())
if a + b + c != 180:
print "Error"
elif a == 60 and b == 60 and c == 60:
print "Equilateral"
elif a == b or a== c or b == c:
print "Isosceles"
else:
print "Scalene"
23 changes: 23 additions & 0 deletions 2014/Junior/J2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# CCC 2014 Junior 2: Vote Count
#
# straight forward string processing
#

file = open ("j2.4b.in", "r")
v = int(file.readline())
votes = file.readline()

# in python the following loop could be replaced with
# acount = votes.count("A")
acount = 0
for letter in votes:
if letter == "A":
acount = acount + 1

bcount = v - acount
if acount > bcount:
print "A"
elif bcount > acount:
print "B"
else:
print "Tie"
22 changes: 22 additions & 0 deletions 2014/Junior/J3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# CCC 2014 Junior 3: Double Dice
#
# straight forward loop and counting
#

file = open ("j3.5.in", "r")
n = int(file.readline())

antonia = 100
david = 100

for x in range(n):
roll = file.readline().split()
a = int(roll[0])
d = int(roll[1])
if a < d:
antonia = antonia - d
elif d < a:
david = david - a

print antonia
print david
32 changes: 32 additions & 0 deletions 2014/Junior/J4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# CCC 2014 Junior 4: Party Invitation
#
# relatively straight forward 1D array processing
# (element removal and modulo arithmetic)
#

file = open ("j4.5.in", "r")
k = int(file.readline())

#create friends array [1...k]
friends = []
for i in range (k):
friends.append(i+1)

m = int(file.readline())

for round in range(m):
r = int(file.readline())

# eliminate every rth friend
newfriends = []
for i in range(len(friends)):
if (i+1) % r != 0:
newfriends.append (friends[i])

# copy the new friends back into the old one
friends = []
for f in newfriends:
friends.append(f)

for f in friends:
print f
36 changes: 36 additions & 0 deletions 2014/Junior/J5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# CCC Junior 5: Assigning Partners
#
# Array inverse checking (The idea is for all positions i
# in array A, find the position of B[i] in A, call it j.
# Now A[i] should equal B[j].)
#
# The problem is made easy because the input lines
# are guaranteed to have N DIFFERENT names, so no checks
# for duplicates or missing names need occur and the second
# line has the SAME names as the first, so no checks needed
# for 'not found' conditions.
#

file = open ("j5.5b.in", "r")
n = int(file.readline())

first = file.readline().split()
second = file.readline().split()

# for each position i, it must be the case that
# first [i] = second[position in first of second[i]]
# (ie. a to b is also b to a)
# and that position in first of second[i] not = i
# (ie. a to a is not the case)

i = 0
state = "good"
while i < n and state == "good":
position = first.index(second[i])
if first[i] != second[position] or position == i:
state = "bad"
i = i + 1

print state


32 changes: 32 additions & 0 deletions 2014/Senior/S1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# CCC 2014 Senior 1: Party Invitation
#
# relatively straight forward 1D array processing
# (element removal and modulo arithmetic)
#

file = open ("s1.5.in", "r")
k = int(file.readline())

#create friends array [1...k]
friends = []
for i in range (k):
friends.append(i+1)

m = int(file.readline())

for round in range(m):
r = int(file.readline())

# eliminate every rth friend
newfriends = []
for i in range(len(friends)):
if (i+1) % r != 0:
newfriends.append (friends[i])

# copy the new friends back into the old one
friends = []
for f in newfriends:
friends.append(f)

for f in friends:
print f
36 changes: 36 additions & 0 deletions 2014/Senior/S2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# CCC Senior 2: Assigning Partners
#
# Array inverse checking (The idea is for all positions i
# in array A, find the position of B[i] in A, call it j.
# Now A[i] should equal B[j].)
#
# The problem is made easy because the input lines
# are guaranteed to have N DIFFERENT names, so no checks
# for duplicates or missing names need occur and the second
# line has the SAME names as the first, so no checks needed
# for 'not found' conditions.
#

file = open ("s2.5b.in", "r")
n = int(file.readline())

first = file.readline().split()
second = file.readline().split()

# for each position i, it must be the case that
# first [i] = second[position in first of second[i]]
# (ie. a to b is also b to a)
# and that position in first of second[i] not = i
# (ie. a to a is not the case)

i = 0
state = "good"
while i < n and state == "good":
position = first.index(second[i])
if first[i] != second[position] or position == i:
state = "bad"
i = i + 1

print state


63 changes: 63 additions & 0 deletions 2014/Senior/S3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# CCC Senior 3: the Geneva Confection
#
# A Stack problem
#
# Test case 5 runs on my machine in 4 seconds :-)
# (a Pentium 4, 3.2 Ghz, 1GB RAM)
#
# I fine tuned the file input to shave off a second or 2.
# (File input seems to be the slowest part.)
#
# For the cars on the mountain, I just use an index (mtnCar)
# to keep track of where I am.
# (Nothing actually is removed from it.)
#
# For the branch, I use a list and literally add to or
# pop from it. (You can't just keep track of where you are,
# because sometimes things have to 'come off' literally.)
#

file = open ("s3.5.in", "r")
entirefile = file.readlines()
p = 0
t = int(entirefile[p])
p = p + 1

for test in range(t):
n = int(entirefile[p])
p = p + 1
mountain = []
for i in range(n):
mountain.append(int(entirefile[p]))
p = p + 1

branch = []
mtnCar = n - 1

nextCar = 1
state = "Y"

# Next car should be either on mountian or branch
# if not, move a car from mountian into branch

while state == "Y" and nextCar <= n:
# if the next car is on the mountain, take it off
if mtnCar >= 0 and nextCar == mountain[mtnCar]:
mtnCar = mtnCar - 1
nextCar = nextCar + 1
# if the nextcar is on the branch, take it off
elif len(branch) > 0 and nextCar == branch[len(branch) - 1]:
branch.pop(len(branch) - 1)
nextCar = nextCar + 1
# otherwise move a car from the mountain to the branch
# (if you can)
elif mtnCar >= 0:
branch.append(mountain[mtnCar])
mtnCar = mtnCar - 1
# otherwise you're done
else:
state = "N"

print state


88 changes: 88 additions & 0 deletions 2014/Senior/S4_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# CCC 2014 S4: Tinted Glass Window
#
# This code is by Calvin Liu of Glenforest Secondary School
# (Yikuan (Timothy) Li also suggested a similar approach.)
#
# This algorithm uses a "Sweep-Line".
# I will let Calvin explain:
# This performs coordinate compression on the Y-axis
# [that means only using the y coordinates of the rectangles]
# and runs a sweep line along the X-axis.
# Every rectangle is separated into two vertical line segments,
# an incoming edge from the left and an outgoing edge to the right.
# The value of a rectangle is added when the sweep line encounters
# the incoming edge and subtracted when it encounters the outgoing edge.
# The final answer accumulates through the process.
#
# Here is a trace of Calvin's program, with the example given with the question
# line= [-1000000000, [11, 11, 15, 1], [12, 12, 13, 1], [13, 8, 17, 2],
# [14, 8, 17, -2], [17, 8, 17, 1], [18, 8, 17, -1],
# [19, 12, 13, -1], [20, 11, 15, -1]]
#
# After removing duplicates and sorting:
# segy= [-1000000000, 8, 11, 12, 13, 15, 17]
#
# coordinate compression and hash table creation
# findy= {8: 1, 11: 2, 12: 3, 13: 4, 15: 5, 17: 6}
#
# initialize the yaxis (keeps track of the total tint)
# yaxis= [0, 0, 0, 0, 0, 0, 0, 0]
#
# in the main loop
# i= 1 yaxis= [0, 0, 1, 1, 1, 0, 0, 0]
# i= 2 yaxis= [0, 0, 1, 2, 1, 0, 0, 0]
# i= 3 yaxis= [0, 2, 3, 4, 3, 2, 0, 0]
# i= 4 j= 2 ans= 1
# i= 4 j= 3 ans= 2
# i= 4 j= 4 ans= 4
# i= 4 yaxis= [0, 0, 1, 2, 1, 0, 0, 0]
# i= 5 yaxis= [0, 1, 2, 3, 2, 1, 0, 0]
# i= 6 j= 3 ans= 5
# i= 6 yaxis= [0, 0, 1, 2, 1, 0, 0, 0]
# i= 7 yaxis= [0, 0, 1, 1, 1, 0, 0, 0]
# i= 8 yaxis= [0, 0, 0, 0, 0, 0, 0, 0]
#
# technically test data set 9 runs in 6.7 seconds and
# test data set 10 runs in 5.7 seconds on my old Pentium 4 with 3.2 GHZ CPU


file = open ("s4.15.in", "r")
n = int(file.readline())
t = int(file.readline())

#-1000000000 is small enough to be at index 0 after sorting
line = [-1000000000]
#-1000000000 is just a place holder
segy = [-1000000000]

for i in xrange(n):
x1,y1,x2,y2,val=map(int,file.readline().split())
line.append([x1,y1,y2,val]) #Incoming side
line.append([x2,y1,y2,-val]) #Outgoing side
segy.append(y1)
segy.append(y2)

line.sort()

#Remove duplicates and sort
segy = list(set(segy))
segy.sort()

#Coordinate Compression with a hash table
findy = {}
for i in xrange(1, len(segy)):
findy[segy[i]] = i

yaxis=[0 for i in xrange(len(segy) + 1)]

ans=0

#Sweep Line loop
for i in xrange(1, len(line)):
for j in xrange(1, len(segy)):
if yaxis[j] >= t:
ans += (segy[j + 1] - segy[j]) * (line[i][0] - line[i - 1][0])
for j in xrange(findy[line[i][1]], findy[line[i][2]]):
yaxis[j] += line[i][3]

print ans
Loading

0 comments on commit 14103ce

Please sign in to comment.