Skip to content

Commit

Permalink
Added 2008 Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent 2e11762 commit 6f41291
Show file tree
Hide file tree
Showing 13 changed files with 1,296 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2008/Junior/J1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
% CCC 2008
% J1: Body Mass Index
%
% BMI = w / (h*h)
%
% BMI > 25 = Overweight
% BMI 18.5 - 25 = normal weight
% BMI < 18.5 = Underweight

var BMI, weight, height : real

put "Enter weight: " ..
get weight
put "Enter height: " ..
get height

BMI := weight / (height * height)
if BMI > 25 then
put "Overweight"
elsif BMI < 18.5 then
put "Underweight"
else
put "Normal weight"
end if

36 changes: 36 additions & 0 deletions 2008/Junior/J2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
% CCC 2008
% J2: Do the Shuffle
%
% playlist starts at ABCDE
%
% button 1 moves first to last
% button 2 moves last to first
% button 3 switches 1 and 2
% button 4 output list
%
% this is a simple string assignment
%

var playlist : string (5)
var button, presses : int

playlist := "ABCDE"
loop
put "Button number: " ..
get button
put "Number of presses: " ..
get presses
exit when button = 4 and presses = 1
for i : 1 .. presses
if button = 1 then
playlist := playlist (2 .. 5) + playlist (1)
elsif button = 2 then
playlist := playlist (5) + playlist (1 .. 4)
else
playlist := playlist (2) + playlist (1) + playlist (3 .. 5)
end if
end for
end loop

put playlist (1) + " " + playlist (2) + " " + playlist (3) + " " +
playlist (4) + " " + playlist (5)
61 changes: 61 additions & 0 deletions 2008/Junior/J3.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
% CCC 2008
% J3: GPS
%
% given the grid
% ABCDEF
% GHIJKL
% MNOPQR
% STUVWX
% YZ -.enter
%
% given a phrase, determine the number
% of movements in the grid to type the
% phrase (no diagonals nor wrap around)
%
% start at A
%
% its a question of changing Letters to coordinates
% and then getting the distance between, so to speak.
%

var phrase : string (40)
var letter : char
var x : int
var movements, r, c, newr, newc : int

get phrase : *
movements := 0
r := 1
c := 1
for i : 1 .. length (phrase)
letter := phrase (i)

% if this div mod business to too complex
if letter >= 'A' and letter <= 'Z' then
x := ord (letter) - ord ('A') + 1
newr := (x - 1) div 6 + 1
newc := (x - 1) mod 6 + 1
elsif letter = ' ' then
newr := 5
newc := 3
elsif letter = '-' then
newr := 5
newc := 4
elsif letter = '.' then
newr := 5
newc := 5
end if
movements := movements + abs (newr - r) + abs (newc - c)
r := newr
c := newc
end for

% don't forget to go to the enter at (5, 6)
movements := movements + abs (newr - 5) + abs (newc - 6)

put movements





67 changes: 67 additions & 0 deletions 2008/Junior/J4.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
% CCC 2008
% J4: Prefix to postfix
%
% It occurs to me that the best way to do this
% is with recursion.
%
% this method takes an input prefix string s,
% and creates the postfix, stored in post1
% with a temporary string called rest
%
% generally the method is, if the postfix starts with + or -
% the first operand starts in the third letter
% the second operand is found from the rest of the string
% (after the first operand is taken out)
% then put the postfix together: "first second +"
%
% If the postfix starts with a number, that is the postfix.
%
% an example might help, starting with "+ - 1 2 3"
% 1. it would enter with s = "+ - 1 2 3"
% 2. it would enter again with s = "- 1 2 3"
% 3. it would enter again with "1 2 3" and leave with post1="1" and rest="2 3"
% (the "1" is the first part of "- 1 2 3")
% 4. it would enter again with "2 3" and leave with post1="2" and rest="3"
% (the "2" is the second part of "- 1 2 3")
% now 2 finishes with post1 = "1 2 -" and rest = "3"
% now the first half of 1 is done and second call is done:
% 5. it would enter with s = "3" and leave with post1 = "3" and rest = ""
% now 1 is done with post1 = "1 2 - 3 +"
%
% think about it enough and you'll get it :-)
%


var prefix : string
var post1, temp : string

procedure postfix (s : string, var post1 : string, var rest : string)
var first, second, temp1, temp2 : string
put "in:" + s
if s (1) = '+' then
postfix (s (3 .. *), first, temp1)
postfix (temp1, second, temp2)
post1 := first + " " + second + " +"
rest := temp2
elsif s (1) = '-' then
postfix (s (3 .. *), first, temp1)
postfix (temp1, second, temp2)
post1 := first + " " + second + " -"
rest := temp2
else
post1 := s (1)
if length (s) > 1 then
rest := s (3 .. *)
else
rest := ""
end if
end if
put "out:" + post1 + ":" + rest
end postfix

