-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperson.java
More file actions
135 lines (125 loc) · 3.75 KB
/
person.java
File metadata and controls
135 lines (125 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package problems;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
public class person {
static boolean dontPrintCrap = false;
static StringBuilder finalAnswer = new StringBuilder();
static int countOfRounds = 0;
static int countOfDead = 0;
static int numberOfPeople = 0;
boolean holderOfGun = false;
boolean isDead = false;
int number = 0;
person(int n){
this.number = n;
}
public static Boolean isAnyoneAlive(person[] arrayoPersons){
for(int i=0; i<arrayoPersons.length; i++){
if(!arrayoPersons[i].isDead)
return true;
}
return false;
}
public static void accountForAlives(person[] arrayOfPeople){
System.out.print("Persons alive : ");
for(int i=0; i<arrayOfPeople.length; i++){
if(!arrayOfPeople[i].isDead)
{
System.out.print(arrayOfPeople[i].number + " ");
}
}
System.out.println("\n");
}
//returns first alive person
public static int getAlivePerson(int indexOfGunHolder, person[] arrayOfPeople){
for(int i = indexOfGunHolder+1; i<arrayOfPeople.length; i++){
if(!arrayOfPeople[i].isDead)
{
return i;
}
}
return -1; //return -1 if no one is deemed alive
}
public static void recurse(int startIndex, person[] arrayOfPeople){
if(countOfDead < numberOfPeople-1)
{
int nextAlivePerson;
int personWhoKillsNext;
nextAlivePerson = getAlivePerson(startIndex, arrayOfPeople);
if(nextAlivePerson == -1)
{
nextAlivePerson = getAlivePerson(-1, arrayOfPeople);
}
arrayOfPeople[nextAlivePerson].isDead = true;
countOfDead++;
personWhoKillsNext = getAlivePerson(startIndex, arrayOfPeople);
if(personWhoKillsNext == -1){
personWhoKillsNext = getAlivePerson(-1, arrayOfPeople);
countOfRounds++;
if(!dontPrintCrap){
System.out.println("Round number : " + countOfRounds + " finished");
accountForAlives(arrayOfPeople);
}
}
recurse(personWhoKillsNext, arrayOfPeople);
}else{
if(!dontPrintCrap)
System.out.println(arrayOfPeople[getAlivePerson(-1, arrayOfPeople)].number + " is the last man standing!");
else{
//print to a text file : numberOfPeople, lastManStanding
finalAnswer.append(numberOfPeople + "," + arrayOfPeople[getAlivePerson(-1, arrayOfPeople)].number + "\n");
}
}
}
public static person[] makePeople(int number){
person[] arrayOfPeople = new person[number];
for(int i=0; i<number; i++){
arrayOfPeople[i] = new person(i+1);
}
return arrayOfPeople;
}
public static void main(String args[]){
dontPrintCrap = true;
if(!dontPrintCrap)
{
numberOfPeople = 3;
person[] arrayOfPeople = makePeople(numberOfPeople);
long startTime = Calendar.getInstance().getTimeInMillis();
recurse(0, arrayOfPeople);
long endTime = Calendar.getInstance().getTimeInMillis();
System.out.println("Time taken : " + (endTime-startTime) + "ms");
}
else
{
//to generate a csv
int number = 10000;
boolean writeSuccess = false;
long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = 0;
for(int j = 1; j<=number; j++){
numberOfPeople = j;
countOfDead = 0;
person[] genericPeopleArray = makePeople(j);
recurse(0, genericPeopleArray);
}
//write final answer to a csv file
File outFile = new File("C:\\Users\\Raghudevan.S\\Desktop\\findCurve.csv");
try {
BufferedWriter output = new BufferedWriter(new FileWriter(outFile));
output.write(finalAnswer.toString());
output.close();
endTime = Calendar.getInstance().getTimeInMillis();
writeSuccess = true;
} catch (IOException e) {
writeSuccess = false;
e.printStackTrace();
}finally{
if(writeSuccess)
System.out.println("Write to csv success! Time taken : " + (endTime-startTime) + "ms" );
}
}
}
}