/
game24.cpp
176 lines (146 loc) · 5.06 KB
/
game24.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "game24.h"
#include "sort.h"
#include <algorithm>
#include "rpermut.h"
namespace game24 {
/*
* Algorithm:
*
* for (a,b,c,d) in all permutation of 4 numbers input
* for (op1, op2, op3) in all repeatable triple permutation of + - -r * / /r
* if (24 == a op1 (b op2 (c op3 d)))
* output "a op1 (b op2 (c op3 d))"
* if (24 == (a op1 b) op2 (c op3 d))
* output "(a op1 b) op2 (c op3 d)"
*/
inline
bool swapable(int op) { return op == '*' || op == '+'; }
inline
bool eq(double a, double b) { return (a-b) * (a-b) < 0.01; }
inline
double eval(int op, double a, double b, bool* div_zero) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/':
if (b == 0) {
*div_zero = true;
return 0;
}
else
return a / b;
default:
throw "bad things happened";
}
return 0; /** never reach here **/
}
inline
bool form1(int a, int b, int c, int d, int op1, int op2, int op3) {
// ((a op1 b) op2 c) op3 d
if (a > b && swapable(op1))
return false;
bool div_zero = false;
double r = eval(op3, eval(op2, eval(op1, a, b, &div_zero), c, &div_zero), d, &div_zero);
if (!div_zero && eq(r, 24)) {
fprintf(stdout, "((%d %c %d) %c %d) %c %d\n", a, char(op1), b, char(op2), c, char(op3), d);
return true;
}
return false;
}
inline
bool form2(int a, int b, int c, int d, int op1, int op2, int op3) {
// (a op1 (b op2 c)) op3 d
if (b > c && swapable(op2))
return false;
// already covered by form2 in this case
if (swapable(op1))
return false;
bool div_zero = false;
double r = eval(op3, eval(op1, a, eval(op2, b, c, &div_zero), &div_zero), d, &div_zero);
if (!div_zero && eq(r, 24)) {
fprintf(stdout, "(%d %c (%d %c %d)) %c %d\n", a, char(op1), b, char(op2), c, char(op3), d);
return true;
}
return false;
}
inline
bool form3(int a, int b, int c, int d, int op1, int op2, int op3) {
// (a op1 b) op2 (c op3 d)
if (a > b && swapable(op1))
return false;
if (c > d && swapable(op3))
return false;
bool div_zero = false;
double r = eval(op2, eval(op1, a, b, &div_zero), eval(op3, c, d, &div_zero), &div_zero);
if (!div_zero && eq(r, 24)) {
fprintf(stdout, "(%d %c %d) %c (%d %c %d)\n", a, char(op1), b, char(op2), c, char(op3), d);
return true;
}
return false;
}
inline
bool form4(int a, int b, int c, int d, int op1, int op2, int op3) {
// a op1 ((b op2 c) op3 d)
if (b > c && swapable(op2))
return false;
// already covered by form1 in this case
if (swapable(op1))
return false;
bool div_zero = false;
double r = eval(op1, a, eval(op3, eval(op2, b, c, &div_zero), d, &div_zero), &div_zero);
if (!div_zero && eq(r, 24)) {
fprintf(stdout, "%d %c ((%d %c %d) %c %d)\n", a, char(op1), b, char(op2), c, char(op3), d);
return true;
}
return false;
}
inline
bool form5(int a, int b, int c, int d, int op1, int op2, int op3) {
// a op1 (b op2 (c op3 d))
if (c > d && swapable(op3))
return false;
// already covered by form1 in this case
if (swapable(op1))
return false;
// already covered by form4 in this case
if (swapable(op2))
return false;
bool div_zero = false;
double r = eval(op1, a, eval(op2, b, eval(op3, c, d, &div_zero), &div_zero), &div_zero);
if (!div_zero && eq(r, 24)) {
fprintf(stdout, "%d %c (%d %c (%d %c %d))\n", a, char(op1), b, char(op2), c, char(op3), d);
return true;
}
return false;
}
bool exprs_for_24(int a1, int a2, int a3, int a4)
{
int ar[4] = {a1, a2, a3, a4};
sort_quick(4, ar);
int ops[4] = {'+', '-', '*', '/'};
int opr[3];
Rpermut rp(3, opr, sizeof(ops)/sizeof(int), ops);
int a, b, c, d;
int op1, op2, op3;
bool found = false;
for (; std::next_permutation(ar, ar+4); ) {
a = ar[0];
b = ar[1];
c = ar[2];
d = ar[3];
for (rp.begin(); !rp.ended; rp.next()) {
op1 = opr[0];
op2 = opr[1];
op3 = opr[2];
if (form1(a, b, c, d, op1, op2, op3) ||
form2(a, b, c, d, op1, op2, op3) ||
form3(a, b, c, d, op1, op2, op3) ||
form4(a, b, c, d, op1, op2, op3) ||
form5(a, b, c, d, op1, op2, op3))
found = true;
}
}
return found;
}
}