# Plant trees in a park - As fast as you can!

This challenge is inspired by this app. The test cases are borrowed from that app.

This is a challenge, where the objective is to solve the largest test cases in the least amount of time. There are provided some smaller test cases, so that people might test their algorithms faster.

You'll be given a square input grid, of dimensions n-by-n where 9 <= n <= 12. This grid will be divided into n areas, where the cells of each area has a unique identifiers (I'll use lower case letters from a-l in the the text here, but you may choose whatever you like, for instance integers 1-12).

The input may look like this (optional input format):

aabbbbbcc
hhhdifggg
hdddifffg
hhhiifffg
hihiifffg
iiiiiiggg


Or, easier to visualize:

## Challenge:

You are to place 2*n trees in this park, according to the following rules:

• There shall be exactly 2 trees per column, and 2 trees per row
• All areas shall have exactly 2 trees.
• No trees can be adjacent to another tree, vertically, horizontally or diagonally

The solution to the layout above is:

Note: There is only one solution to each puzzle

• The input and output formats are optional
• The output might for instance be a list of indices, a grid with 1/0 indicating if there's a tree in that position, or a modified version of the input where the trees are indicated
• The execution time must be deterministic
• The program must finish withing 1 minute at @isaacg's computer
• Specs: 4 CPUs, i5-4300U CPU @ 1.9 GHz, 7.5G of RAM.
• In case your program can't solve the two largest test case in one minute each then the time for the second largest (n=11) will be your score. You'll lose against a solution that solves the largest case.

### Test cases:

I might edit this list if submissions seems to be customized to fit these test cases.

12-by-12:

--- Input ---
aaaaabccccdd
aaaaabccccdd
aaaaabbbbddd
eeeafffgbghh
eeaafffgbghh
eefffffggghh
eeefijffghhh
iieiijjjjkhh
iiiiijjjjkhk
lljjjjjjjkkk
llllllkkkkkk
llllllkkkkkk
--- Solution ---
aaaaabcccCdD
aaaaaBcCccdd
aAaaabbbbdDd
eeeaffFgBghh
eeAaFffgbghh
eefffffGgGhh
EeefijffghhH
iiEiIjjjjkhh
IiiiijjjjkHk
lljJjJjjjkkk
lLllllkkKkkk
lllLllKkkkkk


11-by-11:

--- Input ---
aaaaaaabbcc
edddbbbbbbc
eddddbbbbbb
effffggghhh
effffgghhii
eefffjjhhii
eeeejjjhhii
eeejjjjkiii
jjjjjjkkiii
jjjjjkkkiii
--- Solution ---
aaAaaaabbCc
eDddbbbbbbC
eddDdBbbbbb
effffggGhHh
eFfffGghhii
eefFfjjhHii
EeeejjjhhiI
eeEjjjjKiii
JjjjJjkkiii
jjjjjkKkIii


10-by-10

--- Input ---
aaaaabccdd
aeaabbbccd
aeaabfbgcd
eeeaafggcd
eeeaafghcd
eeeiifghcd
ieiiigghcd
iiijighhcd
jjjjighhcd
jjjggghhdd
--- Solution ---
aaAaabccdD
aeaaBbBccd
aEaabfbgcD
eeeaaFgGcd
eEeAafghcd
eeeiiFghCd
IeiIigghcd
iiijigHhCd
JjJjighhcd
jjjgGghHdd


9-by-9

--- Input ---
aabbbbbcc
hhhdifggg
hdddifffg
hhhiifffg
hihiifffg
iiiiiiggg
--- Solution ---
aAbBbbbcc
hhhDiFggg
hDddifffG
hhhiIfFfg
HiHiifffg
iiiiiIgGg
--- Input ---
aaabbbccc
aaaabbccc
ffddddcce
ffffddeee
fgffdheee
fggfhhhee
iggggheee
iiigggggg
--- Solution ---
aaAbBbccc
AaaabbcCc
fFddddcCe
fffFdDeee
fGffdheeE
fggfHhHee
IggggheeE
iiIgggGgg

