in reply to Google Code Jam 2019 Round 1A Problem 1: Pylons

Crummy problem :( ----- (because random)

This is a permutation problem with a weak ordering requirement.

"shuffle" the grid indexes, about half the time always taking the first non-conflict from the array of empty indexes gives a valid order, if not, reshuffle till it works.
I am seeing on average slightly less than one reshuffle per grid solution over all the grids.

Processes all 361 grids in under 2 seconds total typically.

( slight cheat: All IMPOSSIBLEs are hard coded (HINT: there are very few) :)

It's not in final submit form because I wanted to test all grid sizes.

#!/usr/bin/perl # https://codingcompetitions.withgoogle.com/codejam/round/000000000005 +1635/0000000000104e03 use strict; use warnings; use List::Util qw( first shuffle ); my $w; my $case = 0; for my $row ( 2 .. 20 ) # testing all sizes, needs change to read i +nput file { for my $col ( 2 .. 20 ) { $case++; if( $row == 2 && $col < 5 || $row < 5 && $col == 2 || $row == 3 && $col == 3) { print "Case #$case: IMPOSSIBLE\n"; # h +ard coded next; } $w = $col; my @pylons = 0; # first pylon in upper le +ft corner my @empty = shuffle 1 .. $row * $col - 1; while( @empty ) { if( my $try = first { ! conflict($pylons[-1], $_) } @empty ) { push @pylons, $try; @empty = grep $_ != $try, @empty; } else { push @empty, shuffle splice @pylons, 1; # +reshuffle } } print "Case #$case: POSSIBLE\n"; # uncomment next line to print grid locations #print +(int 1 + $_ / $w) . ' ' . (1 + $_ % $w) . "\n" for @pylons +; } } sub conflict { my ($x, $y) = @_; my ($r, $c, $rr, $cc) = (int $x / $w, $x % $w, int $y / $w, $y % $w) +; $r == $rr || $c == $cc || $r + $c == $rr + $cc || $r - $c == $rr - $ +cc; }

Replies are listed 'Best First'.
Re^2: Google Code Jam 2019 Round 1A Problem 1: Pylons
by LanX (Saint) on Apr 15, 2019 at 17:01 UTC
    > ( slight cheat: All IMPOSSIBLEs are hard coded (HINT: there are very few) :)

    Lemme guess, the remains were proven to be POSSIBLES by brute-force searching (N.B. only inside the max-boundaries) and not by applying mathematical reasoning?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      It is a "code jam" and not a "math jam" :)

        "JAM" it certainly is. ;)

        But my point is a bit different, I once participated at a golfing competition where the winner had impossibly short code.

        Turned out he just hardcoded the desired result into a print.

        Precomputing all results for a finite input set indeed looks like ... jam.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice