Comments on Connect the corners without 4 in a row
Parent
Connect the corners without 4 in a row
Connect opposite corners of a rectangle of characters without putting 4 characters in a row.
Input
- Two numbers, W and H, representing the width and height of the rectangle
- Each number will be in the range 2 to 70
Output
- A rectangular grid of characters, of width W and height H
- You may choose any 2 distinct characters to represent path and background
- The output must contain 2 connected path characters in diagonally opposite corners of the grid
- Connected means there is a path from one to the other moving only between adjacent path characters
- Adjacent means 1 character away horizontally or vertically (but not both - diagonal adjacency does not count as connection for this challenge)
- The output must not contain 4 adjacent path characters in a line horizontally or vertically
- There is no limit to how many of the characters may be path characters, provided there are never 4 in a row.
- There may be redundant sections connected to the path
- There may be disconnected sections of path provided the main path meets the requirements
Examples
These examples use #
for a path character and .
for the background.
Valid examples for input 4, 3
##..
.##.
..##
.###
.#..
##..
#...
##..
.###
##.#
.###
##.#
Invalid examples for input 4, 3
4 in a line horizontally:
#...
####
...#
Depends on diagonal adjacency, which does not count as connection:
#...
.#..
..##
Does not connect diagonally opposite corners:
###.
..##
###.
Valid examples for input 7, 5
###....
..#....
..###..
....#..
....###
##.....
.##....
..##...
...##..
....###
#......
#......
##.###.
.#.#.##
.###..#
###....
..###..
###.###
#...#.#
###...#
Invalid examples for input 7, 5
Does not connect two diagonally opposite corners:
###....
..###..
....###
...##..
...#...
Has 4 in a row horizontally:
####...
...#...
...###.
.....#.
.....##
Has 4 in a row vertically:
###....
..#....
..#.###
..###.#
......#
Depends on diagonal adjacency, which does not count as connection:
##.....
..##...
....##.
......#
......#
Validator
You can check a specific output using this path validator, which will give a reason for any failed validations.
Explanations are optional, but I'm more likely to upvote answers that have one.
Post
Haskell + hgl, 134 bytes
k=cy"X.XX"
x#1=[4,0,9,9]!x
2#y=8
3#y=[8,9,4,4]!y
x#3=[0,3]!x
_#_=0
x?y|(n,j)<-fvD 4$x%4#(y%4)=tk y$dr j$tk x<dr n<cy[dr2 k,k,cy".X",k]
I first set up a pretty dense background pattern which doesn't break any rules:
XXX.XXX.XXX.XXX.XXX.
X.XXX.XXX.XXX.XXX.XX
.X.X.X.X.X.X.X.X.X.X
X.XXX.XXX.XXX.XXX.XX
XXX.XXX.XXX.XXX.XXX.
X.XXX.XXX.XXX.XXX.XX
.X.X.X.X.X.X.X.X.X.X
X.XXX.XXX.XXX.XXX.XX
XXX.XXX.XXX.XXX.XXX.
X.XXX.XXX.XXX.XXX.XX
.X.X.X.X.X.X.X.X.X.X
X.XXX.XXX.XXX.XXX.XX
XXX.XXX.XXX.XXX.XXX.
X.XXX.XXX.XXX.XXX.XX
.X.X.X.X.X.X.X.X.X.X
X.XXX.XXX.XXX.XXX.XX
XXX.XXX.XXX.XXX.XXX.
X.XXX.XXX.XXX.XXX.XX
.X.X.X.X.X.X.X.X.X.X
X.XXX.XXX.XXX.XXX.XX
All we have to do is make sure the pattern is aligned so there's always a path. I wish I had some clever way of doing this, but I just use a bunch of magic numbers I calculated by trial and error.
Since the pattern repeats with period 4, I just tried every pair of numbers on the range 1-4 and found a shifting that lined up. I encoded these in base 4 and that's what the program uses.
There might be an opportunity to optimize this by finding better numbers.
0 comment threads