• "The input and output formats are optional, but should be the same" What does that mean? Can't I output a list of lists containing 1s and 0s for trees and non trees without caring about outputting the areas? – Fatalize Jun 23 '17 at 13:32
• @Fatalize, edited. I think outputting a list of indices or a grid with 1/0 as you suggest is a good idea. – Stewie Griffin Jun 23 '17 at 13:35
• Information (if I calculate correctly): There are 3647375398569086976 configurations to put 24 trees in a 12*12 grid satisfy only (1): There shall be exactly 2 trees per column, and 2 trees per row, so a brute-force is probably impossible. – user202729 Jun 23 '17 at 14:13
• "shouldn't be a big problem": I personally think that it is. My current implementation solves the first test case in ~150ms and the 3rd one in 5s but fails to solve the last one (which is 'only' 11x11) in any reasonable amount of time. It would probably require some much more aggressive forward pruning -- and therefore a significant amount of additional code -- to complete within 1 minute. – Arnauld Jun 23 '17 at 16:25
• @Arnauld, I changed the maximum to 11 since that's the largest test case. You may post your solution (as a valid, competing submission), but it will not win if someone posts a solution that solves all test cases, regardless of code length. Fair? – Stewie Griffin Jun 23 '17 at 16:57

## JavaScript (ES6), 271 bytes

Takes input as an array of arrays of characters. Returns an array of bitmasks (integers) describing the position of the trees on each row, where the least significant bit is the leftmost position.

f=(a,p=0,r=[S=y=0],w=a.length)=>a.some((R,y)=>a.some((_,x)=>r[y]>>x&1&&(o[k=R[x]]=-~o[k])>2),o=[])?0:y<w?[...Array(1<<w)].some((_,n)=>(k=n^(x=n&-n))<=x*2|k&-k^k|n&(p|p/2|p*2)||r.some((A,i)=>r.some((B,j)=>A&B&n&&i<y&j<i))?0:(w=r[y],f(a,r[y++]=n,r),r[--y]=w,S))&&S:S=[...r]


### Formatted and commented

f = (                                           // given:
a,                                            //   - a = input matrix
p = 0,                                        //   - p = previous bitmask
r = [                                         //   - r = array of tree bitmasks
S = y = 0 ],                            //   - S = solution / y = current row
w = a.length                                  //   - w = width of matrix
) =>                                            //
a.some((R, y) => a.some((_, x) =>             // if there's at least one area with more
r[y] >> x & 1 && (o[k = R[x]] = -~o[k]) > 2 // than two trees:
), o = []) ?                                  //
0                                           //   abort right away
:                                             // else:
y < w ?                                     //   if we haven't reached the last row:
[...Array(1 << w)].some((_, n) =>         //     for each possible bitmask n:
(k = n ^ (x = n & -n)) <= x * 2 |       //       if the bitmask does not consist of
k & - k ^ k |                           //       exactly two non-consecutive bits,
n & (p | p / 2 | p * 2) ||              //       or is colliding with the previous
r.some((A, i) => r.some((B, j) =>       //       bitmask, or generates more than two
A & B & n && i < y & j < i            //       trees per column:
)) ?                                    //
0                                     //         yield 0
:                                       //       else:
(                                     //
w = r[y],                           //         save the previous bitmask
f(a, r[y++] = n, r),                //         recursive call with the new one
r[--y] = w,                         //         restore the previous bitmask
S                                   //         yield S
)                                     //
) && S                                    //     end of some(): return false or S
:                                           //   else:
S = [...r]                                //     this is a solution: save a copy in S


### Test cases

This snippet includes an additional function to display the results in a more readable format. It is too slow to solve the last test case.

Expected runtime: ~5 seconds.

f=(a,p=0,r=[S=y=0],w=a.length)=>a.some((R,y)=>a.some((_,x)=>r[y]>>x&1&&(o[k=R[x]]=-~o[k])>2),o=[])?0:y<w?[...Array(1<<w)].some((_,n)=>(k=n^(x=n&-n))<=x*2|k&-k^k|n&(p|p/2|p*2)||r.some((A,i)=>r.some((B,j)=>A&B&n&&i<y&j<i))?0:(w=r[y],f(a,r[y++]=n,r),r[--y]=w,S))&&S:S=[...r]

format = (a, w) => a.map(n => [...('0'.repeat(w) + n.toString(2)).slice(-w)].reverse().join('')).join('\n')

console.log(format(f([
[..."aabbbbbcc"],
[..."hhhdifggg"],
[..."hdddifffg"],
[..."hhhiifffg"],
[..."hihiifffg"],
[..."iiiiiiggg"]
]), 9));

console.log(format(f([
[..."aaabbbccc"],
[..."aaaabbccc"],
[..."ffddddcce"],
[..."ffffddeee"],
[..."fgffdheee"],
[..."fggfhhhee"],
[..."iggggheee"],
[..."iiigggggg"]
]), 9));

