A puzzle

B

Bill Guindon

given 5 variables... incoming, minimium, current, maximum, reserve

produce a text file that shows every possible map of the variable relations.

Something along the lines of this:

minimum < current < hidden = incoming = reserve

For cases such as the above that have two or more equal variables, the
names should be sorted alphabetically, and duplicates removed:

keep: minimum < current < hidden = incoming = reserve

drop: minimum < current < incoming = hidden = reserve
drop: minimum < current < incoming = reserve = hidden
etc.

btw, if you think this is quiz worthy, don't post possible answers --
but feel free to send "solutions" or suggestions to me off list.
 
W

William James

Bill said:
given 5 variables... incoming, minimium, current, maximum, reserve

produce a text file that shows every possible map of the variable relations.

Something along the lines of this:

minimum < current < hidden = incoming = reserve

For cases such as the above that have two or more equal variables, the
names should be sorted alphabetically, and duplicates removed:

keep: minimum < current < hidden = incoming = reserve

drop: minimum < current < incoming = hidden = reserve
drop: minimum < current < incoming = reserve = hidden
etc.

This program produces 541 variations.

$rel_hash = { '0' => '=', '1' => '<' }
$var_hash = { '1' => 'incoming', '3' => 'minimium',
'0' =>'current', '2' =>'maximum', '4' =>'reserve' }

def generate( used, unused )
if 0==unused.size
all_relations( used )
else
unused.split(//).each {|c|
generate( used+c, unused.delete(c) )
}
end
end

def all_relations( s )
num = s.size - 1
(0 .. 2**(num)-1).each { |x|
show( s, sprintf( "%0#{num}b", x ) )
}
end

def ordered( s, relations )
relations.split(//).each_with_index{ |c,i|
return nil if '0'==c and s > s[i+1]
}
return true
end

def show( s, relations )
if ordered( s, relations )
puts s.to_var_names( relations )
end
end

class String
def to_var_names( relations )
out = ''
self.split(//).each_with_index{ |c,i|
out += $var_hash[ c ]
out += $rel_hash[relations[i..i]] if i< relations.size
}
return out
end
end
generate( '', '01234' )
 
C

Cs. Henk

--ylS2wUBXLOxYXZFQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

given 5 variables... incoming, minimium, current, maximum, reserve

produce a text file that shows every possible map of the variable relations.

Something along the lines of this:

minimum < current < hidden = incoming = reserve

I've sent my solution already offlist, but now I see William James sent
one publicly, so I make it public as well. His solution gives 541
relation, I have 1305... interesting, which could be the good one?

Csaba

--ylS2wUBXLOxYXZFQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="puzzle.rb"


def onp n # as in ordered number theoretic partition
n.zero? and return [[]]
res = []
(1..n).each { |k|
onp(n-k).each { |a|
res << [k]+a
}
}
res
end

def perm n
n.zero? and return [[0]]
res = []
perm(n-1).each{ |pe|
(0..n).each{|j|
res << pe.dup.insert(j,n)
}
}
res
end

def show(n)
onp(n).each{|o|
perm(n-1).each{|pe|
catch:)bad) {
b=[]
o.each{|i|
b << []
i.times{b[-1] << pe.shift}
((q=b[-1]).size-2).times{|i| q < q[i+1] or throw :bad}
}
yield b
}
}
}
end

if $0 == __FILE__
words=%w(minimum current hidden incoming reserve).sort
show(5) { |db|
puts db.map{|e|
e.map{|i| words}.join " = "
}.join(" < ")
}
end

--ylS2wUBXLOxYXZFQ--
 
P

Pit Capitain

Cs. Henk said:
I've sent my solution already offlist, but now I see William James sent
one publicly, so I make it public as well. His solution gives 541
relation, I have 1305... interesting, which could be the good one?

I get 541, too. Your code seems to not pay attention to the following
restriction:
minimum < current < hidden = incoming = reserve

For cases such as the above that have two or more equal variables, the
names should be sorted alphabetically, and duplicates removed:

Regards,
Pit
 
P

Pit Capitain

Pit said:
I get 541, too. Your code seems to not pay attention to the following
restriction:
...

Looking at your code I see that the restriction *IS* handled there. The
check has to be repeated one more time, though, so it should be

((q=b[-1]).size-1).times

Then you get the same 541 results.

Regards,
Pit
 
B

Bill Guindon

Pit said:
I get 541, too. Your code seems to not pay attention to the following
restriction:
...

Looking at your code I see that the restriction *IS* handled there. The
check has to be repeated one more time, though, so it should be

((q=b[-1]).size-1).times

Then you get the same 541 results.

Regards,
Pit

Got some great replies to this, on and off-list. Thanks to all who
took a shot at it. You all managed to solve that last part (which had
me stumped) of eliminating the redundancies.

Hope you enjoyed it, was asked on another list, and just seemed like a
fun one :).
 
C

Cs. Henk

Pit said:
I get 541, too. Your code seems to not pay attention to the following
restriction:
...

Looking at your code I see that the restriction *IS* handled there. The
check has to be repeated one more time, though, so it should be

((q=b[-1]).size-1).times

Then you get the same 541 results.

Yes, you are right! Thanks for the enlightenment!

Csaba
 
B

Brian Schröder

given 5 variables... incoming, minimium, current, maximum, reserve

produce a text file that shows every possible map of the variable relations.

Something along the lines of this:

minimum < current < hidden = incoming = reserve

For cases such as the above that have two or more equal variables, the
names should be sorted alphabetically, and duplicates removed:

keep: minimum < current < hidden = incoming = reserve

drop: minimum < current < incoming = hidden = reserve
drop: minimum < current < incoming = reserve = hidden
etc.

btw, if you think this is quiz worthy, don't post possible answers --
but feel free to send "solutions" or suggestions to me off list.

A little quiz is always nice. Attatched my solution, maybe not the
best readable but nice and easy.

best regards,

Brian

---8<----8<----
variables = %w(incoming minimum current maximum reserve)
values = (0...variables.length).to_a

def mapto(variables, values)
return [[]] if variables.empty?
result = []
var, *variables = *variables
values.each do | val |
mapto(variables, values).each do | mapping |
result << (mapping << [val, var])
end
end
result
end

maps = mapto(variables, values).map{ | map |
map.sort.inject(){ | (lv, lt), (rv, rt) | [rv, "#{lt} #{lv<rv ? '<' :
'='} #{rt}"] }[1]
}.sort.uniq

puts maps
---8<----8<----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top