#!/usr/bin/env ruby
# Author: Stefan <orangenhain@gmx.eu>
#  WARNING: YOU DON'T HAVE PERMISSION YET!!
# Given 6,6,5,2, make 17

def solve(numbers, target)
  numbers.permutation.to_a.uniq.each do |numbers_permutation|
    [:+, :- , :*, :/].repeated_permutation(numbers_permutation.length - 1) do |operations_permutation|
      result = numbers_permutation.first
      operations_permutation.each_with_index do |operation, idx|
        result = result.send(operation, numbers_permutation[idx + 1])
      end
    
      if result == target
        p numbers_permutation
        p operations_permutation
        puts
      end
    end
  end
end

# solve [6, 3], 2
# solve [6, 6, 5, 2], 24
# solve [6, 6, 5, 4], 17
solve [6, 6, 5, 2], 17

# MJD notes: 
#   This one has fails on division by zero
#     (Unless you use FP constants)
#   And fails to find the 17 solution
#     (Because of integer division)
#   And fails to find the 41 solution