console.log(format(f([
[..."aaaaabccdd"],
[..."aeaabbbccd"],
[..."aeaabfbgcd"],
[..."eeeaafggcd"],
[..."eeeaafghcd"],
[..."eeeiifghcd"],
[..."ieiiigghcd"],
[..."iiijighhcd"],
[..."jjjjighhcd"],
[..."jjjggghhdd"]
]), 10));

• OP's note: This submission was made when the challenge was a code-golf challenge. It is therefore perfectly valid, even though it isn't optimized for the current winning criterion! – Stewie Griffin Jun 24 '17 at 13:23
• Timing: Takes over a minute on 11x11. – isaacg Jun 24 '17 at 23:03
• We're in a pickle, maybe you can help. Can you think of any way to generate nontrivial larger puzzle instances? – isaacg Jun 26 '17 at 9:42

# C, official time: 5ms for 12x12

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <omp.h>

#define valT char
#define posT int

#ifndef _OPENMP
#  warning Building without OpenMP support
#endif

static void complete(posT n, valT *workspace) {
const posT s = n * 3 + 2;

const valT *regions = workspace;
valT *output = &workspace[n*2+1];

for(posT y = 0; y < n; ++ y) {
for(posT x = 0; x < n; ++ x) {
//          putchar(output[y*s+x] ? '*' : '-');
putchar(regions[y*s+x] + (output[y*s+x] ? 'A' : 'a'));
}
putchar('\n');
}

_Exit(0);
}

static void solveB(const posT n, valT *workspace, valT *pops, const posT y) {
const posT s = n * 3 + 2;

const valT *regions = workspace;
const valT *remaining = &workspace[n];
valT *output = &workspace[n*2+1];

for(posT r = 0; r < n; ++ r) {
if(pops[r] + remaining[r] < 2) {
return;
}
}

for(posT t1 = 0; t1 < n - 2; ++ t1) {
posT r1 = regions[t1];
if(output[t1+1-s]) {
t1 += 2;
continue;
}
if(output[t1-s]) {
++ t1;
continue;
}
if((pops[t1+n] | pops[r1]) & 2 || output[t1-1-s]) {
continue;
}
output[t1] = 1;
++ pops[t1+n];
++ pops[r1];
for(posT t2 = t1 + 2; t2 < n; ++ t2) {
posT r2 = regions[t2];
if(output[t2+1-s]) {
t2 += 2;
continue;
}
if(output[t2-s]) {
++ t2;
continue;
}
if((pops[t2+n] | pops[r2]) & 2 || output[t2-1-s]) {
continue;
}
output[t2] = 1;
++ pops[t2+n];
++ pops[r2];
if(y == 0) {
complete(n, &workspace[-s*(n-1)]);
}
solveB(n, &workspace[s], pops, y - 1);
output[t2] = 0;
-- pops[t2+n];
-- pops[r2];
}
output[t1] = 0;
-- pops[t1+n];
-- pops[r1];
}
}

static void solve(const posT n, valT *workspace) {
const posT s = n * 3 + 2;

valT *regions = workspace;
valT *remaining = &workspace[n];
valT *pops = &workspace[n*s];
//  memset(&remaining[n*s], 0, n * sizeof(valT));

for(posT y = n; (y --) > 0;) {
memcpy(&remaining[y*s], &remaining[(y+1)*s], n * sizeof(valT));
for(posT x = 0; x < n; ++ x) {
valT r = regions[y*s+x];
valT *c = &remaining[y*s+r];
valT *b = &pops[r*3];
if(*c == 0) {
*c = 1;
b[0] = y - 1;
b[1] = x - 1;
b[2] = x + 1;
} else if(x < b[1] || x > b[2] || y < b[0]) {
*c = 2;
} else {
b[1] = b[1] > (x - 1) ? b[1] : (x - 1);
b[2] = b[2] < (x + 1) ? b[2] : (x + 1);
}
}
//      memset(&output[y*s], 0, (n+1) * sizeof(valT));
}
memset(pops, 0, n * 2 * sizeof(valT));

posT sz = (n + 1) * s + n * 3;
for(posT i = 1; i < omp_get_max_threads(); ++ i) {
memcpy(&workspace[i*sz], workspace, sz * sizeof(valT));
}
}

