Skip to content

Instantly share code, notes, and snippets.

@aziflaj
Created February 15, 2016 10:25
Show Gist options
  • Save aziflaj/112e2d6ce6936d77dab6 to your computer and use it in GitHub Desktop.
Save aziflaj/112e2d6ce6936d77dab6 to your computer and use it in GitHub Desktop.
My solutions to the Qualification Round of Google Code Jam. You can find the questions here https://code.google.com/codejam/contest/2974486/dashboard
/**
* Google Code Jam 1
* Pset1.java
*/
import java.io.*;
import java.util.Scanner;
public class Pset1 {
public static void main(String[] args) throws Exception {
File file = new File("A-small-attempt2.in");
File outFile = new File("A-small-attempt2.out");
Scanner stdIn = new Scanner(file); //read input from file
PrintWriter pw = new PrintWriter(outFile); //write output to outFile
int tests; // casess of test, [1;100]
int cases=1;
int row1=0,row2=0; // row of card for the 1st and 2nd time
int[][] cards1 = new int[4][4]; //matrix of cards for the 1st time
int[][] cards2 = new int[4][4]; //matrix of cards for the 2nd time
int numbersMatch = 0;
int flag=0;
//read the number of tests from the file
tests = stdIn.nextInt();
if(tests<0 || tests>100) { //error on this cases
return;
}
for(int i=0;i<tests;i++) {
//read the 1st row
row1 = stdIn.nextInt();
row1--;
//read the first arrange
for (int a=0;a<4;a++) {
for(int b=0;b<4;b++) {
cards1[a][b] = stdIn.nextInt();
}
}
//read the 2nd row
row2 = stdIn.nextInt();
row2--;
//read the 2nd arrange
for (int a=0;a<4;a++) {
for(int b=0;b<4;b++) {
cards2[a][b] = stdIn.nextInt();
}
}
//check if any of the cards in cards1's row1 is also in cards2's row2
for (int count=0;count<4;count++) {
for(int cnt=0;cnt<4;cnt++) {
if(cards1[row1][count] == cards2[row2][cnt]) { //match found
numbersMatch++;
flag = cnt;
}
}
}
//work things out with flag and numbersMatch
if(numbersMatch == 0) { //magician did not found anything
pw.println(String.format("Case #%d: Volunteer cheated!",cases));
}
else if(numbersMatch == 1) { //magician found the card
pw.println(String.format("Case #%d: %d",cases,cards2[row2][flag]));
}
else if (numbersMatch>1) pw.println(String.format("Case #%d: Bad magician!",cases));
cases++;
numbersMatch=0;
}
stdIn.close();
pw.close();
}
}
/**
* Google Code Jam
* pset2.java
*/
import java.io.*;
import java.util.Scanner;
public class Pset2 {
public static void main(String[] args) throws Exception {
File file = new File("B-large.in");
File outFile = new File("OutputLarge.txt");
Scanner input = new Scanner(file);
PrintWriter pw = new PrintWriter(outFile);
int tests; // between 1 and 100
double C, F, X; //cookies per farm, cookie farm gain, max score
double time = 0.0; //time to achieve X
double cookieRate = 2.0; //rate of cookie multiply
double timeForFarm = 0.0;
double timeForX = 0.0;
double timeForXAfterFarm = 0.0 ;
//read number of tests
tests = input.nextInt();
if(tests<1 || tests>100) {
return;
}
//iterate over the file for tests times
for (int i=0;i<tests;i++) {
//read C, F and X
C = input.nextDouble();
F = input.nextDouble();
X = input.nextDouble();
do {
//calculate time to achieve X with current cookieRate
timeForX = (double) X/cookieRate;
//calculate time to achieve C with current cookieRate
timeForFarm = (double) C/cookieRate;
timeForXAfterFarm = timeForFarm + X/(cookieRate+F);
if (timeForXAfterFarm < timeForX) { //buys a farm
cookieRate += F;
time += timeForFarm;
if (timeForFarm * cookieRate > X) {
time += X/cookieRate;
break;
}
}
else {
time += timeForX;
break;
}
} while(timeForX > timeForXAfterFarm);
//print into file
pw.println(String.format("Case #%d: %.7f",i+1,time));
//reset variables
time = 0.0;
timeForFarm = 0.0;
timeForX = 0.0;
timeForXAfterFarm = 0.0;
cookieRate = 2.0;
}
input.close();
pw.close();
}
}
/**
* Google Code Jam
* Pset3.java
*/
import java.io.*;
import java.util.Scanner;
public class Pset3 {
public static void main(String[] args) throws Exception {
File file = new File("input.txt");
File outFile = new File("Output.txt");
Scanner input = new Scanner(file);
PrintWriter pw = new PrintWriter(outFile);
int T; //test cases
int M; //number of mines
int R,C; //dimensions of field
//read the number of test cases
T = input.nextInt();
for (int i=0;i<T;i++) {
//read dimensions
R = input.nextInt();
C = input.nextInt();
//read number of mines
M = input.nextInt();
if (R*C-M<4) pw.println(String.format("Case #%d:\nImpossible",i+1));
else if ( R*C-M==1 && (R==1 || C==1)) pw.println(String.format("Case #%d:\nImpossible",i+1));
else { //ka zgjidhje ta haj dreqi :(
for (int a=0;a<R-2;a++)
print('*',C);
System.out.print("..");
print('*',C-2);
System.out.print("c.");
print('*',C-2);
System.out.println("\n\n\n");
}
}
input.close();
pw.close();
}
static void print(char ch,int times) {
for (int i=0;i<times;i++)
System.out.print(ch);
System.out.print("\n");
}
}
/**
* Google Code Jam
* Pset4.java
*/
import java.io.*;
import java.util.*;
public class Pset4 {
public static void main(String[] args) throws Exception {
File file = new File("D-large.in");
File outFile = new File("OutputLarge.txt");
Scanner input = new Scanner(file);
PrintWriter pw = new PrintWriter(outFile);
int testCases; //number of times the program will iterate
int N; //number of blocks for each player
List<Double> naomiWeights = null; //weights of Naomi
List<Double> kenWeights = null; //weights of Ken
int warScore=0; //score of Naomi while playing War
int dwarScore=0; //score of Naomi while playing Deceitful War
double tN; //number that Naomi tells to Ken in a Deceitful War game
double cN,cK=0;
//read testCases
testCases = input.nextInt();
System.out.println("testCases: "+testCases);
if(testCases>51 || testCases<0) { //test cases out of bound
System.exit(1);
}
for (int i=0;i<testCases;i++) { //iterate over test cases
//read number of blocks
N = input.nextInt();
System.out.println("N: "+N);
naomiWeights = new ArrayList<>();
kenWeights = new ArrayList<>();
//read naomiWeights
for(int nw=0;nw<N;nw++) naomiWeights.add(input.nextDouble());
//read kenWeights
for(int kw=0;kw<N;kw++) kenWeights.add(input.nextDouble());
if(N==1) {
if(kenWeights.get(0) < naomiWeights.get(0)) warScore = dwarScore = 1;
//now print values into the outFile
pw.println(String.format("Case #%d: %d %d",i+1,dwarScore,warScore));
System.out.println(String.format("Case #%d: %d %d",i+1,dwarScore,warScore));
System.out.println("\n\n");
//reset variables in the end
dwarScore=0;
warScore=0;
N=0;
naomiWeights = null;
kenWeights = null;
continue;
}
//sort weights
Collections.sort(naomiWeights);
Collections.sort(kenWeights);
List<Double> kenCopy = new ArrayList<>(kenWeights);
System.out.println("naomiWeights: "+naomiWeights);
System.out.println("kenWeights: "+kenWeights);
/**
* Let's calculate how many points can Naomi win in a War game.
* She will win the same amount of weights that are bigger than
* Ken's, while looping from the end to the begginning of the array.
*/
for(int index=0;index<N;index++) {
cN = tN = naomiWeights.get(index);
//search for kN
for(int l=0;l<kenCopy.size();l++) {
if (kenCopy.get(l)>tN) {
cK = kenCopy.get(l);
break; //found the smaller weight bigger than tN
}
cK = kenCopy.get(l); //get the last if break is not reached
}
if (cN > cK) warScore++;
kenCopy.remove(kenCopy.indexOf(cK));
}
System.out.println("warScore: "+warScore);
kenCopy = new ArrayList<>(kenWeights);
/**
* Let's calculate how many points can Naomi win in a Deceitful War
* game. She throws her smaller weight and tells the average of 2 of
* Ken's bigger weights, in order to get rid of massive weights of Ken
*/
while (!kenCopy.isEmpty()) {
if (naomiWeights.get(naomiWeights.size()-1) > kenCopy.get(kenCopy.size()-1) ) {
dwarScore++;
naomiWeights.remove(naomiWeights.size()-1);
kenCopy.remove(kenCopy.size()-1);
}
else {
if(kenCopy.size() == 1) { //every value removed
cK = kenCopy.get(0);
cN = naomiWeights.get(naomiWeights.size()-1);
if (cN > cK) dwarScore++;
break;
}
cN = naomiWeights.get(0);
tN = (kenCopy.get(kenCopy.size()-1) + kenCopy.get(kenCopy.size()-2))/2;
//search for kN
cK = kenCopy.get(kenCopy.size()-1);
//kenCopy.remove(kenCopy.size()-1);
if (cN > cK) dwarScore++;
kenCopy.remove(kenCopy.indexOf(cK));
}
}
/*
while(!kenCopy.isEmpty()) {
if(kenCopy.size() == 1) { //every value removed
cK = kenCopy.get(0);
cN = naomiWeights.get(naomiWeights.size()-1);
if (cN > cK) dwarScore++;
break;
}
cN = naomiWeights.get(0);
tN = (kenCopy.get(kenCopy.size()-1) + kenCopy.get(kenCopy.size()-2))/2;
//search for kN
cK = kenCopy.get(kenCopy.size()-1);
//kenCopy.remove(kenCopy.size()-1);
if (cN > cK) dwarScore++;
kenCopy.remove(kenCopy.indexOf(cK));
//index++;
if(kenCopy.get(kenCopy.size()-1) < naomiWeights.get(naomiWeights.size()-1)) {
dwarScore++;
kenCopy.remove(kenCopy.size()-1);
naomiWeights.remove(naomiWeights.size()-1);
}
}
*/
/*
do {
//krahaso max2 me max1
if(kenCopy.get(kenCopy.size()-1) < naomiWeights.get(naomiWeights.size()-1)) {
dwarScore++;
kenCopy.remove(kenCopy.size()-1);
naomiWeights.remove(naomiWeights.size()-1);
}
else {
kenCopy.remove(kenCopy.size()-1);
naomiWeights.remove(naomiWeights.size()-1);
}
}while(! kenCopy.isEmpty());
*/
System.out.println("dwarScore: "+dwarScore);
//now print values into the outFile
pw.println(String.format("Case #%d: %d %d",i+1,dwarScore,warScore));
System.out.println(String.format("Case #%d: %d %d",i+1,dwarScore,warScore));
System.out.println("\n\n");
//reset variables in the end
dwarScore=0;
warScore=0;
N=0;
naomiWeights = null;
kenWeights = null;
}
input.close();
pw.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment