Nonoblock: Difference between revisions

alphabetize, minor clean-up
m (→‎{{header|Perl}}: lightly edited for clarity)
(alphabetize, minor clean-up)
Line 62:
* The blog post [http://paddy3118.blogspot.co.uk/2014/03/nonogram-puzzle-solver-part-1.html Nonogram puzzle solver (part 1)] Inspired this task and donated its [[Nonoblock#Python]] solution.
<br><br>
 
=={{header|C sharp}}==
This solution uses a StringBuilder. Spaces are moved from right to left and the problem is then solved recursively.
<lang csharp>using System;
using System.Linq;
using System.Text;
 
public static class Nonoblock
{
public static void Main() {
Positions(5, 2,1);
Positions(5);
Positions(10, 8);
Positions(15, 2,3,2,3);
Positions(5, 2,3);
}
 
public static void Positions(int cells, params int[] blocks) {
if (cells < 0 || blocks == null || blocks.Any(b => b < 1)) throw new ArgumentOutOfRangeException();
Console.WriteLine($"{cells} cells with [{string.Join(", ", blocks)}]");
if (blocks.Sum() + blocks.Length - 1 > cells) {
Console.WriteLine("No solution");
return;
}
var spaces = new int[blocks.Length + 1];
int total = -1;
for (int i = 0; i < blocks.Length; i++) {
total += blocks[i] + 1;
spaces[i+1] = total;
}
spaces[spaces.Length - 1] = cells - 1;
var sb = new StringBuilder(string.Join(".", blocks.Select(b => new string('#', b))).PadRight(cells, '.'));
Iterate(sb, spaces, spaces.Length - 1, 0);
Console.WriteLine();
}
 
private static void Iterate(StringBuilder output, int[] spaces, int index, int offset) {
Console.WriteLine(output.ToString());
if (index <= 0) return;
int count = 0;
while (output[spaces[index] - offset] != '#') {
count++;
output.Remove(spaces[index], 1);
output.Insert(spaces[index-1], '.');
spaces[index-1]++;
Iterate(output, spaces, index - 1, 1);
}
if (offset == 0) return;
spaces[index-1] -= count;
output.Remove(spaces[index-1], count);
output.Insert(spaces[index] - count, ".", count);
}
 
}</lang>
{{out}}
<pre style="height:50ex;overflow:scroll">
5 cells with [2, 1]
##.#.
##..#
.##.#
 
5 cells with []
.....
 
10 cells with [8]
########..
.########.
..########
 
15 cells with [2, 3, 2, 3]
##.###.##.###..
##.###.##..###.
##.###..##.###.
##..###.##.###.
.##.###.##.###.
##.###.##...###
##.###..##..###
##..###.##..###
.##.###.##..###
##.###...##.###
##..###..##.###
.##.###..##.###
##...###.##.###
.##..###.##.###
..##.###.##.###
 
5 cells with [2, 3]
No solution</pre>
 
=={{header|C++}}==
Line 212 ⟶ 300:
 
</pre>
 
=={{header|C sharp}}==
This solution uses a StringBuilder. Spaces are moved from right to left and the problem is then solved recursively.
<lang csharp>using System;
using System.Linq;
using System.Text;
 
public static class Nonoblock
{
public static void Main() {
Positions(5, 2,1);
Positions(5);
Positions(10, 8);
Positions(15, 2,3,2,3);
Positions(5, 2,3);
}
 
public static void Positions(int cells, params int[] blocks) {
if (cells < 0 || blocks == null || blocks.Any(b => b < 1)) throw new ArgumentOutOfRangeException();
Console.WriteLine($"{cells} cells with [{string.Join(", ", blocks)}]");
if (blocks.Sum() + blocks.Length - 1 > cells) {
Console.WriteLine("No solution");
return;
}
var spaces = new int[blocks.Length + 1];
int total = -1;
for (int i = 0; i < blocks.Length; i++) {
total += blocks[i] + 1;
spaces[i+1] = total;
}
spaces[spaces.Length - 1] = cells - 1;
var sb = new StringBuilder(string.Join(".", blocks.Select(b => new string('#', b))).PadRight(cells, '.'));
Iterate(sb, spaces, spaces.Length - 1, 0);
Console.WriteLine();
}
 
private static void Iterate(StringBuilder output, int[] spaces, int index, int offset) {
Console.WriteLine(output.ToString());
if (index <= 0) return;
int count = 0;
while (output[spaces[index] - offset] != '#') {
count++;
output.Remove(spaces[index], 1);
output.Insert(spaces[index-1], '.');
spaces[index-1]++;
Iterate(output, spaces, index - 1, 1);
}
if (offset == 0) return;
spaces[index-1] -= count;
output.Remove(spaces[index-1], count);
output.Insert(spaces[index] - count, ".", count);
}
 
}</lang>
{{out}}
<pre style="height:50ex;overflow:scroll">
5 cells with [2, 1]
##.#.
##..#
.##.#
 
5 cells with []
.....
 
10 cells with [8]
########..
.########.
..########
 
15 cells with [2, 3, 2, 3]
##.###.##.###..
##.###.##..###.
##.###..##.###.
##..###.##.###.
.##.###.##.###.
##.###.##...###
##.###..##..###
##..###.##..###
.##.###.##..###
##.###...##.###
##..###..##.###
.##.###..##.###
##...###.##.###
.##..###.##.###
..##.###.##.###
 
5 cells with [2, 3]
No solution</pre>
 
=={{header|D}}==
Line 569:
size:5 blocks:(2 3)
❌ no solution for 5 (2 3)
</pre>
 
=={{header|Elixir}}==
Line 1,294:
 
</pre>
 
=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6>for (5, [2,1]), (5, []), (10, [8]), (5, [2,3]), (15, [2,3,2,3]) -> ($cells, @blocks) {
say $cells, ' cells with blocks: ', @blocks ?? join ', ', @blocks !! '∅';
my $letter = 'A';
my $row = join '.', map { $letter++ x $_ }, @blocks;
say "no solution\n" and next if $cells < $row.chars;
say $row ~= '.' x $cells - $row.chars;
say $row while $row ~~ s/^^ (\.*) <|w> (.*?) <|w> (\w+) \.<!|w> /$1$0.$2/;
say '';
}</lang>
{{out}}
<pre>5 cells with blocks: 2, 1
AA.B.
AA..B
.AA.B
 
5 cells with blocks: ∅
.....
 
10 cells with blocks: 8
AAAAAAAA..
.AAAAAAAA.
..AAAAAAAA
 
5 cells with blocks: 2, 3
no solution
 
15 cells with blocks: 2, 3, 2, 3
AA.BBB.CC.DDD..
AA.BBB.CC..DDD.
AA.BBB..CC.DDD.
AA..BBB.CC.DDD.
.AA.BBB.CC.DDD.
AA.BBB.CC...DDD
AA.BBB..CC..DDD
AA..BBB.CC..DDD
.AA.BBB.CC..DDD
AA.BBB...CC.DDD
AA..BBB..CC.DDD
.AA.BBB..CC.DDD
AA...BBB.CC.DDD
.AA..BBB.CC.DDD
..AA.BBB.CC.DDD</pre>
 
=={{header|Phix}}==
Line 1,679 ⟶ 1,634:
5 cells, (2 3) blocks => impossible
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<lang perl6>for (5, [2,1]), (5, []), (10, [8]), (5, [2,3]), (15, [2,3,2,3]) -> ($cells, @blocks) {
say $cells, ' cells with blocks: ', @blocks ?? join ', ', @blocks !! '∅';
my $letter = 'A';
my $row = join '.', map { $letter++ x $_ }, @blocks;
say "no solution\n" and next if $cells < $row.chars;
say $row ~= '.' x $cells - $row.chars;
say $row while $row ~~ s/^^ (\.*) <|w> (.*?) <|w> (\w+) \.<!|w> /$1$0.$2/;
say '';
}</lang>
{{out}}
<pre>5 cells with blocks: 2, 1
AA.B.
AA..B
.AA.B
 
5 cells with blocks: ∅
.....
 
10 cells with blocks: 8
AAAAAAAA..
.AAAAAAAA.
..AAAAAAAA
 
5 cells with blocks: 2, 3
no solution
 
15 cells with blocks: 2, 3, 2, 3
AA.BBB.CC.DDD..
AA.BBB.CC..DDD.
AA.BBB..CC.DDD.
AA..BBB.CC.DDD.
.AA.BBB.CC.DDD.
AA.BBB.CC...DDD
AA.BBB..CC..DDD
AA..BBB.CC..DDD
.AA.BBB.CC..DDD
AA.BBB...CC.DDD
AA..BBB..CC.DDD
.AA.BBB..CC.DDD
AA...BBB.CC.DDD
.AA..BBB.CC.DDD
..AA.BBB.CC.DDD</pre>
 
=={{header|REXX}}==
10,333

edits