#pragma omp parallel for if (n >= MIN_THREADED_SIZE)
for(posT t1 = 0; t1 < n - 2; ++ t1) {
valT *regions = workspace2;
valT *output = &workspace2[n*2+1];
valT *pops = &workspace2[n*s];

posT r1 = regions[t1];
output[t1] = pops[t1+n] = pops[r1] = 1;
for(posT t2 = t1 + 2; t2 < n; ++ t2) {
posT r2 = regions[t2];
output[t2] = pops[t2+n] = 1;
++ pops[r2];
solveB(n, &regions[s], pops, n - 2);
output[t2] = pops[t2+n] = 0;
-- pops[r2];
}
output[t1] = pops[t1+n] = pops[r1] = 0;
}
}

int main(int argc, const char *const *argv) {
if(argc < 2) {
fprintf(stderr, "Usage: %s 'grid-here'\n", argv[0]);
return 1;
}

const char *input = argv[1];
const posT n = strchr(input, '\n') - input;
const posT s = n * 3 + 2;

posT sz = (n + 1) * s + n * 3;
valT *workspace = (valT*) calloc(sz * threads, sizeof(valT));
valT *regions = workspace;

for(posT y = 0; y < n; ++ y) {
for(posT x = 0; x < n; ++ x) {
regions[y*s+x] = input[y*(n+1)+x] - 'a';
}
}

solve(n, workspace);

fprintf(stderr, "Failed to solve grid\n");
return 1;
}


Compiled with GCC 7 using the -O3 and -fopenmp flags. Should have similar results on any version of GCC with OpenMP installed.

gcc-7 Trees.c -O3 -fopenmp -o Trees


Input and output format are as given in the question.

On my machine this takes 0.009s 0.008s 0.005s for the 12x12 example, and 0.022s 0.020s 0.019s to run all the examples. On the benchmark machine, isaacg reports 5ms for the 12x12 example using the original (non-threaded) version of the code.

Usage:

./Trees 'aaabbbccc
aaaabbccc
ffddddcce
ffffddeee
fgffdheee
fggfhhhee
iggggheee
iiigggggg'


Just a simple brute-force solver, working on one row at a time. It runs at a good speed by recognising impossible situations early (e.g. no region cells remaining, but less than 2 trees in the region).

The first update improves cache hits by putting related data close together in memory, and makes the possible-trees-remaining-in-segment calculations slightly smarter (now accounts for the fact that trees cannot be adjacent). It also extracts the outermost loop so that fewer special cases are needed in the hottest part of the algorithm.

The second update makes the outermost loop run in parallel across the available processors (using OpenMP), giving a linear speed boost. This is only enabled for n >= 11, since the overhead of spawning threads outweighs the benefits for smaller grids.

• Official timing: 5ms for 12x12. If anyone else gets close, we'll need bigger test cases. – isaacg Jun 24 '17 at 23:24
• We're in a pickle, maybe you can help. Can you think of any way to generate nontrivial larger puzzle instances? – isaacg Jun 26 '17 at 9:41
• @isaacg Well from the illustrated example, it seems the original grids were made by placing trees first (in a knight pattern with minor changes in that example, but I guess any pattern with 2 trees per row & column would work) then fitting regions around them so that each region contains 2 trees. Seems like that should be a simple enough method for a human to follow for arbitrarily large grids. – Dave Jun 26 '17 at 17:47
• In fact, looking again, it's not a knight pattern with minor changes, but a wrapping pattern where each tree is offset (1,2) from the previous. Once you have a pattern, you can permute rows and columns to make less structured solutions, just as long as it doesn't leave trees adjacent. – Dave Jun 26 '17 at 17:51

# Java (OpenJDK 8), Official Timing: 1.2s on 12x12

edit: no longer code golf

import java.util.*;

