How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the D programming language
Table of Contents
Problem Statement
Replace a, b, c, d, e, f, and g with the decimal digits LOW ───► HIGH such that the sum of the letters inside of each of the four large squares add up to the same sum. Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the D programming language
Source code in the d programming language
import std.stdio;
void main() {
fourSquare(1,7,true,true);
fourSquare(3,9,true,true);
fourSquare(0,9,false,false);
}
void fourSquare(int low, int high, bool unique, bool print) {
int count;
if (print) {
writeln("a b c d e f g");
}
for (int a=low; a<=high; ++a) {
for (int b=low; b<=high; ++b) {
if (!valid(unique, a, b)) continue;
int fp = a+b;
for (int c=low; c<=high; ++c) {
if (!valid(unique, c, a, b)) continue;
for (int d=low; d<=high; ++d) {
if (!valid(unique, d, a, b, c)) continue;
if (fp != b+c+d) continue;
for (int e=low; e<=high; ++e) {
if (!valid(unique, e, a, b, c, d)) continue;
for (int f=low; f<=high; ++f) {
if (!valid(unique, f, a, b, c, d, e)) continue;
if (fp != d+e+f) continue;
for (int g=low; g<=high; ++g) {
if (!valid(unique, g, a, b, c, d, e, f)) continue;
if (fp != f+g) continue;
++count;
if (print) {
writeln(a,' ',b,' ',c,' ',d,' ',e,' ',f,' ',g);
}
}
}
}
}
}
}
}
if (unique) {
writeln("There are ", count, " unique solutions in [",low,",",high,"]");
} else {
writeln("There are ", count, " non-unique solutions in [",low,",",high,"]");
}
}
bool valid(bool unique, int needle, int[] haystack ...) {
if (unique) {
foreach (value; haystack) {
if (needle == value) {
return false;
}
}
}
return true;
}
You may also check:How to resolve the algorithm Queue/Usage step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Test integerness step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Disarium numbers step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the IDL programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the NewLISP programming language