Zebra puzzle: Difference between revisions

m
→‎{{header|C}}: make gcc -pedantic happy
(→‎{{header|C}}: reduce both C and Perl source code size, though the C source source is unreadable without indent (and cpp))
m (→‎{{header|C}}: make gcc -pedantic happy)
Line 50:
return -1; }\n";
 
print "FIND_BY($_);\n" for (keys %props);
 
local $" = ", ";
Line 104:
my $idx = 0;
my $return1 = $find_all_solutions ? "" : "return 1";
print << "SNIPPET";
#define TRY(a, b, c, d, p, n) \\
if ((b = a d) >= 0 && b < N_ITEMS) { \\
Line 112:
house[b].p = 0; \\
}}
";
SNIPPET
 
while (@conds) {
Line 127:
my $this = "work".++$idx;
 
print << "SNIPPET";
/* condition pair($c1, $c2, [@$diff]) */
int $this(void) {
Line 134:
if (a != -1 && b != -1) {
switch(b - a) {
";
SNIPPET
print "case $_: " for @$diff;
print "return $next(); default: return 0; }\n } if (a != -1) {";
Line 140:
print " return 0; } if (b != -1) {";
print "TRY(b, a, $c1, -($_), $p1, $next);" for @$diff;
print << "SNIPPET";
return 0; }
/* neither condition is set; try all possibles */
Line 146:
if (house[a].$p1) continue;
house[a].$p1 = $c1;
";
SNIPPET
 
print "TRY(a, b, $c2, +($_), $p2, $next);" for @$diff;
Line 194:
# the logic. It's here just to make sure the code will insert a zebra
# somewhere in the table (after all other conditions are met) so the
# final print-out shows it. The(the C code can be better structured, but
# meh, I ain't reading it, so who cares).
pair(zebra, AEnglisk, [ -4 .. 4 ]);
 
# write C code. If it's ugly to you: I didn't write it; Perl did.
make_c;</lang>
output (ran as <code>perl test.pl | gcc -Wall -x c -; ./a.out</code>):<pre>
Anonymous user