// Callable method, takes an int[][] and modifies it
static void f(int[][] areas){
List<List<BitSet>> areaBitSets = new ArrayList<>();
List<List<BitSet>> areaTreeBitSets = new ArrayList<>();
for(int i = 0; i < areas.length; i++){
}

// Add a bitset to our list representing each possible square, grouped by area
for(int i=0; i < areas.length; i++){
for(int j=0; j < areas.length; j++){
BitSet b = new BitSet();
b.set(i*areas.length + j);
}
}

// Fold each set onto itself so each area bitset has two trees
for(int i=0; i < areas.length; i++){
for(int j=0; j<areaBitSets.get(i).size()-1; j++){
for(int k=j+1; k <areaBitSets.get(i).size(); k++){
if(canFoldTogether(areaBitSets.get(i).get(j),areaBitSets.get(i).get(k), areas.length)){
BitSet b = (BitSet)areaBitSets.get(i).get(j).clone();
b.or(areaBitSets.get(i).get(k));
}
}
}
}

// Starting with area 0 add each area one at a time doing Cartesian products

for(int i=1; i < areaTreeBitSets.size(); i++){
while(!currentPossibilities.isEmpty()){
BitSet b= (BitSet)currentPossibilities.poll().clone();

for(BitSet c: areaTreeBitSets.get(i)){
if(canFoldTogether(b,c,areas.length)){
BitSet d=(BitSet)b.clone();
d.or(c);
}
}
}
futurePossibilities.clear();
}

// Get final output and modify the array
BitSet b = currentPossibilities.poll();
for(int i=0; i<areas.length*areas.length; i++){
areas[i/areas.length][i%areas.length] = b.get(i)?1:0;
}
}

// Helper method which determines whether combining two bitsets
// will still produce a valid output
static boolean canFoldTogether(BitSet a, BitSet b, int size){

// See if there are trees too close to each other
int c=-1;
while((c=a.nextSetBit(c+1))>=0){
int d=-1;
while((d=b.nextSetBit(d+1))>=0){
int r1=c/size;
int r2=d/size;
int c1=c%size;
int c2=d%size;

int rDifference = r1>r2 ? r1-r2 : r2-r1;
int cDifference = c1>c2 ? c1-c2 : c2-c1;
if(rDifference<2 && cDifference<2)
return false;
}
}

// Check for row and column cardinality
BitSet f,g;
for(int i=0; i<size; i++){
f = new BitSet();
f.set(i*size,(i+1)*size);
g=(BitSet)f.clone();
f.and(a);
g.and(b);
f.or(g);
if(f.cardinality()>2){
return false;
}

f=new BitSet();
for(int j = 0; j<size; j++){
f.set(j*size+i);
}
g=(BitSet)f.clone();
f.and(a);
g.and(b);
f.or(g);
if(f.cardinality()>2){
return false;
}
}

return true;
}


Try it online!

TIO link is for the 12x12 test case. TIO reports 2.429s for the run time.

Takes an array of integers as the input and modifies the array to contain 1s for trees and 0s for non-trees.

This runs for all testcases. The largest testcase runs on my machine in less than a second, although I have a pretty powerful machine

Test code for the 12x12, can modify for others

public static void main(String[] args){
int[][] test = {{0,  0,  0,  0,  0,  1,  2,  2,  2,  2,  3,  3},
{0,  0,  0,  0,  0,  1,  2,  2,  2,  2,  3,  3},
{0,  0,  0,  0,  0,  1,  1,  1,  1,  3,  3,  3},
{4,  4,  4,  0,  5,  5,  5,  6,  1,  6,  7,  7},
{4,  4,  0,  0,  5,  5,  5,  6,  1,  6,  7,  7},
{4,  4,  5,  5,  5,  5,  5,  6,  6,  6,  7,  7},
{4,  4,  4,  5,  8,  9,  5,  5,  6,  7,  7,  7},
{8,  8,  4,  8,  8,  9,  9,  9,  9,  10,  7,  7},
{8,  8,  8,  8,  8,  9,  9,  9,  9,  10,  7,  10},
{11,  11,  9,  9,  9,  9,  9,  9,  9,  10,  10,  10},
{11,  11,  11,  11,  11,  11,  10,  10,  10,  10,  10,  10},
{11,  11,  11,  11,  11,  11,  10,  10,  10,  10,  10,  10}};

long l = System.currentTimeMillis();
f(test);
System.out.println("12x12: " + (System.currentTimeMillis() - l) + "ms");

for(int[] t : test){
System.out.println(Arrays.toString(t));
}

}


Produces this on my machine:

12x12: 822ms
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1]
[0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0]
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0]

• OP's note: This submission was made when the challenge was a code-golf challenge. It is therefore perfectly valid, even though it isn't (only) optimized for the current winning criterion! – Stewie Griffin Jun 24 '17 at 13:23
• @StewieGriffin thanks for the comment. When I get a chance I'll work on cleaning it up some since it's no longer code golf and see if I can optimize it for some speed – PunPun1000 Jun 24 '17 at 14:17
• Official timing: 1.2 seconds on 12x12. – isaacg Jun 24 '17 at 23:16
• We're in a pickle, maybe you can help. Can you think of any way to generate nontrivial larger puzzle instances? – isaacg Jun 26 '17 at 9:41

