Layout my tiles for me

1

I'm retiling my kitchen with different coloured tiles. In fact, it's going to have at least 2 colours - and 26 colours as a maximum.

But, I'm fussy. I want this wall to look random, but sometimes random isn't random enough.

So, when I lay out my tiles, I don't want to have any the same colour next to each other in the 4 Cartesian directions. Diagonally adjacent is (of course) fine.

Acceptable:

┌───┐
│ A │
└───┘
┌───┬───┬───┐
│ A │ B │ A │
├───┼───┼───┤
│ B │ A │ B │
└───┴───┴───┘
┌───┬───┬───┐
│ A │ B │ C │
├───┼───┼───┤
│ C │ A │ B │
└───┴───┴───┘

Unacceptable:

┌───┬───┬───┐
│ A │ B │ A │
├───┼───┼───┤
│ A │ A │ B │
└───┴───┴───┘
┌───┬───┬───┐
│ A │ C │ C │
├───┼───┼───┤
│ C │ A │ B │
└───┴───┴───┘

Given input in the form A B N where A is the width, B is the height and N is the number of colours - an integer from 2 to 26 - you need to output a grid like so:

Input:

5 6 4

Example output:

A B C D A
B A D A C
A D C D A
B C B A B
D A D C A
B C A D C

The input will be given as a) 3 arguments to a function or b) as 3 separate input statements or b) as A B N. Input will always be valid.

All colours have to be used and all valid tilings have to have a non-zero probability of being generated.

This is so the shortest code wins.

Tim

Posted 2015-11-15T15:15:25.223

Reputation: 2 789

14... come on, you're familiar with the SE network; you should know better than to delete and repost questions. – Doorknob – 2015-11-15T15:17:49.580

2

You deleted and then reposted this question (and you should know full well that's highly frowned upon on SE...).

– Doorknob – 2015-11-15T15:20:44.767

1

There were no answers on the original question. If you want to prevent answers while you're working on a question, use the Sandbox.

– Doorknob – 2015-11-15T15:22:01.850

4You know you can undelete questions, right? (It's also recommended that you use the Sandbox anyway, even if you think there are no issues with your question, because there could still be (this question being a case in point).) – Doorknob – 2015-11-15T15:24:57.670

Let us continue this discussion in chat.

– Tim – 2015-11-15T15:25:06.480

@Tim You can undelete and then edit quickly. Just paste this challenge over the old one. – Calvin's Hobbies – 2015-11-15T15:33:13.307

@Calvin'sHobbies should I do that now it's been reposted? – Tim – 2015-11-15T15:33:52.717

Does each row have to be printed space-separated? – Sp3000 – 2015-11-15T17:12:13.023

@Sp3000 yes it does. – Tim – 2015-11-15T17:12:34.060

What's the minimum for the width/height? Are they at least 2? – Reto Koradi – 2015-11-15T17:14:53.983

1@RetoKoradi one of them is at least two, and the min is 1. – Tim – 2015-11-15T17:15:47.423

Should the output be formatted exactly as in the example, i.e., using the first N uppercase letters, with spaces in rows and newlines between columns? – Dennis – 2015-11-15T17:37:42.643

@Dennis Space and newlines yes. No, the arrangment should not be fixed with A B C D ... Z. – Tim – 2015-11-15T17:41:27.107

Answers

1

Python 2, 151 149 bytes

from random import*
W,H,N=input()
L='x'*W
exec"M='';exec'M+=sample(set(map(chr,range(65,65+N)))-{L[len(M)],M[-1:]},1)[0];'*W;L=M;print' '.join(L);"*H

Takes input comma-separated via STDIN, e.g. 10, 10, 2.

Sp3000

Posted 2015-11-15T15:15:25.223

Reputation: 58 729

1

CJam, 53 52 51 bytes

q~:L,'Af+a*a*{_::mR_:|,L<\_z:O|N*2ew::=|:|}g;OSf*N*

This will take a long time if AB is too large or N is too small.

Try it online in the CJam interpreter.

Dennis

Posted 2015-11-15T15:15:25.223

Reputation: 196 637

I wrote a solution earlier but haven't posted it, see if there's anything you can use: q~:K;\,a*{{;Kmr}f%{_2few:::=s\z}2*@@|'0-}g'Aff+N* – aditsu quit because SE is EVIL – 2015-11-15T18:04:28.590

0

Python2, 177 bytes

from random import *
W,H,N=input()
A,l={chr(c)for c in range(65,65+N)},[" "*W]
for h in range(H):
 s=""
 for c in l[h]:s+=sample(A-{s[-1:],c},1)[0]
 l+=[s]
print"\n".join(l[1:])

orlp

Posted 2015-11-15T15:15:25.223

Reputation: 37 067