24 game/Solve: Difference between revisions
m
→{{header|Sidef}}: added one more implementation
m (→{{header|Sidef}}: changed the output sample) |
m (→{{header|Sidef}}: added one more implementation) |
||
Line 4,807:
=={{header|Sidef}}==
'''With evaluation:'''
<lang ruby>var formats = [
'((%d %s %d) %s %d) %s %d',
Line 4,828 ⟶ 4,830:
var n = input.split.map{.to_i};
▲ var numbers = [];
n.permute {▼
numbers.append(_);▼
};▼
formats.each { |format|
Line 4,843 ⟶ 4,841:
}
}
}
'''Without evaluation:'''
<lang ruby>var formats = [
{|a,b,c|
Hash.new(
func => {|d,e,f,g| ((d.$a(e)).$b(f)).$c(g) }.copy,
format => "((%d #{a} %d) #{b} %d) #{c} %d"
)
{|a,b,c|
Hash.new(
func => {|d,e,f,g| (d.$a((e.$b(f)))).$c(g) }.copy,
format => "(%d #{a} (%d #{b} %d)) #{c} %d",
)
},
{|a,b,c|
Hash.new(
func => {|d,e,f,g| (d.$a(e)).$b(f.$c(g)) }.copy,
format => "(%d #{a} %d) #{b} (%d #{c} %d)",
)
},
{|a,b,c|
Hash.new(
func => {|d,e,f,g| (d.$a(e)).$b(f.$c(g)) }.copy,
format => "(%d #{a} %d) #{b} (%d #{c} %d)",
)
},
{|a,b,c|
Hash.new(
func => {|d,e,f,g| d.$a(e.$b(f.$c(g))) }.copy,
format => "%d #{a} (%d #{b} (%d #{c} %d))",
)
},
];
var op = %w( + - * / );
var blocks = op.map { |a| op.map { |b| op.map { |c| formats.map { |format|
format(a,b,c)
}}}}.flatten;
loop {
var input = Sys.scanln("Enter four integers or 'q' to exit: ");
input == 'q' && break;
input ~~ /^[1-9]\h+[1-9]\h+[1-9]\h+[1-9]$/ || (
say "Invalid input!"; next;
);
var n = input.split.map{.to_i};
blocks.each { |block|
if (block[:func].call(n...) == 24) {
say (block[:format] % (n...));
}
}
}
}</lang>
{{out}}
|