Last active
October 3, 2016 08:06
-
-
Save reubenjohn/d2ebd0188aded0d9a824d1386c114e08 to your computer and use it in GitHub Desktop.
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
import java.io.*; | |
import java.util.*; | |
import org.json.simple.JSONObject; | |
enum TURN{ | |
JUMP,LEFT,RIGHT | |
}; | |
class Turn{ | |
public int pos,cnt; | |
public int el; | |
TURN cmd; | |
public Turn(int position,int elevation,int turnCount,TURN command){ | |
this.pos = position; | |
this.el = elevation; | |
this.cnt = turnCount; | |
this.cmd=command; | |
} | |
} | |
class positionComparator implements Comparator<Turn>{ | |
public int compare(Turn obj1, Turn obj2) { | |
if(obj1.cnt<obj2.cnt) | |
return obj1.cnt; | |
else | |
return obj2.cnt; | |
} | |
} | |
class turnComparator implements Comparator<Turn>{ | |
public int compare(Turn obj1, Turn obj2) { | |
if(obj1.pos<obj2.pos) | |
return 0; | |
else | |
return 1; | |
} | |
} | |
class Terrain { | |
private static final int C=100; | |
private int size; | |
private int t[],c[]; | |
public Terrain(){ | |
Scanner sc = new Scanner(System.in); | |
init(sc); | |
} | |
public Terrain(BufferedReader br) throws IOException{ | |
Scanner sc = new Scanner(br); | |
init(sc); | |
} | |
public void init(Scanner sc){ | |
size=sc.nextInt(); | |
//sc.close(); | |
t = new int[size]; | |
c = new int[size]; | |
for(int i=0;i<size;i++){ | |
int x; | |
x=sc.nextInt(); | |
t[i]=x; | |
} | |
for(int i=0;i<size;i++){ | |
int x; | |
x=sc.nextInt(); | |
c[i]=x; | |
} | |
sc.close(); | |
} | |
public int getTerrainElevationAt(int pos){ | |
return t[pos]; | |
} | |
public int getCoinElevationAt(int pos){ | |
return c[pos]; | |
} | |
public int getTerrainSize(){ | |
return this.size; | |
} | |
public void markCoinAsCollected(int pos){ | |
c[pos] = C; | |
} | |
public int countCollectedCoins(){ | |
int cnt=0; | |
for(int i=0;i<size;i++) | |
if(c[i]==C) | |
cnt++; | |
return cnt; | |
} | |
public void visualize(){ | |
for(int i=0;i<size;i++){ | |
System.out.print('|'); | |
for(int h = 0; h<t[i]; h++) System.out.print('*'); | |
if(c[i]!=C){ | |
for(int h = t[i]; h<c[i]; h++) System.out.print(' '); | |
System.out.print("+"); | |
} | |
System.out.println(); | |
} | |
} | |
} | |
class Mario | |
{ | |
private int el; | |
private int pos; | |
Terrain t; | |
int lifetime; | |
int turnCount; | |
ArrayList<Turn> turns = new ArrayList<Turn>(); | |
public void handleFall(){ | |
if(el > t.getTerrainElevationAt(pos)) | |
el--; | |
} | |
public void addTurnToHistory(TURN command){ | |
Turn turn=new Turn(pos,el,turnCount++,command); | |
turns.add(turn); | |
if(el==t.getCoinElevationAt(pos)) | |
t.markCoinAsCollected(pos); | |
} | |
public Mario(){ | |
this.t=new Terrain(); | |
this.lifetime = t.getTerrainSize(); | |
this.el = 0; | |
this.pos = 1; | |
turnCount = 0; | |
} | |
public Mario(Terrain t,int lifetime){ | |
this.t=t; | |
el=pos=0; | |
this.lifetime=lifetime; | |
turnCount=0; | |
} | |
public int getPosition(){ | |
return pos; | |
} | |
public int getElevation(){ | |
return el; | |
} | |
public int getTerrainSize(){ | |
return t.getTerrainSize(); | |
} | |
public void jump(){ | |
if(lifetime>0){ | |
lifetime--; | |
handleFall(); | |
if(el == t.getTerrainElevationAt(pos)) | |
el+=3; | |
addTurnToHistory(TURN.JUMP); | |
} | |
} | |
public void left(){ | |
if(lifetime>0){ | |
lifetime--; | |
handleFall(); | |
if(el >= t.getTerrainElevationAt(pos-1)) | |
pos--; | |
addTurnToHistory(TURN.LEFT); | |
} | |
} | |
public void right(){ | |
if(lifetime>0){ | |
lifetime--; | |
handleFall(); | |
if(el >= t.getTerrainElevationAt(pos+1)) | |
pos++; | |
addTurnToHistory(TURN.RIGHT); | |
} | |
} | |
public void logStatus(){ | |
System.out.println("Position: "+pos+"\tElevation: "+(int)el); | |
} | |
void visualize(){ | |
Collections.sort(turns,new positionComparator()); | |
int c=0,next = 1; | |
for(int i=0;i<turns.size();i++){ | |
System.out.println(i+" "+turns.get(i).pos+" "+turns.get(i).el); | |
} | |
System.out.println("|*******"); | |
int index = -1, currentP = turns.get(0).pos; | |
for(int i=1;i<t.getTerrainSize();i++){ | |
index++; | |
if(index>=turns.size()) | |
break; | |
while(index<turns.size() && turns.get(index).pos==currentP)index++; | |
index--; | |
currentP++; | |
for(int h = 0; h<10; h++) | |
{ | |
if(index<turns.size()&&h==turns.get(index).el) | |
System.out.print('o'); | |
else if(h<t.getTerrainElevationAt(i)){ | |
System.out.print('*'); | |
}else if(h==t.getCoinElevationAt(i)) | |
System.out.print('+'); | |
else | |
System.out.print(' '); | |
} | |
System.out.println(); | |
} | |
} | |
void outputTurns(){ | |
Collections.sort(turns,new turnComparator()); | |
for(int i=0;i<turns.size();i++){ | |
System.out.print(turns.get(i).cmd.ordinal()+" "); | |
} | |
System.out.println(); | |
} | |
void getResults(ResultStruct rs){ | |
int coinsCollected = t.countCollectedCoins(); | |
int dist = turns.get(turns.size()-1).pos; | |
rs.message = "Coins:\t"+coinsCollected+", Distance:\t"+dist; | |
rs.score = coinsCollected*10 + dist*5; | |
rs.result = true; | |
} | |
}; | |
/* | |
class TestStruct{ | |
String testcase_output_path; | |
} | |
class ResultStruct{ | |
boolean result; | |
String message; | |
float score; | |
} | |
*/ | |
// Start of BODY | |
public class Solution { | |
static boolean is_prime_number(int n) { | |
for(int i=2;i<n;i++) { | |
if(n%i==0) | |
return false; | |
} | |
return true; | |
} | |
static void run_custom_checker(final TestStruct t_obj, ResultStruct r_obj) { | |
// Read contents of the result file | |
String result_data; | |
try { | |
//System.out.println("Reading from "+t_obj.testcase_output_path); | |
BufferedReader br = new BufferedReader(new FileReader(t_obj.testcase_output_path)); | |
// BufferedReader br = new BufferedReader(new FileReader("testCase0.txt")); | |
StringBuilder sb = new StringBuilder(); | |
Terrain t = new Terrain(br); | |
Mario m = new Mario(t,t.getTerrainSize()); | |
//Random rand=new Random(); | |
for(int i=0;i<2*m.getTerrainSize();i++){ | |
//m.logStatus(); | |
m.jump(); | |
m.right(); | |
} | |
//m.logStatus(); | |
//m.visualize(); | |
m.getResults(r_obj); | |
//m.outputTurns(); | |
br.close(); | |
//System.out.println(sb.toString()); | |
} catch(IOException e) { | |
r_obj.result = false; | |
r_obj.score = 0; | |
r_obj.message = "Error reading result file"; | |
return; | |
} | |
} | |
public static void main(String args[]){ | |
//call run_custom_checker here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment