6702ffae264e03b8ffa2b626741de08dc36e6515
2 * This program implements a breadth-first search which
3 * exhaustively solves the Countdown numbers game, and related
4 * games with slightly different rule sets such as `Flippo'.
6 * Currently it is simply a standalone command-line utility to
7 * which you provide a set of numbers and it tells you everything
8 * it can make together with how many different ways it can be
9 * made. I would like ultimately to turn it into the generator for
10 * a Puzzles puzzle, but I haven't even started on writing a
11 * Puzzles user interface yet.
17 * - start thinking about difficulty ratings
18 * + anything involving associative operations will be flagged
19 * as many-paths because of the associative options (e.g.
20 * 2*3*4 can be (2*3)*4 or 2*(3*4), or indeed (2*4)*3). This
21 * is probably a _good_ thing, since those are unusually
23 * + tree-structured calculations ((a*b)/(c+d)) have multiple
24 * paths because the independent branches of the tree can be
25 * evaluated in either order, whereas straight-line
26 * calculations with no branches will be considered easier.
27 * Can we do anything about this? It's certainly not clear to
28 * me that tree-structure calculations are _easier_, although
29 * I'm also not convinced they're harder.
30 * + I think for a realistic difficulty assessment we must also
31 * consider the `obviousness' of the arithmetic operations in
32 * some heuristic sense, and also (in Countdown) how many
33 * numbers ended up being used.
34 * - actually try some generations
35 * - at this point we're probably ready to start on the Puzzles
49 * To search for numbers we can make, we employ a breadth-first
50 * search across the space of sets of input numbers. That is, for
51 * example, we start with the set (3,6,25,50,75,100); we apply
52 * moves which involve combining two numbers (e.g. adding the 50
53 * and the 75 takes us to the set (3,6,25,100,125); and then we see
54 * if we ever end up with a set containing (say) 952.
56 * If the rules are changed so that all the numbers must be used,
57 * this is easy to adjust to: we simply see if we end up with a set
58 * containing _only_ (say) 952.
60 * Obviously, we can vary the rules about permitted arithmetic
61 * operations simply by altering the set of valid moves in the bfs.
62 * However, there's one common rule in this sort of puzzle which
63 * takes a little more thought, and that's _concatenation_. For
64 * example, if you are given (say) four 4s and required to make 10,
65 * you are permitted to combine two of the 4s into a 44 to begin
66 * with, making (44-4)/4 = 10. However, you are generally not
67 * allowed to concatenate two numbers that _weren't_ both in the
68 * original input set (you couldn't multiply two 4s to get 16 and
69 * then concatenate a 4 on to it to make 164), so concatenation is
70 * not an operation which is valid in all situations.
72 * We could enforce this restriction by storing a flag alongside
73 * each number indicating whether or not it's an original number;
74 * the rules being that concatenation of two numbers is only valid
75 * if they both have the original flag, and that its output _also_
76 * has the original flag (so that you can concatenate three 4s into
77 * a 444), but that applying any other arithmetic operation clears
78 * the original flag on the output. However, we can get marginally
79 * simpler than that by observing that since concatenation has to
80 * happen to a number before any other operation, we can simply
81 * place all the concatenations at the start of the search. In
82 * other words, we have a global flag on an entire number _set_
83 * which indicates whether we are still permitted to perform
84 * concatenations; if so, we can concatenate any of the numbers in
85 * that set. Performing any other operation clears the flag.
88 #define SETFLAG_CONCAT 1 /* we can do concatenation */
93 struct set
*prev
; /* index of ancestor set in set list */
94 unsigned char pa
, pb
, po
, pr
; /* operation that got here from prev */
98 int *numbers
; /* rationals stored as n,d pairs */
99 short nnumbers
; /* # of rationals, so half # of ints */
100 short flags
; /* SETFLAG_CONCAT only, at present */
101 int npaths
; /* number of ways to reach this set */
102 struct ancestor a
; /* primary ancestor */
103 struct ancestor
*as
; /* further ancestors, if we care */
110 int index
; /* which number in the set is it? */
111 int npaths
; /* number of ways to reach this */
114 #define SETLISTLEN 1024
115 #define NUMBERLISTLEN 32768
116 #define OUTPUTLISTLEN 1024
119 struct set
**setlists
;
120 int nsets
, nsetlists
, setlistsize
;
123 int nnumbers
, nnumberlists
, numberlistsize
;
124 struct output
**outputlists
;
125 int noutputs
, noutputlists
, outputlistsize
;
127 const struct operation
*const *ops
;
130 #define OPFLAG_NEEDS_CONCAT 1
131 #define OPFLAG_KEEPS_CONCAT 2
132 #define OPFLAG_UNARY 4
133 #define OPFLAG_UNARYPFX 8
137 * Most operations should be shown in the output working, but
138 * concatenation should not; we just take the result of the
139 * concatenation and assume that it's obvious how it was
145 * Text display of the operator, in expressions and for
146 * debugging respectively.
148 char *text
, *dbgtext
;
151 * Flags dictating when the operator can be applied.
156 * Priority of the operator (for avoiding unnecessary
157 * parentheses when formatting it into a string).
162 * Associativity of the operator. Bit 0 means we need parens
163 * when the left operand of one of these operators is another
164 * instance of it, e.g. (2^3)^4. Bit 1 means we need parens
165 * when the right operand is another instance of the same
166 * operator, e.g. 2-(3-4). Thus:
168 * - this field is 0 for a fully associative operator, since
169 * we never need parens.
170 * - it's 1 for a right-associative operator.
171 * - it's 2 for a left-associative operator.
172 * - it's 3 for a _non_-associative operator (which always
173 * uses parens just to be sure).
178 * Whether the operator is commutative. Saves time in the
179 * search if we don't have to try it both ways round.
184 * Function which implements the operator. Returns TRUE on
185 * success, FALSE on failure. Takes two rationals and writes
188 int (*perform
)(int *a
, int *b
, int *output
);
192 const struct operation
*const *ops
;
196 #define MUL(r, a, b) do { \
198 if ((b) && (a) && (r) / (b) != (a)) return FALSE; \
201 #define ADD(r, a, b) do { \
203 if ((a) > 0 && (b) > 0 && (r) < 0) return FALSE; \
204 if ((a) < 0 && (b) < 0 && (r) > 0) return FALSE; \
207 #define OUT(output, n, d) do { \
208 int g = gcd((n),(d)); \
210 if ((d) < 0) g = -g; \
211 if (g == -1 && (n) < -INT_MAX) return FALSE; \
212 if (g == -1 && (d) < -INT_MAX) return FALSE; \
213 (output)[0] = (n)/g; \
214 (output)[1] = (d)/g; \
215 assert((output)[1] > 0); \
218 static int gcd(int x
, int y
)
220 while (x
!= 0 && y
!= 0) {
226 return abs(x
+ y
); /* i.e. whichever one isn't zero */
229 static int perform_add(int *a
, int *b
, int *output
)
233 * a0/a1 + b0/b1 = (a0*b1 + b0*a1) / (a1*b1)
243 static int perform_sub(int *a
, int *b
, int *output
)
247 * a0/a1 - b0/b1 = (a0*b1 - b0*a1) / (a1*b1)
257 static int perform_mul(int *a
, int *b
, int *output
)
261 * a0/a1 * b0/b1 = (a0*b0) / (a1*b1)
269 static int perform_div(int *a
, int *b
, int *output
)
274 * Division by zero is outlawed.
280 * a0/a1 / b0/b1 = (a0*b1) / (a1*b0)
288 static int perform_exact_div(int *a
, int *b
, int *output
)
293 * Division by zero is outlawed.
299 * a0/a1 / b0/b1 = (a0*b1) / (a1*b0)
306 * Exact division means we require the result to be an integer.
308 return (output
[1] == 1);
311 static int perform_concat(int *a
, int *b
, int *output
)
316 * We can't concatenate anything which isn't a non-negative
319 if (a
[1] != 1 || b
[1] != 1 || a
[0] < 0 || b
[0] < 0)
323 * For concatenation, we can safely assume leading zeroes
324 * aren't an issue. It isn't clear whether they `should' be
325 * allowed, but it turns out not to matter: concatenating a
326 * leading zero on to a number in order to harmlessly get rid
327 * of the zero is never necessary because unwanted zeroes can
328 * be disposed of by adding them to something instead. So we
329 * disallow them always.
331 * The only other possibility is that you might want to
332 * concatenate a leading zero on to something and then
333 * concatenate another non-zero digit on to _that_ (to make,
334 * for example, 106); but that's also unnecessary, because you
335 * can make 106 just as easily by concatenating the 0 on to the
336 * _end_ of the 1 first.
342 * Find the smallest power of ten strictly greater than b. This
343 * is the power of ten by which we'll multiply a.
345 * Special case: we must multiply a by at least 10, even if b
349 while (p10
<= (INT_MAX
/10) && p10
<= b
[0])
351 if (p10
> INT_MAX
/10)
352 return FALSE
; /* integer overflow */
359 #define IPOW(ret, x, y) do { \
360 int ipow_limit = (y); \
361 if ((x) == 1 || (x) == 0) ipow_limit = 1; \
362 else if ((x) == -1) ipow_limit &= 1; \
364 while (ipow_limit-- > 0) { \
371 static int perform_exp(int *a
, int *b
, int *output
)
373 int an
, ad
, xn
, xd
, limit
, t
, i
;
376 * Exponentiation is permitted if the result is rational. This
379 * - first we see whether we can take the (denominator-of-b)th
380 * root of a and get a rational; if not, we give up.
382 * - then we do take that root of a
384 * - then we multiply by itself (numerator-of-b) times.
387 an
= 0.5 + pow(a
[0], 1.0/b
[1]);
388 ad
= 0.5 + pow(a
[1], 1.0/b
[1]);
391 if (xn
!= a
[0] || xd
!= a
[1])
411 static int perform_factorial(int *a
, int *b
, int *output
)
416 * Factorials of non-negative integers are permitted.
418 if (a
[1] != 1 || a
[0] < 0)
422 * However, a special case: we don't take a factorial of
423 * anything which would thereby remain the same.
425 if (a
[0] == 1 || a
[0] == 2)
429 for (i
= 1; i
<= a
[0]; i
++) {
438 const static struct operation op_add
= {
439 TRUE
, "+", "+", 0, 10, 0, TRUE
, perform_add
441 const static struct operation op_sub
= {
442 TRUE
, "-", "-", 0, 10, 2, FALSE
, perform_sub
444 const static struct operation op_mul
= {
445 TRUE
, "*", "*", 0, 20, 0, TRUE
, perform_mul
447 const static struct operation op_div
= {
448 TRUE
, "/", "/", 0, 20, 2, FALSE
, perform_div
450 const static struct operation op_xdiv
= {
451 TRUE
, "/", "/", 0, 20, 2, FALSE
, perform_exact_div
453 const static struct operation op_concat
= {
454 FALSE
, "", "concat", OPFLAG_NEEDS_CONCAT
| OPFLAG_KEEPS_CONCAT
,
455 1000, 0, FALSE
, perform_concat
457 const static struct operation op_exp
= {
458 TRUE
, "^", "^", 0, 30, 1, FALSE
, perform_exp
460 const static struct operation op_factorial
= {
461 TRUE
, "!", "!", OPFLAG_UNARY
, 40, 0, FALSE
, perform_factorial
465 * In Countdown, divisions resulting in fractions are disallowed.
466 * http://www.askoxford.com/wordgames/countdown/rules/
468 const static struct operation
*const ops_countdown
[] = {
469 &op_add
, &op_mul
, &op_sub
, &op_xdiv
, NULL
471 const static struct rules rules_countdown
= {
476 * A slightly different rule set which handles the reasonably well
477 * known puzzle of making 24 using two 3s and two 8s. For this we
478 * need rational rather than integer division.
480 const static struct operation
*const ops_3388
[] = {
481 &op_add
, &op_mul
, &op_sub
, &op_div
, NULL
483 const static struct rules rules_3388
= {
488 * A still more permissive rule set usable for the four-4s problem
489 * and similar things. Permits concatenation.
491 const static struct operation
*const ops_four4s
[] = {
492 &op_add
, &op_mul
, &op_sub
, &op_div
, &op_concat
, NULL
494 const static struct rules rules_four4s
= {
499 * The most permissive ruleset I can think of. Permits
500 * exponentiation, and also silly unary operators like factorials.
502 const static struct operation
*const ops_anythinggoes
[] = {
503 &op_add
, &op_mul
, &op_sub
, &op_div
, &op_concat
, &op_exp
, &op_factorial
, NULL
505 const static struct rules rules_anythinggoes
= {
506 ops_anythinggoes
, TRUE
509 #define ratcmp(a,op,b) ( (long long)(a)[0] * (b)[1] op \
510 (long long)(b)[0] * (a)[1] )
512 static int addtoset(struct set
*set
, int newnumber
[2])
516 /* Find where we want to insert the new number */
517 for (i
= 0; i
< set
->nnumbers
&&
518 ratcmp(set
->numbers
+2*i
, <, newnumber
); i
++);
520 /* Move everything else up */
521 for (j
= set
->nnumbers
; j
> i
; j
--) {
522 set
->numbers
[2*j
] = set
->numbers
[2*j
-2];
523 set
->numbers
[2*j
+1] = set
->numbers
[2*j
-1];
526 /* Insert the new number */
527 set
->numbers
[2*i
] = newnumber
[0];
528 set
->numbers
[2*i
+1] = newnumber
[1];
535 #define ensure(array, size, newlen, type) do { \
536 if ((newlen) > (size)) { \
537 (size) = (newlen) + 512; \
538 (array) = sresize((array), (size), type); \
542 static int setcmp(void *av
, void *bv
)
544 struct set
*a
= (struct set
*)av
;
545 struct set
*b
= (struct set
*)bv
;
548 if (a
->nnumbers
< b
->nnumbers
)
550 else if (a
->nnumbers
> b
->nnumbers
)
553 if (a
->flags
< b
->flags
)
555 else if (a
->flags
> b
->flags
)
558 for (i
= 0; i
< a
->nnumbers
; i
++) {
559 if (ratcmp(a
->numbers
+2*i
, <, b
->numbers
+2*i
))
561 else if (ratcmp(a
->numbers
+2*i
, >, b
->numbers
+2*i
))
568 static int outputcmp(void *av
, void *bv
)
570 struct output
*a
= (struct output
*)av
;
571 struct output
*b
= (struct output
*)bv
;
573 if (a
->number
< b
->number
)
575 else if (a
->number
> b
->number
)
581 static int outputfindcmp(void *av
, void *bv
)
584 struct output
*b
= (struct output
*)bv
;
588 else if (*a
> b
->number
)
594 static void addset(struct sets
*s
, struct set
*set
, int multiple
,
595 struct set
*prev
, int pa
, int po
, int pb
, int pr
)
598 int npaths
= (prev ? prev
->npaths
: 1);
600 assert(set
== s
->setlists
[s
->nsets
/ SETLISTLEN
] + s
->nsets
% SETLISTLEN
);
601 s2
= add234(s
->settree
, set
);
604 * New set added to the tree.
611 set
->npaths
= npaths
;
613 s
->nnumbers
+= 2 * set
->nnumbers
;
615 set
->nas
= set
->assize
= 0;
618 * Rediscovered an existing set. Update its npaths.
620 s2
->npaths
+= npaths
;
622 * And optionally enter it as an additional ancestor.
625 if (s2
->nas
>= s2
->assize
) {
626 s2
->assize
= s2
->nas
* 3 / 2 + 4;
627 s2
->as
= sresize(s2
->as
, s2
->assize
, struct ancestor
);
629 s2
->as
[s2
->nas
].prev
= prev
;
630 s2
->as
[s2
->nas
].pa
= pa
;
631 s2
->as
[s2
->nas
].po
= po
;
632 s2
->as
[s2
->nas
].pb
= pb
;
633 s2
->as
[s2
->nas
].pr
= pr
;
639 static struct set
*newset(struct sets
*s
, int nnumbers
, int flags
)
643 ensure(s
->setlists
, s
->setlistsize
, s
->nsets
/SETLISTLEN
+1, struct set
*);
644 while (s
->nsetlists
<= s
->nsets
/ SETLISTLEN
)
645 s
->setlists
[s
->nsetlists
++] = snewn(SETLISTLEN
, struct set
);
646 sn
= s
->setlists
[s
->nsets
/ SETLISTLEN
] + s
->nsets
% SETLISTLEN
;
648 if (s
->nnumbers
+ nnumbers
* 2 > s
->nnumberlists
* NUMBERLISTLEN
)
649 s
->nnumbers
= s
->nnumberlists
* NUMBERLISTLEN
;
650 ensure(s
->numberlists
, s
->numberlistsize
,
651 s
->nnumbers
/NUMBERLISTLEN
+1, int *);
652 while (s
->nnumberlists
<= s
->nnumbers
/ NUMBERLISTLEN
)
653 s
->numberlists
[s
->nnumberlists
++] = snewn(NUMBERLISTLEN
, int);
654 sn
->numbers
= s
->numberlists
[s
->nnumbers
/ NUMBERLISTLEN
] +
655 s
->nnumbers
% NUMBERLISTLEN
;
658 * Start the set off empty.
667 static int addoutput(struct sets
*s
, struct set
*ss
, int index
, int *n
)
669 struct output
*o
, *o2
;
672 * Target numbers are always integers.
674 if (ss
->numbers
[2*index
+1] != 1)
677 ensure(s
->outputlists
, s
->outputlistsize
, s
->noutputs
/OUTPUTLISTLEN
+1,
679 while (s
->noutputlists
<= s
->noutputs
/ OUTPUTLISTLEN
)
680 s
->outputlists
[s
->noutputlists
++] = snewn(OUTPUTLISTLEN
,
682 o
= s
->outputlists
[s
->noutputs
/ OUTPUTLISTLEN
] +
683 s
->noutputs
% OUTPUTLISTLEN
;
685 o
->number
= ss
->numbers
[2*index
];
688 o
->npaths
= ss
->npaths
;
689 o2
= add234(s
->outputtree
, o
);
691 o2
->npaths
+= o
->npaths
;
699 static struct sets
*do_search(int ninputs
, int *inputs
,
700 const struct rules
*rules
, int *target
,
701 int debug
, int multiple
)
706 const struct operation
*const *ops
= rules
->ops
;
708 s
= snew(struct sets
);
710 s
->nsets
= s
->nsetlists
= s
->setlistsize
= 0;
711 s
->numberlists
= NULL
;
712 s
->nnumbers
= s
->nnumberlists
= s
->numberlistsize
= 0;
713 s
->outputlists
= NULL
;
714 s
->noutputs
= s
->noutputlists
= s
->outputlistsize
= 0;
715 s
->settree
= newtree234(setcmp
);
716 s
->outputtree
= newtree234(outputcmp
);
720 * Start with the input set.
722 sn
= newset(s
, ninputs
, SETFLAG_CONCAT
);
723 for (i
= 0; i
< ninputs
; i
++) {
725 newnumber
[0] = inputs
[i
];
727 addtoset(sn
, newnumber
);
729 addset(s
, sn
, multiple
, NULL
, 0, 0, 0, 0);
732 * Now perform the breadth-first search: keep looping over sets
733 * until we run out of steam.
736 while (qpos
< s
->nsets
) {
737 struct set
*ss
= s
->setlists
[qpos
/ SETLISTLEN
] + qpos
% SETLISTLEN
;
743 printf("processing set:");
744 for (i
= 0; i
< ss
->nnumbers
; i
++) {
745 printf(" %d", ss
->numbers
[2*i
]);
746 if (ss
->numbers
[2*i
+1] != 1)
747 printf("/%d", ss
->numbers
[2*i
]+1);
753 * Record all the valid output numbers in this state. We
754 * can always do this if there's only one number in the
755 * state; otherwise, we can only do it if we aren't
756 * required to use all the numbers in coming to our answer.
758 if (ss
->nnumbers
== 1 || !rules
->use_all
) {
759 for (i
= 0; i
< ss
->nnumbers
; i
++) {
762 if (addoutput(s
, ss
, i
, &n
) && target
&& n
== *target
)
768 * Try every possible operation from this state.
770 for (k
= 0; ops
[k
] && ops
[k
]->perform
; k
++) {
771 if ((ops
[k
]->flags
& OPFLAG_NEEDS_CONCAT
) &&
772 !(ss
->flags
& SETFLAG_CONCAT
))
773 continue; /* can't use this operation here */
774 for (i
= 0; i
< ss
->nnumbers
; i
++) {
775 int jlimit
= (ops
[k
]->flags
& OPFLAG_UNARY ?
1 : ss
->nnumbers
);
776 for (j
= 0; j
< jlimit
; j
++) {
780 if (!(ops
[k
]->flags
& OPFLAG_UNARY
)) {
782 continue; /* can't combine a number with itself */
783 if (i
> j
&& ops
[k
]->commutes
)
784 continue; /* no need to do this both ways round */
786 if (!ops
[k
]->perform(ss
->numbers
+2*i
, ss
->numbers
+2*j
, n
))
787 continue; /* operation failed */
789 sn
= newset(s
, ss
->nnumbers
-1, ss
->flags
);
791 if (!(ops
[k
]->flags
& OPFLAG_KEEPS_CONCAT
))
792 sn
->flags
&= ~SETFLAG_CONCAT
;
794 for (m
= 0; m
< ss
->nnumbers
; m
++) {
795 if (m
== i
|| (!(ops
[k
]->flags
& OPFLAG_UNARY
) &&
798 sn
->numbers
[2*sn
->nnumbers
] = ss
->numbers
[2*m
];
799 sn
->numbers
[2*sn
->nnumbers
+ 1] = ss
->numbers
[2*m
+ 1];
803 if (ops
[k
]->flags
& OPFLAG_UNARY
)
804 pb
= sn
->nnumbers
+10;
808 pr
= addtoset(sn
, n
);
809 addset(s
, sn
, multiple
, ss
, pa
, po
, pb
, pr
);
812 printf(" %d %s %d ->", pa
, ops
[po
]->dbgtext
, pb
);
813 for (i
= 0; i
< sn
->nnumbers
; i
++) {
814 printf(" %d", sn
->numbers
[2*i
]);
815 if (sn
->numbers
[2*i
+1] != 1)
816 printf("/%d", sn
->numbers
[2*i
]+1);
830 static void free_sets(struct sets
*s
)
834 freetree234(s
->settree
);
835 freetree234(s
->outputtree
);
836 for (i
= 0; i
< s
->nsetlists
; i
++)
837 sfree(s
->setlists
[i
]);
839 for (i
= 0; i
< s
->nnumberlists
; i
++)
840 sfree(s
->numberlists
[i
]);
841 sfree(s
->numberlists
);
842 for (i
= 0; i
< s
->noutputlists
; i
++)
843 sfree(s
->outputlists
[i
]);
844 sfree(s
->outputlists
);
849 * Print a text formula for producing a given output.
851 void print_recurse(struct sets
*s
, struct set
*ss
, int pathindex
, int index
,
852 int priority
, int assoc
, int child
);
853 void print_recurse_inner(struct sets
*s
, struct set
*ss
,
854 struct ancestor
*a
, int pathindex
, int index
,
855 int priority
, int assoc
, int child
)
857 if (a
->prev
&& index
!= a
->pr
) {
861 * This number was passed straight down from this set's
862 * predecessor. Find its index in the previous set and
869 if (pi
>= min(a
->pa
, a
->pb
)) {
871 if (pi
>= max(a
->pa
, a
->pb
))
874 print_recurse(s
, a
->prev
, pathindex
, pi
, priority
, assoc
, child
);
875 } else if (a
->prev
&& index
== a
->pr
&&
876 s
->ops
[a
->po
]->display
) {
878 * This number was created by a displayed operator in the
879 * transition from this set to its predecessor. Hence we
880 * write an open paren, then recurse into the first
881 * operand, then write the operator, then the second
882 * operand, and finally close the paren.
885 int parens
, thispri
, thisassoc
;
888 * Determine whether we need parentheses.
890 thispri
= s
->ops
[a
->po
]->priority
;
891 thisassoc
= s
->ops
[a
->po
]->assoc
;
892 parens
= (thispri
< priority
||
893 (thispri
== priority
&& (assoc
& child
)));
898 if (s
->ops
[a
->po
]->flags
& OPFLAG_UNARYPFX
)
899 for (op
= s
->ops
[a
->po
]->text
; *op
; op
++)
902 print_recurse(s
, a
->prev
, pathindex
, a
->pa
, thispri
, thisassoc
, 1);
904 if (!(s
->ops
[a
->po
]->flags
& OPFLAG_UNARYPFX
))
905 for (op
= s
->ops
[a
->po
]->text
; *op
; op
++)
908 if (!(s
->ops
[a
->po
]->flags
& OPFLAG_UNARY
))
909 print_recurse(s
, a
->prev
, pathindex
, a
->pb
, thispri
, thisassoc
, 2);
915 * This number is either an original, or something formed
916 * by a non-displayed operator (concatenation). Either way,
917 * we display it as is.
919 printf("%d", ss
->numbers
[2*index
]);
920 if (ss
->numbers
[2*index
+1] != 1)
921 printf("/%d", ss
->numbers
[2*index
+1]);
924 void print_recurse(struct sets
*s
, struct set
*ss
, int pathindex
, int index
,
925 int priority
, int assoc
, int child
)
927 if (!ss
->a
.prev
|| pathindex
< ss
->a
.prev
->npaths
) {
928 print_recurse_inner(s
, ss
, &ss
->a
, pathindex
,
929 index
, priority
, assoc
, child
);
932 pathindex
-= ss
->a
.prev
->npaths
;
933 for (i
= 0; i
< ss
->nas
; i
++) {
934 if (pathindex
< ss
->as
[i
].prev
->npaths
) {
935 print_recurse_inner(s
, ss
, &ss
->as
[i
], pathindex
,
936 index
, priority
, assoc
, child
);
939 pathindex
-= ss
->as
[i
].prev
->npaths
;
943 void print(int pathindex
, struct sets
*s
, struct output
*o
)
945 print_recurse(s
, o
->set
, pathindex
, o
->index
, 0, 0, 0);
949 * gcc -g -O0 -o numgame numgame.c -I.. ../{malloc,tree234,nullfe}.c -lm
951 int main(int argc
, char **argv
)
953 int doing_opts
= TRUE
;
954 const struct rules
*rules
= NULL
;
955 char *pname
= argv
[0];
956 int got_target
= FALSE
, target
= 0;
957 int numbers
[10], nnumbers
= 0;
959 int pathcounts
= FALSE
;
960 int multiple
= FALSE
;
961 int debug_bfs
= FALSE
;
971 if (doing_opts
&& *p
== '-') {
974 if (!strcmp(p
, "-")) {
977 } else if (*p
== '-') {
979 if (!strcmp(p
, "debug-bfs")) {
982 fprintf(stderr
, "%s: option '--%s' not recognised\n",
985 } else while (*p
) switch (c
= *p
++) {
987 rules
= &rules_countdown
;
993 rules
= &rules_four4s
;
996 rules
= &rules_anythinggoes
;
1013 } else if (--argc
) {
1016 fprintf(stderr
, "%s: option '-%c' expects an"
1017 " argument\n", pname
, c
);
1029 fprintf(stderr
, "%s: option '-%c' not"
1030 " recognised\n", pname
, c
);
1034 if (nnumbers
>= lenof(numbers
)) {
1035 fprintf(stderr
, "%s: internal limit of %d numbers exceeded\n",
1036 pname
, lenof(numbers
));
1039 numbers
[nnumbers
++] = atoi(p
);
1045 fprintf(stderr
, "%s: no rule set specified; use -C,-B,-D,-A\n", pname
);
1050 fprintf(stderr
, "%s: no input numbers specified\n", pname
);
1054 s
= do_search(nnumbers
, numbers
, rules
, (got_target ?
&target
: NULL
),
1055 debug_bfs
, multiple
);
1058 o
= findrelpos234(s
->outputtree
, &target
, outputfindcmp
,
1062 o
= findrelpos234(s
->outputtree
, &target
, outputfindcmp
,
1066 assert(start
!= -1 || limit
!= -1);
1069 else if (limit
== -1)
1074 limit
= count234(s
->outputtree
);
1077 for (i
= start
; i
< limit
; i
++) {
1080 o
= index234(s
->outputtree
, i
);
1082 sprintf(buf
, "%d", o
->number
);
1085 sprintf(buf
+ strlen(buf
), " [%d]", o
->npaths
);
1087 if (got_target
|| verbose
) {
1095 for (j
= 0; j
< npaths
; j
++) {
1096 printf("%s = ", buf
);
1101 printf("%s\n", buf
);