loop
get prefix : *
exit when prefix = "0"
postfix (prefix, post1, temp)
put post1
end loop
125 changes: 125 additions & 0 deletions 2008/Junior/J5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// CCC 2008
//
// J5: Nukit
//
// This is a Recursive approach
//
// in English:
// you win if there is at least one way to create a losing combo.
// you lose if there is no move, OR
// all possible moves lead to a winning combo
//
// this algorithm is a straight forward implementation of that.
//
// By the way, this is ALSO the solution to S5, data set 1 and 2.
// It fails on data 3 and 4 due to time concerns as the recursion
// is not very efficient.
// However it is easy and a simple Junior solution.
//

import java.awt.*;
import hsa.*;

public class CCC2008J5NukitRecursion
{

public static boolean canDoAABDD (int a, int b, int c, int d)
{
return a >= 2 && b >= 1 && d >= 2;
}


public static boolean canDoABCD (int a, int b, int c, int d)
{
return a >= 1 && b >= 1 && c >= 1 && d >= 1;
}


public static boolean canDoCCD (int a, int b, int c, int d)
{
return c >= 2 && d >= 1;
}


public static boolean canDoBBB (int a, int b, int c, int d)
{
return b >= 3;
}


public static boolean canDoAD (int a, int b, int c, int d)
{
return a >= 1 && d >= 1;
}


public static boolean canMove (int a, int b, int c, int d)
{
return canDoAABDD (a, b, c, d) || canDoABCD (a, b, c, d) ||
canDoCCD (a, b, c, d) || canDoBBB (a, b, c, d) || canDoAD (a, b, c, d);
}


// lose if you can't move or if all possible moves you can do lead to a winning combo
public static boolean losingCombo (int a, int b, int c, int d)
{
if (!canMove (a, b, c, d))
return true;
else
{
boolean temp = true;
if (canDoAABDD (a, b, c, d))
temp = temp && winningCombo (a - 2, b - 1, c, d - 2);
if (canDoABCD (a, b, c, d))
temp = temp && winningCombo (a - 1, b - 1, c - 1, d - 1);
if (canDoCCD (a, b, c, d))
temp = temp && winningCombo (a, b, c - 2, d - 1);
if (canDoBBB (a, b, c, d))
temp = temp && winningCombo (a, b - 3, c, d);
if (canDoAD (a, b, c, d))
temp = temp && winningCombo (a - 1, b, c, d - 1);
return temp;
}
}


// win if at least one rule creates a losing combo
public static boolean winningCombo (int a, int b, int c, int d)
{
if (canDoAABDD (a, b, c, d) && losingCombo (a - 2, b - 1, c, d - 2))
return true;
else if (canDoABCD (a, b, c, d) && losingCombo (a - 1, b - 1, c - 1, d - 1))
return true;
else if (canDoCCD (a, b, c, d) && losingCombo (a, b, c - 2, d - 1))
return true;
else if (canDoBBB (a, b, c, d) && losingCombo (a, b - 3, c, d))
return true;
else if (canDoAD (a, b, c, d) && losingCombo (a - 1, b, c, d - 1))
return true;
else
return false;

}


public static void main (String[] args)
{
Console cc = new Console ();
int a, b, c, d, n;
TextInputFile f = new TextInputFile ("j5.1.in");
n = f.readInt ();
for (int i = 0 ; i < n ; i++)
{
a = f.readInt ();
b = f.readInt ();
c = f.readInt ();
d = f.readInt ();
if (winningCombo (a, b, c, d))
cc.println ("Patrick");
else
cc.println ("Roland");

}
}
}

38 changes: 38 additions & 0 deletions 2008/Senior/S1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// CCC 2008
//
// S1: It's Cold Here
//
// this is the classic "find smallest" number problem
// (The trick is to set the 1st one to the smallest, or coldest)
//

import java.awt.*;
import hsa.*;

public class CCC2008S1ItsColdHere
{
static Console c;

public static void main (String[] args)
{
String city, coldestCity;
int temp, coldest;
c = new Console ();
TextInputFile f = new TextInputFile ("s1.3.in");

city = f.readString ();
coldestCity = city;
coldest = f.readInt ();
while (!city.equals ("Waterloo"))
{
city = f.readString ();
temp = f.readInt ();
if (temp < coldest)
{
coldestCity = city;
coldest = temp;
}
}
c.println (coldestCity);
}
}
Loading

0 comments on commit 6f41291

Please sign in to comment.