How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the jq 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 jq programming language

Source code in the jq programming language

# Generate a stream of all the permutations of the input array
def permutations:
  if length == 0 then []
  else
    range(0;length) as $i
    | [.[$i]] + (del(.[$i])|permutations)
  end ;

# Permutations of a ... n inclusive
def permutations(a;n):
  [range(a;n+1)] | permutations;

# value of a box
# Input: the table of values
def valueOfBox($box):
  [ .[ $box[] ]] | add;

def allEqual($boxes):
  . as $values
  | valueOfBox($boxes[0]) as $sum
  | all($boxes[1:][]; . as $box | $values | valueOfBox($box) == $sum);

def combinations($m; $n; $size):
  [range(0; $size) | [range($m; $n)]] | combinations;

def count(s): reduce s as $x (null; .+1);

# a=0, b=1, etc
def boxes: [[0,1], [1,2,3], [3,4,5], [5,6]];

def tasks:
  "1 to 7:",
  (permutations(1;7) | select(allEqual(boxes))),
  "\n3 to 9:",
  (permutations(3;9) | select(allEqual(boxes))),
  "\n0 to 9:\n\(count(permutations(0;9) | select(allEqual(boxes))))",
  "\nThere are \(count(combinations(0;10;7) | select(allEqual(boxes)))) solutions for 0 to 9 with replacement."
;

tasks

# rings/3 assumes that each box (except for the last) has exactly one overlap with its successor.
# Input: ignored.
# Output: a stream of solutions, i.e. a stream of arrays.
# $boxes is an array of boxes, each box being a flat array.
# $min and $max define the range of permissible values of items in the boxes (inclusive)
def rings($boxes; $min; $max):

  def inrange: $min <= . and . <= $max;
 
  # The following helper function deals with the case when the global per-box sum ($sum) is known.
  # Input: an array representing the solution so far, or null.
  # Output: the input plus the solution corresponding to the first argument.
  # $this is the sum of the previous items in the first box, or 0.
  def solve($boxes; $this; $sum):
  
    # The following is a helper function for handling the case when:
    # *  $sum is known
    # *  $boxes[0] | length == 1, and
    # *  $boxes|length>1
    def lastInBox($boxes; $this):
      . as $in
      | ($boxes[1:] | (.[0] |= .[1:])) as $bx
      # the first entry in the next box must be the same:
      | ($sum - $this) as $next
      | select($next | inrange)
      | (. + [$next]) | solve( $bx; $next; $sum) ;

    . as $in
    | if $boxes|length == 0 then $in
      else $boxes[0] as $box
      | if $box|length == 0
	then solve( $boxes[1:]; 0; $sum )
        elif $box|length == 1
        # is this the last box?
        then if $boxes|length == 1
             then ($sum - $this) as $next
  	     | select($next | inrange)
  	     | $in + [$next]
             else lastInBox($boxes; $this)
             end
        else # $box|length > 1
        range($min; $max + 1) as $first
        | select( ($this + $first) <= $sum)
        | ($in + [$first]) | solve( [$box[1:]] + $boxes[1:]; $this + $first; $sum)
        end
      end ;
  
  . as $in
  | $boxes[0] as $box
  | ($boxes[1:] | .[0] |= .[1:]) as $bx
  | [range(0; $box|length) | [range($min; $max + 1)]]
  | combinations
  | solve($bx; .[-1]; add) ;

def count(s): reduce s as $x (null; .+1);

# a=0, b=1, etc
def boxes: [[0,1], [1,2,3], [3,4,5], [5,6]];

count(rings(boxes; 0; 9))

  

You may also check:How to resolve the algorithm Inheritance/Single step by step in the Ruby programming language
You may also check:How to resolve the algorithm Additive primes step by step in the PILOT programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm MD4 step by step in the Ada programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the Clojure programming language