# Clingo, ≈ 7 ms for 12×12, 116 bytes

{t(X,Y):c(X,Y,Z)}=2:-Z=1..n.
:-X=1..n,{t(X,1..n)}!=2.
:-Y=1..n,{t(1..n,Y)}!=2.
:-t(X,Y),t(X+1,Y;X+1,Y+1;X,Y+1;X-1,Y+1).


(Newlines are optional and not counted.)

Run with clingo plant.lp - -c n=<n> where <n> is the grid size. The input format is a list of c(X,Y,Z). statements for each cell (X, Y) colored Z, with 1 ≤ X, Y, Zn, separated by optional whitespace. The output includes t(X,Y) for each tree at (X, Y).

The time is pretty meaningless as it’s basically just startup time, so consider this a vote for larger test cases.

### Demo

\$ clingo plant.lp -c n=12 - <<EOF
> c(1,1,1). c(2,1,1). c(3,1,1). c(4,1,1). c(5,1,1). c(6,1,2). c(7,1,3). c(8,1,3). c(9,1,3). c(10,1,3). c(11,1,4). c(12,1,4).
> c(1,2,1). c(2,2,1). c(3,2,1). c(4,2,1). c(5,2,1). c(6,2,2). c(7,2,3). c(8,2,3). c(9,2,3). c(10,2,3). c(11,2,4). c(12,2,4).
> c(1,3,1). c(2,3,1). c(3,3,1). c(4,3,1). c(5,3,1). c(6,3,2). c(7,3,2). c(8,3,2). c(9,3,2). c(10,3,4). c(11,3,4). c(12,3,4).
> c(1,4,5). c(2,4,5). c(3,4,5). c(4,4,1). c(5,4,6). c(6,4,6). c(7,4,6). c(8,4,7). c(9,4,2). c(10,4,7). c(11,4,8). c(12,4,8).
> c(1,5,5). c(2,5,5). c(3,5,1). c(4,5,1). c(5,5,6). c(6,5,6). c(7,5,6). c(8,5,7). c(9,5,2). c(10,5,7). c(11,5,8). c(12,5,8).
> c(1,6,5). c(2,6,5). c(3,6,6). c(4,6,6). c(5,6,6). c(6,6,6). c(7,6,6). c(8,6,7). c(9,6,7). c(10,6,7). c(11,6,8). c(12,6,8).
> c(1,7,5). c(2,7,5). c(3,7,5). c(4,7,6). c(5,7,9). c(6,7,10). c(7,7,6). c(8,7,6). c(9,7,7). c(10,7,8). c(11,7,8). c(12,7,8).
> c(1,8,9). c(2,8,9). c(3,8,5). c(4,8,9). c(5,8,9). c(6,8,10). c(7,8,10). c(8,8,10). c(9,8,10). c(10,8,11). c(11,8,8). c(12,8,8).
> c(1,9,9). c(2,9,9). c(3,9,9). c(4,9,9). c(5,9,9). c(6,9,10). c(7,9,10). c(8,9,10). c(9,9,10). c(10,9,11). c(11,9,8). c(12,9,11).
> c(1,10,12). c(2,10,12). c(3,10,10). c(4,10,10). c(5,10,10). c(6,10,10). c(7,10,10). c(8,10,10). c(9,10,10). c(10,10,11). c(11,10,11). c(12,10,11).
> c(1,11,12). c(2,11,12). c(3,11,12). c(4,11,12). c(5,11,12). c(6,11,12). c(7,11,11). c(8,11,11). c(9,11,11). c(10,11,11). c(11,11,11). c(12,11,11).
> c(1,12,12). c(2,12,12). c(3,12,12). c(4,12,12). c(5,12,12). c(6,12,12). c(7,12,11). c(8,12,11). c(9,12,11). c(10,12,11). c(11,12,11). c(12,12,11).
> EOF
clingo version 5.1.0
Solving...
c(1,1,1) c(2,1,1) c(3,1,1) c(4,1,1) c(5,1,1) c(6,1,2) c(7,1,3) c(8,1,3) c(9,1,3) c(10,1,3) c(11,1,4) c(12,1,4) c(1,2,1) c(2,2,1) c(3,2,1) c(4,2,1) c(5,2,1) c(6,2,2) c(7,2,3) c(8,2,3) c(9,2,3) c(10,2,3) c(11,2,4) c(12,2,4) c(1,3,1) c(2,3,1) c(3,3,1) c(4,3,1) c(5,3,1) c(6,3,2) c(7,3,2) c(8,3,2) c(9,3,2) c(10,3,4) c(11,3,4) c(12,3,4) c(1,4,5) c(2,4,5) c(3,4,5) c(4,4,1) c(5,4,6) c(6,4,6) c(7,4,6) c(8,4,7) c(9,4,2) c(10,4,7) c(11,4,8) c(12,4,8) c(1,5,5) c(2,5,5) c(3,5,1) c(4,5,1) c(5,5,6) c(6,5,6) c(7,5,6) c(8,5,7) c(9,5,2) c(10,5,7) c(11,5,8) c(12,5,8) c(1,6,5) c(2,6,5) c(3,6,6) c(4,6,6) c(5,6,6) c(6,6,6) c(7,6,6) c(8,6,7) c(9,6,7) c(10,6,7) c(11,6,8) c(12,6,8) c(1,7,5) c(2,7,5) c(3,7,5) c(4,7,6) c(5,7,9) c(6,7,10) c(7,7,6) c(8,7,6) c(9,7,7) c(10,7,8) c(11,7,8) c(12,7,8) c(1,8,9) c(2,8,9) c(3,8,5) c(4,8,9) c(5,8,9) c(6,8,10) c(7,8,10) c(8,8,10) c(9,8,10) c(10,8,11) c(11,8,8) c(12,8,8) c(1,9,9) c(2,9,9) c(3,9,9) c(4,9,9) c(5,9,9) c(6,9,10) c(7,9,10) c(8,9,10) c(9,9,10) c(10,9,11) c(11,9,8) c(12,9,11) c(1,10,12) c(2,10,12) c(3,10,10) c(4,10,10) c(5,10,10) c(6,10,10) c(7,10,10) c(8,10,10) c(9,10,10) c(10,10,11) c(11,10,11) c(12,10,11) c(1,11,12) c(2,11,12) c(3,11,12) c(4,11,12) c(5,11,12) c(6,11,12) c(7,11,11) c(8,11,11) c(9,11,11) c(10,11,11) c(11,11,11) c(12,11,11) c(1,12,12) c(2,12,12) c(3,12,12) c(4,12,12) c(5,12,12) c(6,12,12) c(7,12,11) c(8,12,11) c(9,12,11) c(10,12,11) c(11,12,11) c(12,12,11) t(1,7) t(1,9) t(2,3) t(2,11) t(3,5) t(3,8) t(4,10) t(4,12) t(5,5) t(5,8) t(6,2) t(6,10) t(7,4) t(7,12) t(8,2) t(8,6) t(9,4) t(9,11) t(10,1) t(10,6) t(11,3) t(11,9) t(12,1) t(12,7)
SATISFIABLE

