10
\$\begingroup\$

This challenge is inspired by this app.


This is a much easier version of this challenge. This challenge is , while the other one is .


You'll be given a square input grid, of dimensions 6-by-6 which is divided into 6 areas, where the cells of each area have a unique identifier (I'll use lower case letters from a-f in the the text here, but you may choose whatever you like, for instance integers 1-6).

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

aabbbb
aabbbb
aaccbb
acccdb
ecccdb
eeefff

Or, easier to visualize:

enter image description here

Challenge:

You are to place 6 trees in this park, according to the following rules:

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

The solution to the layout above is:

enter image description here

Note: There is only one solution to each puzzle

Additional rules:

  • 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 within 1 minute on a reasonable modern laptop
  • Brownie points if you don't brute force!

Test cases:

aabbbb
aabbbb
aaccbb
acccdb
ecccdb
eeefff
---
aAbbbb
aabbbB
aaCcbb
acccDb
Ecccdb
eeeFff
----------
aabccc
aacccc
aaddce
aeeeee
aeeefe
eeeeee
---
aaBccc
aacccC
aadDce
Aeeeee
aeeeFe
eEeeee
----------
aaaabb
aacbbb
aadddb
addeef
ddddee
dddeee
---
aaaaBb
aaCbbb
Aadddb
addeeF
dDddee
dddEee
----------
abbbcd
abebcd
addddd
dddddd
effdff
eeffff
---
abBbcd
abebCd
Addddd
dddDdd
effdfF
eEffff

Same test cases on a format that's a bit easier to parse:

Test case 1:
[1,1,2,2,2,2;1,1,2,2,2,2;1,1,3,3,2,2;1,3,3,3,4,2;5,3,3,3,4,2;5,5,5,6,6,6]
Test case 2:
[1,1,2,3,3,3;1,1,3,3,3,3;1,1,4,4,3,5;1,5,5,5,5,5;1,5,5,5,6,5;5,5,5,5,5,5]
Test case 3:
[1,1,1,1,2,2;1,1,3,2,2,2;1,1,4,4,4,2;1,4,4,5,5,6;4,4,4,4,5,5;4,4,4,5,5,5]
Test case 4:
[1,2,2,2,3,4;1,2,5,2,3,4;1,4,4,4,4,4;4,4,4,4,4,4;5,6,6,4,6,6;5,5,6,6,6,6]
\$\endgroup\$
1
  • \$\begingroup\$ Sounds like a sudoku solver but different. \$\endgroup\$
    – Joseph
    Commented Dec 4, 2017 at 23:11

2 Answers 2

2
\$\begingroup\$

C, 223 182 bytes

O[15],U;main(y,v)char**v;{if(y>7)for(;y-->2;printf("%06o\n",O[y]));else for(int*r,x=1,X=8;X<14;U&x|*r|O[10-y]*9&x*9?0:(U^=O[9-y]=*r=x,*r=main(y+1,v),U^=x),x*=8)r=O+v[1][y*7-++X]-88;}

Takes input as an argument in the format given in the question. Writes output to stdout as a grid of 0s with 1s where the trees go.

./TreesMin 'aabbbb
aabbbb
aaccbb
acccdb
ecccdb
eeefff'

Sample output:

010000
000001
001000
000010
100000
000100

Breakdown

O[15],                                  // Tree positions & region usage
U;                                      // Column usage (bitmask)
main(y,v)char**v;{                      // Recursive main function
  if(y>7)                               // Finished grid?
    for(;y-->2;printf("%06o\n",O[y]));  //  Print it (rows are padded octal)
  else                                  // Not finished:
    for(int*r,x=1,X=8;X<14;             //  Loop over columns
      U&x|*r|O[10-y]*9&x*9              //   Current cell violates rules?
        ?0                              //    Do nothing
        :(U^=O[9-y]=*r=x,               //   Else: mark cell
          *r=main(y+1,v),               //    Recurse
          U^=x)                         //    Unmark cell
      ,x*=8)                            //   Advance to next column
      r=O+v[1][y*7-++X]-88;             //   Region pointer for current iteration
}

It's an adaptation of my answer to the fastest-code version of this question. Doesn't have as much short-circuiting, but it's plenty fast enough for 6x6 grids.

\$\endgroup\$
1
\$\begingroup\$

Clingo, 66 bytes

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

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).

Demo

$ clingo plant.lp - -c n=6 <<EOF
> c(1,1,1). c(2,1,1). c(3,1,2). c(4,1,2). c(5,1,2). c(6,1,2).
> c(1,2,1). c(2,2,1). c(3,2,2). c(4,2,2). c(5,2,2). c(6,2,2).
> c(1,3,1). c(2,3,1). c(3,3,3). c(4,3,3). c(5,3,2). c(6,3,2).
> c(1,4,1). c(2,4,3). c(3,4,3). c(4,4,3). c(5,4,4). c(6,4,2).
> c(1,5,5). c(2,5,3). c(3,5,3). c(4,5,3). c(5,5,4). c(6,5,2).
> c(1,6,5). c(2,6,5). c(3,6,5). c(4,6,6). c(5,6,6). c(6,6,6).
> EOF
clingo version 5.1.0
Reading from plant.lp ...
Solving...
Answer: 1
c(1,1,1) c(2,1,1) c(3,1,2) c(4,1,2) c(5,1,2) c(6,1,2) c(1,2,1) c(2,2,1) c(3,2,2) c(4,2,2) c(5,2,2) c(6,2,2) c(1,3,1) c(2,3,1) c(3,3,3) c(4,3,3) c(5,3,2) c(6,3,2) c(1,4,1) c(2,4,3) c(3,4,3) c(4,4,3) c(5,4,4) c(6,4,2) c(1,5,5) c(2,5,3) c(3,5,3) c(4,5,3) c(5,5,4) c(6,5,2) c(1,6,5) c(2,6,5) c(3,6,5) c(4,6,6) c(5,6,6) c(6,6,6) t(1,5) t(2,1) t(6,2) t(3,3) t(5,4) t(4,6)
SATISFIABLE

Models       : 1+
Calls        : 1
Time         : 0.045s (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()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.