What is this program?

Please take a look at this image below for brief instruction on how to complete an addition Fubuki puzzle

An example of fubuki puzzles

An example of fubuki puzzles

This program is meant to solve a fubuki puzzle as above. Users will be prompted to input the selected position, its selected value, and the R and C values. Take the left puzzle as an example:

How is the program run?

The program is uses the combinat library where the permutations function is found.

To install this package, enter into R console install.packages("combinat")

First thing first, we access the library by writing this at the top of your project library(combinat)

The program implements the permutations of 8 numbers (excluded the selected value from earlier) to find all the possible permutations that when added together, would match the R and C values. During this time, the selected value and its position are added back into the array of permutations. Once a match is found, the solution is printed out as a 3X3 matrix along with the total number of solutions.

Once the user has gathered all the values, the program will:

  1. We have a function called fubukiSolver1(pos, selectedVal, r1, r2, r3, c1, c2, c3) that takes in the user inputs.
  2. Next, the function carries out the permutation process:
numList <- (1:9)[-selectedVal]  ## Create a list from 1 to 9 excluding the value selected 

totalPerm <- factorial(8)       ## Create another list to store all the possibilites from 8!
permList <- permn(numList)      ## Create a list for all the permutation from numList

final9 <- numeric(9)            ## A list that stores 9 numbers
remainPositions<- (1:9)[-pos]   ## Another list storing all the positions except the chosen one
  1. We define a variable n to keep track of the solutions
  1. Next, a for loop to traverse the list of permutations to check for a match
for (i in 1:totalPerm){                         ##To traverse 8! elements
    final9[pos] <- selectedVal                  ##Plug in the selected value to the position chosen
    final9[remainPositions] <- permList[[i]]    ##Put in a permutation to the remaining positions 
                                                  except the pre-filled position

    Row1 <- sum(final9[1:3])                    ##Compute the sum to be cross-checked with the R and C values
    Row2 <- sum(final9[4:6])
    Row3 <- sum(final9[7:9])

    Col1 <- sum(final9[c(1,4,7)])           
    Col2 <- sum(final9[c(2,5,8)])
    Col3 <- sum(final9[c(3,6,9)])

    if( Row1 == r1 &&                           ##Condition using AND operator to find the sums that 
                                                match the R and C values 
        Row2 == r2 &&
        Row3 == r3 &&
        Col1 == c1 &&
        Col2 == c2 &&
        Col3 == c3 ){
      
      n<-n+1                                    ##If a match is found, increment n for number of solutions
      
      if (n == 1){                              #When 1 or more solutions, output a matrix
        output = matrix(final9, nrow=3, ncol=3, byrow=TRUE)
      }
      else if (n > 1){
        output = rbind(output, matrix(final9, nrow=3, ncol=3, byrow=TRUE))
      }
    }

  1. Next, we determine the return statement. Our objective is to have it print out the total number of solutions and the following matrix of results
  if (n >= 1){                    
    finOutput <- array(NA, c(3,3,n))                    ##Assign an empty array to variable `finOutput` 
    
    for (i in 1:n) {                                    ##Loop through n and output the amount of matrices
      finOutput[,,i]<- output[((i-1)*3+1):((i-1)*3+3),]
    }
    return (finOutput)
  }
}
  1. Now that we have the output of the solutions, Let’s define another function that will output the total number of solutions

Let’s call fubukiSolutions(valInput) that takes in a value, which is the output from fubukiSolver1()

In the function

  result <- ""
  
  if (length(valInput) != 0){
    result <- sprintf("Total solution(s) are: %d", length(valInput)/9 )
    return (result)
    
    
  }
  else {
    return ("There is no solution")
  }

Let’s try it out!

Let’s use the values as in the left puzzle in the picture above:

We use fubukiSolutions() function and it outputs a statement with the total number of solutions

fubukiSolutions(fubukiSolver1(1, 3, 16, 22, 7, 11, 16, 18))
## [1] "Total solution(s) are: 4"

Next, we use fubukiSolver1() to output the solutions

fubukiSolver1(pos = 1, selectedVal = 3, r1=16, r2=22, r3=7, c1=11, c2=16, c3=18)
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    3    5    8
## [2,]    7    9    6
## [3,]    1    2    4
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    3    8    5
## [2,]    7    6    9
## [3,]    1    2    4
## 
## , , 3
## 
##      [,1] [,2] [,3]
## [1,]    3    5    8
## [2,]    6    7    9
## [3,]    2    4    1
## 
## , , 4
## 
##      [,1] [,2] [,3]
## [1,]    3    8    5
## [2,]    6    7    9
## [3,]    2    1    4

Thank you for checking out my report!

This is a project for my Data Science internship at the National Heart, Lung, and Blood Institute at the National Institutes of Health in Bethesda, MD

If you would like to experiment more on the interactive Shiny platform, please visit here

If you have any questions, please email me