-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallengeSheet.java
More file actions
134 lines (104 loc) · 3.11 KB
/
ChallengeSheet.java
File metadata and controls
134 lines (104 loc) · 3.11 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
import java.util.*;
import java.nio.file.*;
import java.nio.charset.*;
import java.io.*;
public class ChallengeSheet {
public static void makeSheet(String outputFileName) {
Difficulty[] ds = {
Difficulty.getEasyDifficulty(),
Difficulty.getMediumDifficulty(),
Difficulty.getHardDifficulty()
};
Challenge[] cs = new Challenge[3];
{
int found = 0;
while (found != 3) {
Challenge c = new Challenge(ds[found]);
ArrayList<Operator> operators = c.getChallenge();
boolean fail = false;
// Throw away bad Challenges that use the same operator twice in a row
for (int i = 2; i < operators.size(); i++) {
if (operators.get(i).getClass().equals(operators.get(i-1).getClass())) {
fail = true;
}
}
if (!fail) {
cs[found++] = c;
}
}
}
int[] solutions = { cs[0].getSolution(), cs[1].getSolution(), cs[2].getSolution() };
// Open Files
List<String> head = null;
List<String> tail = null;
List<String> chHead = null;
List<String> chTail = null;
StringBuilder sb = new StringBuilder();
try {
Path headP = FileSystems.getDefault().getPath("tex", "head.tex");
Path tailP = FileSystems.getDefault().getPath("tex", "tail.tex");
Path chHeadP = FileSystems.getDefault().getPath("tex", "challengeBefore.tex");
Path chTailP = FileSystems.getDefault().getPath("tex", "challengeAfter.tex");
Charset charset = Charset.defaultCharset();
head = Files.readAllLines(headP, charset);
tail = Files.readAllLines(tailP, charset);
chHead = Files.readAllLines(chHeadP, charset);
chTail = Files.readAllLines(chTailP, charset);
} catch (Exception e) {
e.printStackTrace();
}
// Build Document
for (String s : head) {
sb.append(s);
sb.append("\n");
}
for (Challenge c : cs) {
for (String s : chHead) {
sb.append(s);
sb.append("\n");
}
for (Operator o : c.getChallenge()) {
sb.append(o.toTexString());
sb.append(" & ");
}
sb.append("\\textcolor{white}{?} \\\\");
for (String s : chTail) {
sb.append(s);
sb.append("\n");
}
}
sb.append("\\begin{turn}{180}");
sb.append("Lösungen: ");
sb.append(solutions[0]);
sb.append(", ");
sb.append(solutions[1]);
sb.append(", ");
sb.append(solutions[2]);
sb.append("\n\\end{turn}");
for (String s : tail) {
sb.append(s);
sb.append("\n");
}
Path tempDir = null;
// Run pdflatex
try {
tempDir = Files.createTempDirectory("30seconds");
File log = new File("/dev/null");
ProcessBuilder pb = new ProcessBuilder("pdflatex", "-jobname", "30seconds");
pb.directory(tempDir.toFile());
pb.redirectErrorStream(true);
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
Process p = pb.start();
OutputStream out = p.getOutputStream();
out.write(sb.toString().getBytes());
out.flush(); out.close();
p.waitFor();
final Path filename = FileSystems.getDefault().getPath("30seconds.pdf");
final Path pdf = tempDir.resolve(filename);
final Path destination = FileSystems.getDefault().getPath(outputFileName);
Files.move(pdf, destination);
} catch (Exception e) {
e.printStackTrace();
}
}
}