Skip to content

Commit 74f34ef

Browse files
committed
CJ 2016 qualification
1 parent 90d30be commit 74f34ef

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package codejam2016.qualification;
2+
3+
import java.io.*;
4+
import java.util.Scanner;
5+
6+
public class CountingSheep {
7+
8+
// general part
9+
private static final String INPUT = "src/main/resources";
10+
private static final String OUTPUT = "target/output";
11+
private static final String ROUND = "codejam2016/qualification";
12+
13+
private static final String SAMPLE = "A-sample.in";
14+
private static final String SMALL = "A-small-attempt0.in";
15+
private static final String LARGE = "A-large.in";
16+
17+
private Scanner scanner;
18+
private PrintWriter writer;
19+
20+
public CountingSheep(InputStream is, OutputStream os) {
21+
scanner = new Scanner(is);
22+
writer = new PrintWriter(os);
23+
}
24+
25+
public void close() {
26+
scanner.close();
27+
writer.flush();
28+
}
29+
30+
private static void runTest(String fileName, boolean isConsole) throws Exception {
31+
InputStream is = initInputStream(fileName);
32+
OutputStream os = initOutputStream(fileName, isConsole);
33+
34+
CountingSheep problem = new CountingSheep(is, os);
35+
problem.solve();
36+
problem.close();
37+
38+
doneStreams(isConsole, is, os);
39+
}
40+
41+
private static InputStream initInputStream(String fileName) throws FileNotFoundException {
42+
File inputDir = new File(INPUT + File.separator + ROUND);
43+
File inputFile = new File(inputDir, fileName);
44+
InputStream is = new FileInputStream(inputFile);
45+
return is;
46+
}
47+
48+
private static OutputStream initOutputStream(String fileName, boolean isConsole) throws FileNotFoundException {
49+
OutputStream os = System.out;
50+
if (isConsole) {
51+
System.out.println(fileName);
52+
System.out.println(" ---] cut [---");
53+
} else {
54+
File outputDir = new File(OUTPUT + File.separator + ROUND);
55+
outputDir.mkdirs();
56+
57+
File outputFile = new File(outputDir, fileName.replace(".in", ".out"));
58+
os = new PrintStream(new FileOutputStream(outputFile));
59+
}
60+
return os;
61+
}
62+
63+
private static void doneStreams(boolean isConsole, InputStream is, OutputStream os) throws IOException {
64+
is.close();
65+
if (isConsole) {
66+
System.out.println(" ---] cut [---");
67+
System.out.println("");
68+
} else {
69+
os.close();
70+
}
71+
}
72+
73+
public static void main(String[] args) {
74+
try {
75+
runTest(SAMPLE, true);
76+
runTest(SMALL, false);
77+
runTest(LARGE, false);
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
83+
// problem part
84+
85+
/**
86+
* Solve the problem
87+
*/
88+
public void solve() {
89+
int t = scanner.nextInt();
90+
91+
// 1 <= T <= 100
92+
for (int i = 1; i <= t; i++) {
93+
writer.print("Case #");
94+
writer.print(i + ": ");
95+
96+
// 0 ≤ N ≤ 10^6.
97+
int n = scanner.nextInt();
98+
writer.println(brute(n));
99+
}
100+
}
101+
102+
private String brute(int n) {
103+
if (n == 0)
104+
return "INSOMNIA";
105+
106+
boolean[] flags = new boolean[10];
107+
108+
Long number = 0l;
109+
do {
110+
number += n;
111+
updateFlags(flags, number.toString());
112+
} while (!allFlags(flags));
113+
return number.toString();
114+
}
115+
116+
private boolean allFlags(boolean[] flags) {
117+
for (boolean flag: flags) {
118+
if (!flag) {
119+
return false;
120+
}
121+
}
122+
return true;
123+
}
124+
125+
private void updateFlags(boolean[] flags, String number) {
126+
for (char ch : number.toCharArray()) {
127+
flags[ch-48] = true;
128+
}
129+
}
130+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package codejam2016.qualification;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class RevengeOfThePancakes {
7+
8+
// general part
9+
private static final String INPUT = "src/main/resources";
10+
private static final String OUTPUT = "target/output";
11+
private static final String ROUND = "codejam2016/qualification";
12+
13+
private static final String SAMPLE = "B-sample.in";
14+
private static final String SMALL = "B-small-attempt0.in";
15+
private static final String LARGE = "B-large.in";
16+
17+
private Scanner scanner;
18+
private PrintWriter writer;
19+
20+
public RevengeOfThePancakes(InputStream is, OutputStream os) {
21+
scanner = new Scanner(is);
22+
writer = new PrintWriter(os);
23+
}
24+
25+
public void close() {
26+
scanner.close();
27+
writer.flush();
28+
}
29+
30+
private static void runTest(String fileName, boolean isConsole) throws Exception {
31+
InputStream is = initInputStream(fileName);
32+
OutputStream os = initOutputStream(fileName, isConsole);
33+
34+
RevengeOfThePancakes problem = new RevengeOfThePancakes(is, os);
35+
problem.solve();
36+
problem.close();
37+
38+
doneStreams(isConsole, is, os);
39+
}
40+
41+
private static InputStream initInputStream(String fileName) throws FileNotFoundException {
42+
File inputDir = new File(INPUT + File.separator + ROUND);
43+
File inputFile = new File(inputDir, fileName);
44+
InputStream is = new FileInputStream(inputFile);
45+
return is;
46+
}
47+
48+
private static OutputStream initOutputStream(String fileName, boolean isConsole) throws FileNotFoundException {
49+
OutputStream os = System.out;
50+
if (isConsole) {
51+
System.out.println(fileName);
52+
System.out.println(" ---] cut [---");
53+
} else {
54+
File outputDir = new File(OUTPUT + File.separator + ROUND);
55+
outputDir.mkdirs();
56+
57+
File outputFile = new File(outputDir, fileName.replace(".in", ".out"));
58+
os = new PrintStream(new FileOutputStream(outputFile));
59+
}
60+
return os;
61+
}
62+
63+
private static void doneStreams(boolean isConsole, InputStream is, OutputStream os) throws IOException {
64+
is.close();
65+
if (isConsole) {
66+
System.out.println(" ---] cut [---");
67+
System.out.println("");
68+
} else {
69+
os.close();
70+
}
71+
}
72+
73+
public static void main(String[] args) {
74+
try {
75+
runTest(SAMPLE, true);
76+
runTest(SMALL, false);
77+
runTest(LARGE, false);
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
83+
// problem part
84+
85+
/**
86+
* Solve the problem
87+
*/
88+
public void solve() {
89+
int t = scanner.nextInt();
90+
91+
// 1 <= T <= 100
92+
for (int i = 1; i <= t; i++) {
93+
writer.print("Case #");
94+
writer.print(i + ": ");
95+
96+
String s = scanner.next();
97+
boolean[] happy = new boolean[s.length()];
98+
for (int j = 0; j < s.length(); j++) {
99+
happy[j] = (s.charAt(j) == '+');
100+
}
101+
writer.printf("%1d\n", greedy(happy));
102+
}
103+
}
104+
105+
private long greedy(boolean[] happy) {
106+
long n = 0;
107+
while (true) {
108+
int i = happy.length - 1;
109+
while (happy[i]) {
110+
i--;
111+
if (i < 0)
112+
return n;
113+
}
114+
115+
for (int j = 0; j <= i; j++) {
116+
happy[j] = !happy[j];
117+
}
118+
119+
n++;
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)