-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.java
144 lines (105 loc) · 2.67 KB
/
dice.java
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
136
137
138
139
140
141
import java.lang.Long;
import java.util.HashMap;
import java.util.LinkedList;
public class dice {
private static String nl = "\n";
private static String tt = "\t\t";
public static void main(String[] args) {
HashMap params = parse(args);
long adder = (Long) params.get("constant");
double combos = (Double) params.get("combos");
LinkedList<Poly> polys = (LinkedList<Poly>) params.get("polys");
Poly result = Poly.unit();
for (Poly p : polys) {
result = result.mul(p);
}
result = result.div(combos);
StringBuilder sob = new StringBuilder();
sob.append(nl);
for (int i = polys.size(); i <= result.degree; i++) {
sob.append(
String.format(
"%d\t\t%.5f\t\t%s\n",
(long)i + adder,
result.coeffz[i],
histogram(result.coeffz[i])
)
);
}
System.out.println(sob.toString());
}
public static class Poly {
public int degree;
public double[] coeffz;
public Poly(double[] yarr) {
coeffz = yarr;
degree = yarr.length - 1;
}
public static Poly unit() {
double[] unit = { 1.0 };
return new Poly(unit);
}
public static Poly mkroll(int die) {
double[] yarr = new double[die + 1];
yarr[0] = 0;
for (int i = 1; i <= die; i++) {
yarr[i] = 1.0;
}
return new Poly(yarr);
}
public Poly mul(double scalar) {
double[] yarr = new double[degree + 1];
for (int i = 0; i <= degree; i++) {
yarr[i] = coeffz[i] * scalar;
}
return new Poly(yarr);
}
public Poly mul(Poly other) {
int newD = degree + other.degree;
double[] yarr = new double[newD + 1];
for (int i = 0; i <= degree; i++) {
for (int j = 0; j <= other.degree; j++) {
yarr[i + j] += coeffz[i] * other.coeffz[j];
}
}
return new Poly(yarr);
}
public Poly div(double scalar) {
return mul( 1/scalar );
}
}
private static HashMap parse(String[] args) {
Long constant = 0L;
Double combos = 1.0;
LinkedList<Poly> polys = new LinkedList<Poly>();
for (String arg : args) {
if (arg.indexOf('d') > 0) {
// die roll
String[] tmp = arg.split("d");
int n = Integer.parseInt(tmp[0]);
int d = Integer.parseInt(tmp[1]);
combos *= Math.pow((double)d, (double)n);
for (int i = 0; i < n; i++) {
polys.add(Poly.mkroll(d));
}
}
else {
// constant
constant += Integer.parseInt(arg);
}
}
HashMap rval = new HashMap();
rval.put("constant", constant);
rval.put("combos", combos);
rval.put("polys", polys);
return rval;
}
private static String histogram(double n) {
int num = (int) (0.5 + (500.0 * n));
char[] hg = new char[num];
for (int i = 0; i < num; i++) {
hg[i] = '#';
}
return new String(hg);
}
}