Skip to content

Commit

Permalink
Added 2011 Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent 12efc74 commit 3f7bace
Show file tree
Hide file tree
Showing 13 changed files with 731 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 2011/Junior/J1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# CCC 2011 Junior 1: Which Alien?
# written by C. Robart
# March 2011

# simple ifs

antenna = input("How many antennas?\n")
eyes = input("How many eyes?\n")

if antenna >= 3 and eyes <= 4:
print "TroyMartian"
if antenna <= 6 and eyes >= 2:
print "VladSaturnian"
if antenna <= 2 and eyes <= 3:
print "GraemeMercuian"
18 changes: 18 additions & 0 deletions 2011/Junior/J2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# CCC 2011 Junior 2: Who has seen the wind
# written by C. Robart
# March 2011

# simple loop and if

h = input()
M = input()

t = 1
A = -6*t*t*t*t + h*t*t*t + 2*t*t + t
while t < M and A > 0:
t = t + 1
A = -6*t*t*t*t + h*t*t*t + 2*t*t + t
if A > 0:
print "The balloon does not touch ground in the given time."
else:
print "The balloon touches ground at hour:\n" , t
16 changes: 16 additions & 0 deletions 2011/Junior/J3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# CCC 2011 Junior 3: Sumac Sequences
# written by C. Robart
# March 2011

# a single loop

a = input()
b = input()

count = 2
while a >= b and a >= 0 and b >= 0:
count = count + 1
c = a - b
a = b
b = c
print count
81 changes: 81 additions & 0 deletions 2011/Junior/J4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# CCC 2011 Junior 4: Boring Business
# written by C. Robart
# March 2011

# dictionary (or 2d arrays)
# relatively straight forward 2d movement

# create the original well plan
wellPlan = {}
for row in range(-1,-201,-1):
for col in range (-200,201):
wellPlan[(row,col)] = False
wellPlan[(-1,0)] = True
wellPlan[(-2,0)] = True
wellPlan[(-3,0)] = True
wellPlan[(-3,1)] = True
wellPlan[(-3,2)] = True
wellPlan[(-3,3)] = True
wellPlan[(-4,3)] = True
wellPlan[(-5,3)] = True
wellPlan[(-5,4)] = True
wellPlan[(-5,5)] = True
wellPlan[(-4,5)] = True
wellPlan[(-3,5)] = True
wellPlan[(-3,6)] = True
wellPlan[(-3,7)] = True
wellPlan[(-4,7)] = True
wellPlan[(-5,7)] = True
wellPlan[(-6,7)] = True
wellPlan[(-7,7)] = True
wellPlan[(-7,6)] = True
wellPlan[(-7,5)] = True
wellPlan[(-7,4)] = True
wellPlan[(-7,3)] = True
wellPlan[(-7,2)] = True
wellPlan[(-7,1)] = True
wellPlan[(-7,0)] = True
wellPlan[(-7,-1)] = True
wellPlan[(-6,-1)] = True
wellPlan[(-5,-1)] = True

# read instructions
line = raw_input()
direction = line[0:1]
distance = eval(line[2:])

ok = True
row = -5
col = -1

# do the drilling!
while ok and direction != "q":
dr = 0
dc = 0
if direction == "l":
dc = -1
elif direction == "u":
dr = 1
elif direction == "r":
dc = 1
elif direction == "d":
dr = -1

while distance > 0:
row = row + dr
col = col + dc
if wellPlan[(row,col)]:
ok = False
else:
wellPlan[(row,col)] = True
distance = distance - 1

if ok:
print col, row, "safe"
else:
print col, row, "DANGER"

line = raw_input()
direction = line[0:1]
distance = eval(line[2:])

67 changes: 67 additions & 0 deletions 2011/Junior/J5_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# CCC 2011 Junior 5: Unfriend
# written by C. Robart
# March 2011

# this is a process of determining which sets are possible
# to remove. Each set can be considered an integer. Since max N = 6,
# the max number is 12345. (Or for n = 4 the max = 123)
# Each number from 0 to the max will be considered:
# - all numbers with digits >= N are discarded. (eg 57)
# - all numbers with digits which do not INCREASE are discarded (eg 223)
# - all numbers which do not contain the number of a person invited
# by a person who is in the number is discarded.
# (eg if 3 invited 1, then 23 is discarded)
#
# A count of all the numbers remaining is the answer.

# this chacks a given number i to ensure that it
# has no digits equal or greater than N and the digits
# all increase in value left to right
def goodNumber(N, x):
last = N
ok = True
while x > 0 and ok:
digit = x % 10
ok = digit < last
last = digit
x = x // 10
return ok

# this checks that all the digits of a number i
# are related thru list l. For example if 4 invites 2, then any number
# with a 4 digit must also have a 2 digit.
def goodCombo(N,x,l):

# build the list of the digitis of x
digits = []
while x > 0:
digits.append(x % 10)
x = x // 10

# for each digit of x, see if it "invited" anyone,
# if so, that invitee must be in the list of digits.
for d in digits:
for x in range(0,N-1):
if d == l[x]:
if (x+1) not in digits:
return False
return True