Models       : 1+
Calls        : 1
Time         : 0.009s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time     : 0.000s


To make the input/output format easier to deal with, here are Python programs to convert from and to the format given in the challenge.

### Input

import sys
print(' '.join("c({},{},{}).".format(x + 1, y + 1, ord(cell) - ord('a') + 1) for y, row in enumerate(sys.stdin.read().splitlines()) for x, cell in enumerate(row)))


### Output

import re
import sys
for line in sys.stdin:
c = {(int(x), int(y)): int(z) for x, y, z in re.findall(r'\bc$$(\d+),(\d+),(\d+)$$', line)}
if c:
t = {(int(x), int(y)) for x, y in re.findall(r'\bt$$(\d+),(\d+)$$', line)}
n, n = max(c)
for y in range(1, n + 1):
print(''.join(chr(ord('aA'[(x, y) in t]) + c[x, y] - 1) for x in range(1, n + 1)))
print()

• Looks like we're gonn'a need a bigger test case. Btw, you'd be winning the golf version of this question — just needs the 2s changing to 1s. – Dave Jun 25 '17 at 10:20
• Official timing is 18 milliseconds on 12x12, I apologize. 1 character typo, that's the trouble with abbreviations. – isaacg Jun 26 '17 at 6:49
• We're in a pickle, maybe you can help. Can you think of any way to generate nontrivial larger puzzle instances? – isaacg Jun 26 '17 at 9:41