Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 1456 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

How it works

To obtain all possible tilings of 2 ×ばつ L, we iterate over all non-negative integers I < 3L, making even digits in base 3 correspond to horizontal dominos and odd digits to vertical ones.

Since each I has L or less digits in base 3, this generates all possible ways of covering the 2 ×ばつ L strip. All that's left is to filter out the coverings that are bigger or smaller than 2 ×ばつ L and print the remaining coverings.

3li:L#, " Read an integer L from STDIN and push A := [ 0 1 ... (3 ** N - 1) ]. ";
{ " For each integer I in A: ";
 3b " Push B, the array of I's base 3 digits. ";
 "——|"2/ " Push S := [ '——' '|' ]. ";
 f= " Replace each D in B with S[D % 2] (the modulus is implicit). ";
 s " Flatten B. ";
}% " Collect the result in an array R. ";
{,L=}, " Filter R so it contains only strings of length L. ";
_& " Intersect R with itself to remove duplicates. ";
{N+_N}/ " For each string T in B, push (T . '\n') twice, followed by '\n'. ";

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ for i in {1..10}; do echo $[$(cjam domino.cjam <<< $i | wc -l)/3]; done
1
2
3
5
8
13
21
34
55
89

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ for i in {1..10}; do echo $[$(cjam domino.cjam <<< $i | wc -l)/3]; done
1
2
3
5
8
13
21
34
55
89

How it works

To obtain all possible tilings of 2 ×ばつ L, we iterate over all non-negative integers I < 3L, making even digits in base 3 correspond to horizontal dominos and odd digits to vertical ones.

Since each I has L or less digits in base 3, this generates all possible ways of covering the 2 ×ばつ L strip. All that's left is to filter out the coverings that are bigger or smaller than 2 ×ばつ L and print the remaining coverings.

3li:L#, " Read an integer L from STDIN and push A := [ 0 1 ... (3 ** N - 1) ]. ";
{ " For each integer I in A: ";
 3b " Push B, the array of I's base 3 digits. ";
 "——|"2/ " Push S := [ '——' '|' ]. ";
 f= " Replace each D in B with S[D % 2] (the modulus is implicit). ";
 s " Flatten B. ";
}% " Collect the result in an array R. ";
{,L=}, " Filter R so it contains only strings of length L. ";
_& " Intersect R with itself to remove duplicates. ";
{N+_N}/ " For each string T in B, push (T . '\n') twice, followed by '\n'. ";

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ for i in {1..10}; do echo $[$(cjam domino.cjam <<< $i | wc -l)/3]; done
1
2
3
5
8
13
21
34
55
89
deleted 135 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

CJam, (削除) 42 (削除ここまで) 3637 bytes

3li:L#,{3b"——|"2/f=s_f=s}%{,L=*L=}%_&,_&{N+_N}/

I've counted the dashes as one byte each since the question allows to replace them with ASCII hyphens.

Try it online.

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ for i in {1..10}; do j=$echo $[$(cjam domino.cjam <<< $i | wc -l); echo $[j/3-1];3]; done
1
2
3
5
8
13
21
34
55
89

The output contains a few extra blank lines, which seems to fall under the anything with whitespace rule.

CJam, (削除) 42 (削除ここまで) 36 bytes

3li:L#,{3b"——|"2/f=s_,L=*}%_&{N+_N}/

I've counted the dashes as one byte each since the question allows to replace them with ASCII hyphens.

Try it online.

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ for i in {1..10}; do j=$(cjam domino.cjam <<< $i | wc -l); echo $[j/3-1]; done
1
2
3
5
8
13
21
34
55
89

The output contains a few extra blank lines, which seems to fall under the anything with whitespace rule.

CJam, (削除) 42 (削除ここまで) 37 bytes

3li:L#,{3b"——|"2/f=s}%{,L=},_&{N+_N}/

I've counted the dashes as one byte each since the question allows to replace them with ASCII hyphens.

Try it online.

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ for i in {1..10}; do echo $[$(cjam domino.cjam <<< $i | wc -l)/3]; done
1
2
3
5
8
13
21
34
55
89
added 54 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

CJam, (削除) 42 (削除ここまで) 36 bytes

3li:L#,{3b"——|"2/f=s_,L=*}%_&{N+_N}/

I've counted the dashes as one byte each since the question allows to replace them with ASCII hyphens.

Try it online.

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ for i in {1..10}; do j=$(cjam domino.cjam <<< 10$i | wc -l); echo $[j/3-1]; done
2701
2
3
5
8
13
21
34
55
89

The output contains a few extra blank lines, which seems to fall under the anyhtinganything with whitespace rule.

270 lines correspond to (270 - 3) / 3 = 89 tilings.

CJam, (削除) 42 (削除ここまで) 36 bytes

3li:L#,{3b"——|"2/f=s_,L=*}%_&{N+_N}/

I've counted the dashes as one byte each since the question allows to replace them with ASCII hyphens.

Try it online.

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ cjam domino.cjam <<< 10 | wc -l
270

The output contains a few extra blank lines, which seems to fall under the anyhting with whitespace rule.

270 lines correspond to (270 - 3) / 3 = 89 tilings.

CJam, (削除) 42 (削除ここまで) 36 bytes

3li:L#,{3b"——|"2/f=s_,L=*}%_&{N+_N}/

I've counted the dashes as one byte each since the question allows to replace them with ASCII hyphens.

Try it online.

Example run

$ cjam domino.cjam <<< 3
|——
|——
——|
——|
|||
|||
$ for i in {1..10}; do j=$(cjam domino.cjam <<< $i | wc -l); echo $[j/3-1]; done
1
2
3
5
8
13
21
34
55
89

The output contains a few extra blank lines, which seems to fall under the anything with whitespace rule.

deleted 13 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading

AltStyle によって変換されたページ (->オリジナル) /