# input the info
N = input()
l = []
for i in range(1,N):
l.append(input())

# count the valid numbers (or sets)
max = eval("12345"[0:N-1])
count = 1
for i in range(1, max+1):
if goodNumber(N, i) and goodCombo(N, i, l):
count = count + 1
print count





53 changes: 53 additions & 0 deletions 2011/Junior/J5_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# CCC 2011 Junior 5: Unfriend
# algorithm by Sumudu Fernando
# March 2011

# Let
# ways(x) = the number of ways to unfriend the people under x.
#
# To calculate ways(x), note that x could either remove y (that's 1 way)
# or a set of people under y (that's ways(y)), for each y that x invited.
# So that means ways(x) = the product of (1+ways(y)) for each y that x
# invited.

# lets me work through an example J4.3.in
# 1 was invited by 4
# 2 was invited by 4
# 3 was invited by 5
# 4 was invited by 6
# 5 was invited by 6

# building the ways array from the ground up:
# ways(1) = 1 (he can unfriend none)
# ways(2) = 1 (he can unfriend none)
# ways(3) = 1 (he can unfriend none)
# ways(4) = (1+ways(1)*(1+ways(2)) = 4
# a closer look: 4 can unfriend (1 or none) * (2 or none)
# of if you like: 12, 1, 2, none
# ways(5) = (1+ways(3) = 2
# a closer look: 5 can unfriend 3 or none
# ways(6) = (1+ways(4)*(1+ways(5)) = 15
# a closer look: 6 can unfriend 4, ie: 124 or
# those under 4: 12, 1, 2, none
# = (124, 12, 1, 2, none)
# * unfriend 5, ie: 35 or
# those under 5: 3, none
# = (35, 3, none)
# and if you * those two sets you get:
# (12345,1235,135,235,35,1234,123,13,23,3,124,12,1,2,none)
#
#
# after that, coding is a joke.
#

# input the info
N = input()
ways = [1,1,1,1,1,1,1]
for i in range(1,N):
y = input()
ways[y] = ways[y] * (1 + ways[i])

print ways[N]



50 changes: 50 additions & 0 deletions 2011/Junior/J5_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# CCC 2011 Junior 5: Unfriend
# algorithm Daniel Cressman
#
# March 2011
#
# this is a slight variation which allows j<=i
# making it a more general solution, but somewhat unnecessary
# given the strict definition of the problem.
#
# To calculate ways(x), note that x could either remove y (that's 1 way)
# or a set of people under y (that's ways(y)), for each y that x invited.
# So that means ways(x) = the product of (1+ways(y)) for each y that x
# invited.
#
# Lets work thru an example input: 6,4,4,5,6,6 (see tree below)
# 6
# so n = 6 4 5
# and friends = [4,4,5,6,6] 1 2 3
#
# so ways(1) = 1 (1 invites no one)
# ways(2) = 1 (2 invites no one)
# ways(3) = 1 (3 invites no one)
# ways(4) = (1+ways(1)) * (1+ways(2)) = (1+1)*(1+1) = 4
# ways(5) = (1+ways(3)) = (1+1) = 2
# ways(6) = (1+ways(4)) * (1+ways(5)) = (1+4)*(1+2) = 15
#
#
# this method is as described above, except:
# the friends list and n are passed because they are not global
# y is the index of the list and goes 0,1,2,...n-2
# but then when it is passed to ways(), it is needs to be 1,2,3...n-1
def ways(friends,n,x):
answer = 1
for y in range(n-1):
if friends[y] == x: # read: "x invited y"
# except y is really y+1 because
# indexes start at 0, not 1
answer = answer * (1 + ways(friends,n,y+1))
return answer

# input the info
n = input()
friends = []
for i in range(1, n):
friends.append (input())

print ways(friends,n,n)



22 changes: 22 additions & 0 deletions 2011/Senior/S1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# CCC 2011 Senior 1: English or French
# written by C. Robart
# March 2011

# straight forward file handling with
# simple character counting in strings

file = open("s1.5.in", 'r')
lines = file.readlines(100000)
countS = 0
countT =0
for line in lines:
for i in range(0, len(line)):
if line[i] == "s" or line[i] == "S":
countS = countS + 1
elif line[i] == "t" or line[i] == "T":
countT = countT + 1
if countT > countS:
print "English"
else:
print "French"
file.close()
25 changes: 25 additions & 0 deletions 2011/Senior/S2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# CCC 2011 Senior 2: Multiple Choice
# written by C. Robart
# March 2011

# straight forward file handling with
# simple list (1D array) comparision

# file input
file = open("s2.5.in", 'r')
N = eval(file.readline())
studentResponses = []
for i in range (0, N):
studentResponses.append(file.readline())
correctAnswers = []
for i in range (0, N):
correctAnswers.append(file.readline())

# mark test
score = 0
for i in range (0, N):
if studentResponses[i] == correctAnswers[i]:
score = score + 1
print score
file.close()

Loading

0 comments on commit 3f7bace

Please sign in to comment.