Skip to main content
2 of 5
Thanks Sarge Borsch
VisualMelon
  • 3.9k
  • 18
  • 39

C# - 414bytes

using C=System.Console;class P{static void Main(){string D="",L;int W=0,H=0,i,j=0,k,q;for(;(L=C.ReadLine())!=null;D+=L,H++)W=L.Length;int[]P()=>new[]{-i,-j,i,j,i+j,j-i,-i-j,i-j};int[]B=null;for(;j<H;j++)for(i=0;i<W;i++)if(D[W*j+i]<36)for(B=B??P(),k=8;k-->0;)B[k]=B[k]<(q=P()[k]+1)?q:B[k];for (j=0;j<H;j++,C.WriteLine(L))for(L="",i=0;i<W;L+=q>8?'o':D[W*j+i],i++)for(q=k=8;k-->0;)q*=B[k]<P()[k]?0:B[k]==P()[k]?2:1;}}

Complete program, takes input to standard in, prints it to standard out, uses #, ., and o. Computes a 'profile' (which is the distance over 8 directions), and finds a maximum of each of these. It then writes out the whole map again, and replaces any cell which is both on a boundary and not outside of any with a 'o'. The commented code below explains how it all works.

As per my answer to Save the Geese from Extinction, this produces the smallest octagon (valid circumnavigation with largest area) which bounds the island.

Example usage and output:

type t7.txt | IslandGolf1.exe

.........ooooooooooo....
........o....#......o...
.......o...#.#.##...#o..
......o....#.#.###.##.o.
.....o....########.##..o
....o.....############.o
...o.#....############.o
..o#.###.##############o
.o##.##################o
o.####################.o
o..##################..o
o.##################...o
o...################...o
o###################...o
o#####################.o
o.##################..o.
o####################o..
o#...##############.o...
o##...#############o....
o#.....###....#...o.....
.o.....#.........o......
..ooooooooooooooo.......

Formatted and commented code:

using C=System.Console;

class P
{
    static void Main()
    {
        // # 35
        // . 46
        // o 111

        string D="",L;
    
        int W=0, // width
            H=0, // height
            i, // x-index
            j=0, // y-index
            k, // general purpose counter
            q; // temp store
    
        for(;(L=C.ReadLine())!=null; // read a line, while we can
            D+=L,H++) // add the line to the map, increment height
            W=L.Length; // record the width
    
        // create profile for point
        // each index describes the distance in one of the 8 directions: we want to maximise these to find the 'outer bounds'
        // these 8 bounds describe 8 lines, together an octogen
        int[]P()=>new[]{-i,-j,i,j,i+j,j-i,-i-j,i-j}; // new C#7 local method syntax (if you don't have C#7, you can test this code with the line below instead)
        //i=0;System.Func<int[]>P=()=>new[]{-i,-j,i,j,i+j,j-i,-i-j,i-j}; // old lambda syntax (static checker is worse for lambda, must ensure i is assigned)
        int[]B=null; // our current bounds, initially null (must only call P() when on a #)
    
        // TODO: swap j/i loops, reuse j=H in below
        for(;j<H;j++) // for each line
            for(i=0;i<W;i++) // for each cell
                if(D[W*j+i]<36) // if this cell is #
                    for(B=B??P(),
                        k=8;k-->0;) // for each bound
                        //B[k]=B[k]<P()[k]+1?P()[k]+1:B[k]; // update if necessary
                        B[k]=B[k]<(q=P()[k]+1)?q:B[k]; // update if necessary (add one so that we define the bound _outside_ the hashes)
    
        for (j=0;j<H;j++, // for each line
                C.WriteLine(L)) // print line of result
            for(L="", // line of result
                i=0;i<W; // for each cell
                    L+=q>8?'o':D[W*j+i], // add to line (if q > 8, then we are on the bounds, otherwise, spit out whatever we were before)
                    i++) // must do this after and independant of D[W*j+i]
                // check we are 'inside' all of the bounds, and that we are 'on' atleast one of them
                for(q=k=8;k-->0;) // for each bound
                    q*=B[k]<P()[k]?0: // outside bound (q=0)
                      B[k]==P()[k]?2: // on bound (if q != 0, then q becomes > 8)
                      1; // inside (preserve q)
    }
}
VisualMelon
  • 3.9k
  • 18
  • 39