Task: Solve the no connection puzzle




Go: package main

import (
	"fmt"
	"strings"
)

func main() {
	p, tests, swaps := Solution()
	fmt.Println(p)
	fmt.Println("Tested", tests, "positions and did", swaps, "swaps.")
}



const conn = `
       A   B
      /|\ /|\
     / | X | \
    /  |/ \|  \
   C - D - E - F
    \  |\ /|  /
     \ | X | /
      \|/ \|/
       G   H`

var connections = []struct{ a, b int }{
	{0, 2}, {0, 3}, {0, 4}, 
	{1, 3}, {1, 4}, {1, 5}, 
	{6, 2}, {6, 3}, {6, 4}, 
	{7, 3}, {7, 4}, {7, 5}, 
	{2, 3}, {3, 4}, {4, 5}, 
}

type pegs [8]int




func (p *pegs) Valid() bool {
	for _, c := range connections {
		if absdiff(p[c.a], p[c.b]) <= 1 {
			return false
		}
	}
	return true
}





func Solution() (p *pegs, tests, swaps int) {
	var recurse func(int) bool
	recurse = func(i int) bool {
		if i >= len(p)-1 {
			tests++
			return p.Valid()
		}
		
		for j := i; j < len(p); j++ {
			swaps++
			p[i], p[j] = p[j], p[i]
			if recurse(i + 1) {
				return true
			}
			p[i], p[j] = p[j], p[i]
		}
		return false
	}
	p = &pegs{1, 2, 3, 4, 5, 6, 7, 8}
	recurse(0)
	return
}

func (p *pegs) String() string {
	return strings.Map(func(r rune) rune {
		if 'A' <= r && r <= 'H' {
			return rune(p[r-'A'] + '0')
		}
		return r
	}, conn)
}

func absdiff(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}







Fortran: 



program no_connection_puzzle

  implicit none

  
  integer, parameter :: a = 1
  integer, parameter :: b = 2
  integer, parameter :: c = 3
  integer, parameter :: d = 4
  integer, parameter :: e = 5
  integer, parameter :: f = 6
  integer, parameter :: g = 7
  integer, parameter :: h = 8

  integer :: holes(a:h)

  call find_solutions (holes, a)

contains

  recursive subroutine find_solutions (holes, current_hole_index)
    integer, intent(inout) :: holes(a:h)
    integer, intent(in) :: current_hole_index

    integer :: peg_number

    
    
    do peg_number = 1, 8
       holes(current_hole_index) = peg_number
       if (satisfies_the_constraints (holes, current_hole_index)) then
          if (current_hole_index == h) then
             call print_solution (holes)
             write (*, '()')
          else
             call find_solutions (holes, current_hole_index + 1)
          end if
       end if
    end do
  end subroutine find_solutions

  function satisfies_the_constraints (holes, i) result (satisfies)
    integer, intent(inout) :: holes(a:h)
    integer, intent(in) :: i    
    logical :: satisfies

    integer :: j

    
    
    satisfies = all (holes(a : i - 1) /= holes(i))

    if (satisfies) then
       
       
       do j = i + 1, h
          holes(j) = 100 * j
       end do

       
       satisfies = 1 < abs (holes(a) - holes(c)) .and.     &
            &      1 < abs (holes(a) - holes(d)) .and.     &
            &      1 < abs (holes(a) - holes(e)) .and.     &
            &      1 < abs (holes(c) - holes(g)) .and.     &
            &      1 < abs (holes(d) - holes(g)) .and.     &
            &      1 < abs (holes(e) - holes(g)) .and.     &
            &      1 < abs (holes(b) - holes(d)) .and.     &
            &      1 < abs (holes(b) - holes(e)) .and.     &
            &      1 < abs (holes(b) - holes(f)) .and.     &
            &      1 < abs (holes(d) - holes(h)) .and.     &
            &      1 < abs (holes(e) - holes(h)) .and.     &
            &      1 < abs (holes(f) - holes(h)) .and.     &
            &      1 < abs (holes(c) - holes(d)) .and.     &
            &      1 < abs (holes(d) - holes(e)) .and.     &
            &      1 < abs (holes(e) - holes(f))
    end if
  end function satisfies_the_constraints

  subroutine print_solution (holes)
    integer, intent(in) :: holes(a:h)

    write (*, '(I5, I4)') holes(a), holes(b)
    write (*, '("   /│\ /│\")')
    write (*, '("  / │ X │ \")')
    write (*, '(" /  │/ \│  \")')
    write (*, '(3(I1, "───"), I1)') holes(c), holes(d), holes(e), holes(f)
    write (*, '(" \  │\ /│  /")')
    write (*, '("  \ │ X │ /")')
    write (*, '("   \│/ \│/")')
    write (*, '(I5, I4)') holes(g), holes(h)
  end subroutine print_solution

end program no_connection_puzzle