Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Connect the corners without 4 in a row

Parent

Connect the corners without 4 in a row

+3
−0

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.

History

0 comment threads

Post
+1
−0

Perl, 276 bytes

Takes two space-delimited numbers on stdin. Prints route on stdout.

perl -aE'sub R{my($x,$y)=@_;if(0<=$x<$W&&0<=$y<$H&&$r[$y][$x]==0){local$r[$y][$x]=local$c[$x][$y]=1;if(!grep"@$_"=~/1 1 1 1/,@r,@c){if(!$x&&!$y){say@$_
for@r;exit}R($x,$y-1);R($x-1,$y);++($W<$H?$x:$y);R($x,$y)}}}($W,$H)=@F;@r=map[(0)x$W],1..$H;@c=map[(0)x$H],1..$W;R$W-1,$H-1'

A recursive backtracker.

Restricts choices slightly to reduce amount of backtracking.

perl -aE'
    sub R {
        my ($x,$y) = @_;

        if (
            # within bounds
            0<=$x<$W &&
            0<=$y<$H &&

            # not already visited
            $r[$y][$x]==0
        ) {
            # mark current position
            local $r[$y][$x] = local $c[$x][$y] = 1;

            if (
                # no row or column has run of more than three 1s
                ! grep "@$_" =~ /1 1 1 1/, @r, @c
            ) {
                # reached second corner
                if (!$x && !$y) {
                    say @$_ for @r;
                    exit
                }

                # extend path

                R($x,$y-1); # first try up
                R($x-1,$y); # then try left

                # then
                #   if tall+narrow, try right
                #   if wide+short, try down
                #   if square, also try down
                # (going right backtracks excessively in some
                #  cases (eg. 40 40); but I think would be
                #  correct if order was left,up)
                ++( $W<$H ? $x : $y ); R($x,$y)
            }
        }
    }

    # load dimensions
    ($W,$H) = @F;

    # initialise grid, store twice for ease of lookup
    @r = map [(0)x$W], 1..$H; # list of rows
    @c = map [(0)x$H], 1..$W; # list of columns

    # generate path
    R $W-1,$H-1
'

Try it online!

History

1 comment thread

No time limit (3 comments)
No time limit
trichoplax‭ wrote 6 months ago

This challenge doesn't specify a time limit, so provided your output is correct up to the maximum input of 70 by 70, taking a long time to run is fine.

I've checked your current code with 70 by 70 and it outputs in under a second on TIO (and I've confirmed it's valid with the validator). How much slower was your shorter version...?

jhnc‭ wrote 6 months ago

I'm guessing hours to years but I only let it run for a minute or so. I could see from my debug output that it was filling the grid in a largely up-left-down-up-left-down pattern and attempting a fruitless set of walks that would probably ultimately end up going most of the way back to the start point. 72 by 60ish is a large search space. It did give valid results for some other sizes when it didn't get lost but no guarantee it wasn't buggy. Not to worry. I can't find it now anyway. I guess I overwrote it at some point when I was experimenting with other designs.

jhnc‭ wrote 6 months ago · edited 6 months ago

Here's a similar example by tweaking the current code to try both right and down. It's instrumented to display an animation:

echo 72 19 | perl -aE'sub R{my($x,$y)=@_;if(0<=$x<$W&&0<=$y<$H&&$g[$y][$x]==0){local$g[$y][$x]=1;if(!grep"@$_"=~/1 1 1 1/,@g,map{$a=$_;[map$g[$_][$a],0..$H-1]}0..$W-1){say"\033[2J\033[0;0H";say@$_ for@g;if(!$x&&!$y){say"\033[2J\033[0;0H";say@$_ for@g;exit}R($x,$y-1);R($x-1,$y);R($x+1,$y);R($x,$y+1)}}}($W,$H)=@F;@g=map[(0)x$W],1..$H;R($W-1,$H-1)'

Might actually be heat death of the universe kind of runtime rather than just years.