Skip to content

Instantly share code, notes, and snippets.

@takehiko
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takehiko/bf4c78a757764c2f5c76 to your computer and use it in GitHub Desktop.
Save takehiko/bf4c78a757764c2f5c76 to your computer and use it in GitHub Desktop.
triangle puzzle solver
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# TrianglePuzzleSolver.rb
# by takehikom
# inspired by:
# http://www.huffingtonpost.jp/satoshi-kagimoto/triangle-puzzle_b_5590269.html
module TrianglePuzzle
class Table
def initialize(ary)
@array = ary.dup
end
attr_accessor :array
def print_table(flag_sum = false)
puts " #{@array[9]}"
puts " #{@array[7]} #{@array[8]}"
puts " #{@array[4]} #{@array[5]} #{@array[6]}"
puts "#{@array[0]} #{@array[1]} #{@array[2]} #{@array[3]}"
if flag_sum
a = get_six_sums
puts "left[1] = #{a.shift}"
puts "left[2] = #{a.shift}"
puts "down[1] = #{a.shift}"
puts "down[2] = #{a.shift}"
puts "right[1] = #{a.shift}"
puts "right[2] = #{a.shift}"
end
end
def get_six_sums
[@array[7] + @array[8],
@array[4] + @array[5] + @array[6],
@array[4] + @array[1],
@array[7] + @array[5] + @array[2],
@array[8] + @array[5] + @array[1],
@array[6] + @array[2]]
end
alias :sums :get_six_sums
def valid?(flag_msg = false)
if @array.length != 10
flag_msg ? "wrong size" : false
elsif @array.map {|item| [1, 2, 3, 4, 5].index(item).nil?}.uniq != [false]
flag_msg ? "invalid value" : false
elsif @array.values_at(0, 1, 2, 3, 5).sort != [1, 2, 3, 4, 5] ||
@array.values_at(0, 4, 5, 7, 9).sort != [1, 2, 3, 4, 5] ||
@array.values_at(3, 5, 6, 8, 9).sort != [1, 2, 3, 4, 5]
flag_msg ? "wrong permutation" : false
else
flag_msg ? "OK" : true
end
end
end
class Searcher
def initialize; end
attr_reader :all, :hash
def start(flag_print = false)
@all = []
@hash = Hash.new
tab_cnt = 0
[1, 2, 3, 4, 5].permutation do |a|
ary = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
ary[0] = a[0]
ary[1] = a[1]
ary[2] = a[2]
ary[3] = a[3]
ary[5] = a[4]
([1, 2, 3, 4, 5] - [ary[0], ary[3], ary[5]]).each do |b|
ary[9] = b
([1, 2, 3, 4, 5] - [ary[0], ary[5], ary[9]]).permutation do |c|
ary[4] = c[0]
ary[7] = c[1]
([1, 2, 3, 4, 5] - [ary[3], ary[5], ary[9]]).permutation do |d|
ary[6] = d[0]
ary[8] = d[1]
tab = TrianglePuzzle::Table.new(ary)
# tab.valid? || raise
tab_cnt += 1
if flag_print
puts "==== Table #{tab_cnt} ===="
tab.print_table
puts "sums: #{tab.get_six_sums.join(', ')}"
puts
end
@all << ary
k = tab.get_six_sums.join(",")
if @hash.key?(k)
@hash[k] << tab
else
@hash[k] = [tab]
end
end
end
end
end
end
end
end
if __FILE__ == $0
s = TrianglePuzzle::Searcher.new
s.start($DEBUG)
puts "==== 10,7,7,9,11,7 ===="
s.hash["10,7,7,9,11,7"].each do |tab|
tab.print_table(true)
end
puts
puts "==== 6,11,6,8,6,7 ===="
s.hash["6,11,6,8,6,7"].each do |tab|
tab.print_table(true)
end
puts
if $DEBUG
h = s.hash
h.keys.sort_by {|item| item.split(/,/).map {|num| num.to_i}}.each do |k|
puts "====== values=#{k} ======"
h[k].each_with_index do |tab, i|
puts "(case #{i + 1})"
tab.print_table(true)
end
puts
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment