-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 2015 Solutions, Updated README
Added the solutions for 2015 in Python, updated the README to credit Milliken Mills’ site.
- Loading branch information
Showing
13 changed files
with
726 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# CCC 2015 Junior 1: Special Day | ||
# | ||
# Straight forward else if structure | ||
# | ||
|
||
file = open("j1.1.in", "r") | ||
month = int(file.readline()) | ||
day = int(file.readline()) | ||
|
||
if month == 2 and day == 18: | ||
print "Special" | ||
elif month < 2 or (month == 2 and day < 18): | ||
print "Before" | ||
else: | ||
print "After" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# CCC 2015 Junior 2: Happy or Sad | ||
# | ||
# Strings and counting | ||
# | ||
|
||
file = open("j2.8.in", "r") | ||
line = file.readline() | ||
|
||
# python makes this very easy with the count() function | ||
##happy = line.count(":-)") | ||
##sad = line.count (":-(") | ||
|
||
# with other languages, find the happy and sad substrings | ||
# with something like the following | ||
happy = 0 | ||
sad = 0 | ||
for i in range(len(line)-2): | ||
if line[i:i+3] == ":-)": | ||
happy = happy + 1 | ||
elif line[i:i+3] == ":-(": | ||
sad = sad + 1 | ||
|
||
if happy == 0 and sad == 0: | ||
print "none" | ||
elif happy == sad: | ||
print "unsure" | ||
elif happy > sad: | ||
print "happy" | ||
else: | ||
print "sad" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# CCC 2015 Junior 3: Rovarspraket | ||
# | ||
# Strings (or arrays) will do this one | ||
# though I suppose a monster if would work too :-) | ||
# | ||
|
||
file = open("j3.5.in", "r") | ||
word = file.readline() | ||
|
||
consonant = "bcdfghjklmnpqrstvwxyz" | ||
closestVowel = "aaeeeiiiiooooouuuuuuu" | ||
nextConsonant = "cdfghjklmnpqrstvwxyzz" | ||
|
||
newWord = "" | ||
for i in range(len(word)): | ||
j = consonant.find (word[i]) | ||
newWord = newWord + word[i] # the orginal letter is always copied over | ||
if j > -1: # if it's not a vowel add the closest vowel and | ||
# next consonant | ||
newWord = newWord + closestVowel [j] + nextConsonant[j] | ||
|
||
print newWord |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# CCC 2015 Junior 4: Wait Time | ||
# | ||
# Consider this a 2D array problem, or an array of records | ||
# | ||
# For this I use and array of elements called "database" | ||
# Each element is itself an array of 3 numbers: | ||
# the friend #, time of message, and the total wait time for this friend | ||
# | ||
# I need it organized like this so it can be sorted by friend number at the end. | ||
# | ||
# Also the time of message will be set to -1 when answered, | ||
# so at the end I can detect if there are unanswered messages. | ||
# | ||
# eg [12,6,2] means friend 12 has made a call at time 6 (its not replied to so far) | ||
# and he/she has a wait time of 2 from previous message(s) | ||
|
||
file = open("j4.3.in", "r") | ||
m = int(file.readline()) | ||
|
||
database = [] | ||
time = -1 | ||
for i in range(m): | ||
line = file.readline().split() | ||
letter = line[0] | ||
friend = int(line[1]) | ||
|
||
if letter == "W": | ||
time = time + friend - 1 # in this case friend is really a time | ||
# the -1 is there because +1 will be added next time | ||
else: | ||
time = time + 1 | ||
|
||
if letter != "W": | ||
# search database for this friend | ||
found = False | ||
k = 0 | ||
while not found and k < len(database): | ||
if friend == database[k][0]: | ||
found = True | ||
else: | ||
k = k + 1 | ||
|
||
if letter == "R": | ||
#if found, put inthe time of the message, otherwise add a new element to the database | ||
if found: | ||
database[k][1] = time | ||
else: | ||
database.append ([friend, time, 0]) | ||
elif letter == "S": | ||
# it will be found, so no check is done | ||
database[k][2] = database[k][2] + (time - database[k][1]) | ||
database[k][1] = -1 | ||
|
||
# Before printing the results the database must be sorted by friend number | ||
# A simple bubble sort is used | ||
for i in range(len(database)-1): | ||
for j in range(i+1, len(database)): | ||
if database[i][0] > database[j][0]: | ||
temp = database[i] | ||
database[i] = database[j] | ||
database[j] = temp | ||
|
||
# Print results | ||
for i in range(len(database)): | ||
if database[i][1] == -1: | ||
print database[i][0], database[i][2] | ||
else: | ||
print database[i][0], -1 | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# CCC 2015 Junior 5: Pie Day | ||
# | ||
# This is a recursion problem (or possibliy a Dynamic Programming problem) | ||
# | ||
# This is the recursive solution. | ||
# | ||
# Given n (number of pieces of pie) and k (number of people) and the minimum | ||
# number of pieces that can be taken, the number of ways the pie can be destributed | ||
# is: | ||
# 1 if n = k or k = 1 | ||
# otherwise it is the sum of the ways of (n-i, k-1, i) where i goes from | ||
# min to n/k (inclusive) | ||
# | ||
# an example might help: pi(9,4,1) | ||
# n = 9, k = 4 and min = 1 | ||
# for this the answer is calculated as follows: | ||
# the first person could take 1 piece leaving us the task of finding the ways | ||
# with now 8 pieces (9-1), 3 people and a min of 1 (= pi(8,3,1)) | ||
# the first person could take 2 pieces leaving us the task of finding the ways | ||
# with now 7 pieces (9-2), 3 people and a min of 2 (= pi(7,3,2)) | ||
# the first person could NOT take 3 pieces (or more)_because that would leave 6 | ||
# pieces for 3 people and since the first person took 3, they | ||
# would have to take at least 3 themselves and that's 9 not 6 pieces! | ||
# in general, the most the first person person can take is | ||
# n/k (eg 9/4 = 2) | ||
# | ||
# The recursion as described above works fine, BUT is too slow for the | ||
# later data sets, SO to avoid calculating the same thing twice | ||
# there is a "visited" array. This is a large 3D array keeping track of all | ||
# previous results. Half the running time (and code) is devoted to creating it | ||
# and initializing it to 0. In the recursion, if the visited array knows the answer, | ||
# use it and don't recurse, otherwise calculate and store the answer in visited. | ||
# | ||
# the worst case, data set 10, takes about 3 sec on my old | ||
# XP Pentium 4 CPU, 3.2Ghz, 1G RAM machine, using Python. | ||
|
||
visited = [] | ||
|
||
def pi(n,k,min): | ||
if visited [n][k][min] == 0: | ||
if n == k: | ||
visited[n][k][min] = 1 | ||
elif k == 1: | ||
visited[n][k][min] = 1 | ||
else: | ||
t = 0 | ||
for i in range (min, (n / k)+1): | ||
t = t + pi(n-i, k-1, i) | ||
visited[n][k][min] = t | ||
return visited[n][k][min] | ||
|
||
|
||
file = open("j5.10.in", "r") | ||
n = int(file.readline()) | ||
k = int(file.readline()) | ||
|
||
for i in range(n+1): | ||
x = [] | ||
for j in range(k+1): | ||
t = [] | ||
for kk in range(n+1): | ||
t.append (0) | ||
x.append(t) | ||
visited.append(x) | ||
|
||
print pi(n,k,1) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# CCC 2015 Senior 1: Zero That Out | ||
# | ||
# A list processing problem (using a stack) | ||
# | ||
# Python's append() - add to the end of the list | ||
# and pop() - remove the last item | ||
# make this an easy problem | ||
|
||
|
||
file = open("s1.5.in", "r") | ||
k = int(file.readline()) | ||
|
||
list = [] | ||
for i in range(k): | ||
n = int(file.readline()) | ||
if n > 0: | ||
list.append(n) | ||
else: | ||
list.pop() | ||
|
||
sum = 0 | ||
for i in range(len(list)): | ||
sum = sum + list[i] | ||
|
||
print sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# CCC 2015 Senior 2: Jerseys | ||
# | ||
# The problem is simple. | ||
# 1. store the jersy sizes. (no tricks required) | ||
# 2. read reach request: if its a small -> satisfied | ||
# if sizes match -> satisfied | ||
# if its a M request for a L -> satisfied | ||
# Once a jersy has been given out, mark it as "T" (taken) | ||
# | ||
# With some tweaking of the file input and converting a string to an integer using | ||
# int(s,10) I was able to get the time for the last data set under 4 seconds | ||
# with my old machine (XP Pentium 4 CPU, 3.2Ghz, 1G RAM). | ||
# | ||
|
||
|
||
file = open("s2.6.in", "r") | ||
j = int(file.readline()) | ||
a = int(file.readline()) | ||
|
||
size = [] | ||
for i in range(j): | ||
size.append (file.readline().strip()) | ||
|
||
requestsSatisfied = 0 | ||
for line in file: | ||
number = int(line[2:],10) - 1 | ||
line = line[0] | ||
if size[number] != 'T': | ||
if line == 'S' or \ | ||
line == size[number] or \ | ||
(line == 'M' and size[number] == 'L'): | ||
requestsSatisfied = requestsSatisfied + 1 | ||
size[number] = 'T' | ||
|
||
print requestsSatisfied |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# CCC 2015 Senior 3: Gates | ||
# | ||
# This solution is by Matthew Lee of Galt C.V.I. | ||
# | ||
# Here is his explaination: | ||
# The way it works is that I create an array for the total amount of open spots, | ||
# Then as each airplane tries to land, | ||
# it starts off at its highest possible spot number. | ||
# If the number is not 0, it jumps back the amount of places that number says and | ||
# then adds 1 to that spot. If spot 10 has been accessed 8 times, | ||
# it is known that the previous 8 have been taken. | ||
|
||
data = open("s3.10.in") | ||
|
||
ports = int(data.readline()) | ||
numPlanes = int(data.readline()) | ||
|
||
planes = [] | ||
|
||
total = 0 | ||
|
||
for i in range(ports + 1): | ||
planes.append(0) | ||
|
||
i = 0 | ||
keepGoing = True | ||
while i < numPlanes and keepGoing: | ||
cPlane = int(data.readline()) | ||
while cPlane > 0 and planes[cPlane] > 0: | ||
t = planes[cPlane] | ||
planes[cPlane] = planes[cPlane] + 1 | ||
cPlane = cPlane - t | ||
if cPlane <= 0: | ||
keepGoing = False | ||
else: | ||
planes[cPlane] = 1 | ||
total = total + 1 | ||
i = i + 1 | ||
|
||
print total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# CCC 2015 Senior 3: Gates | ||
# | ||
# This solution is by Weiwei Zhong | ||
# | ||
# This uses a disjoint-set Data Structure | ||
# | ||
# see http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=disjointDataStructure | ||
# for a good exlaination of the data structure | ||
# | ||
# to help explain this, look at the following input data set: | ||
# 6 7 6 1 4 4 6 6 3 (6 gates and 7 planes.) | ||
# | ||
# root is initialized to [0, 0, 0, 0, 0, 0, 0] (there are more 0's, but forget them) | ||
# | ||
# After reading the number of gates, root is [0, 1, 2, 3, 4, 5, 6] | ||
# think of each number being its own set. | ||
# | ||
# The first plane goes in gate 6 and its found in its own set, so "take it", | ||
# by merging it with the set below it. | ||
# so set 6 and 5 are merged and root becomes [0, 1, 2, 3, 4, 5, 5] | ||
# The way to "read" this is that set 6 doesn't exist any more and if part of the "5" set. | ||
# a set is identified with the lowest number in the set. | ||
# | ||
# The same thing happens with the 1 and the 4, they go in their respective gates | ||
# and those gates are merged with the sets below them. | ||
# this makes root [0, 0, 2, 3, 3, 5, 5] | ||
# | ||
# The following 4 finds that 4 is part of the 3 set. We haven't had that before. | ||
# In this case merge changes root to become [0, 0, 2, 2, 3, 5, 5] | ||
# (now stop a minute and look at that: merge sets root[find(x)] = find(y) | ||
# or in our case root[find(3)] = find[2] | ||
# which means root[3] = 2 | ||
# A way to "read" this is that in root, if x = root[x] then gate x is available. | ||
# and now after 4 planes, only root 2 and 5 remain. Or if you prefer the world of sets | ||
# we have 3 sets (0,1), (2,3,4) and (5,6) | ||
# | ||
# With the next plane wanting 6 again we can see that gate 5 will be taken and we'll be | ||
# down to only two sets (0,1) and (2,3,4,5,6), the only "open" gate will be 2. | ||
# After the merge, root becomes [0, 0, 2, 2, 2, 2, 5] | ||
# How did that happen? Well it's complex due to the recursion happening in find, BUT | ||
# we can see that there is only one open gate now, 2, and that we have two sets. | ||
# | ||
# With the third palne wanting 6, the last open gate, 2 is taken and root becomes | ||
# [0, 0, 0, 2, 2, 2, 2] | ||
# which is a single set represented by 0, no gates left. | ||
# | ||
# The next plane request for gate 3 stops the program, and i = 6 is printed. | ||
# | ||
# I realize this is as clear as mud: Go to the website and read! :-) | ||
|
||
|
||
root = [0 for i in range(100001)] | ||
|
||
#find the set this index belongs to | ||
def find(x): | ||
if x!=root[x]: | ||
root[x] = find(root[x]) | ||
return root[x] | ||
|
||
#merge index x into y | ||
def merge(x,y): | ||
root[find(x)] = find(y) | ||
|
||
file = open("s3.10.in") | ||
numGates = int(file.readline()) | ||
numPlanes = int(file.readline()) | ||
for i in range(1,numGates+1): | ||
root[i] = i | ||
i = 0 | ||
keepgoing = True | ||
while i < numPlanes and keepgoing: | ||
gateWanted = int(file.readline()) | ||
gateWantedRoot = find(gateWanted) | ||
if gateWantedRoot == 0: | ||
print i | ||
keepgoing = False | ||
merge(gateWantedRoot,gateWantedRoot-1) | ||
i = i + 1 | ||
if keepgoing: | ||
print numPlanes |
Oops, something went wrong.