Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

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

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

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

New Code
Source Link
VisualMelon
  • 3.9k
  • 18
  • 39

C# 7 - 414 369369 327 bytes

Edit: changed input method, collapsed lookup table, and switched to well defined initial bounds ...and removed the pointless space in the last outer for-loop

using C=System.Console;class P{static void Main(){string D="",L;intvar W=0,H=0,i,j,z,k,q;for(;(L=CD=C.ReadLineIn.ReadToEnd())!=null;H+=W=L.LengthReplace("\r","")D+=L+="\n";int[]P;int W=D.IndexOf('\n')=>new[]{i=z%W,j=z/W+1,-iH=D.Length,-jz=H,i+jk,j-iq,-ic;int P()=>z%W*(k%3-j,i1)+z/W*(k/3-j};int[]B=null;for1)+H;var B=new int[9];for(z=H;z;z-->0;)if(D[z]%7<1)for(B=B??P(),k=8;kk=9;k-->0;>0&D[z]%7<1;)if(B[k]<(q=PB[k]<=P()[k]+1)B[k]=P()B[k]=q;for +1;for(;++z<H;C.Write(q>8q>9?'o':D[z]))for(q=k=8;kq=k=9;k-->0;)q*=B[k]<Pq*=(c=P()[k]-B[k])>0?0:B[k]==P()[k]c<0?21:1;2;}}

Try It Online

Complete program, takes input to standard in, prints it to standard out, uses #, ., and o. For each cell, it computes a 'profile' (which is the distance over 8 directions (it appears to compute a ninth for convenience, but this is always 0), and records 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.

using C=System.Console;

class P
{
    static void Main()
    {
        // \n 10
        // # 35
        // . 46
        // o 111
 
        string D="", // the whole map
    
        var L;D=C.In.ReadToEnd().Replace("\r",""); // initally each line of the map, later each line of output
    
        int W=0, // width
            H=0, // length W=D.IndexOf(width * height'\n')
            i+1, // x-indexwidth
            jH=D.Length, // y-indexlength
            zz=H, // position in map (decomposed into i and j by and for P)
            k, // bound index
            q;q, // bound distance, and later cell condition (0 -> outside, 8 -> inside, >8 -> on boudary)
    
         for(;(L=C.ReadLine())!=null;c; // read a line(free), while wecomparison canstore
                H+=W=L.Length) // record the width, and increment height
            D+=L+="\n"; // add a \n to the line (the rest of the code treats this'indexes' asinto a . cell), and add the lineprofile tofor the map
point z at index k
        // create profile foreffectively point
{i=z%W,j=z/W,-i,-j,i+j,j-i,-i-j,i-j,0}[k] (inside order is a bit different) (0 //const convertsis 1dalways totreated 2das very'inside cheaplybounds')
        // each non-zero-const entry describes the distance in one of the 8 directions: we want to maximise these to find the 'outer bounds'
        // thesethe 8non-zero-const bounds describe 8 lines, together an octogen
        int[]Pint P()=>new[]{i=z%W,j=z/W,-i,-j,i+j,j-i,-i=>z%W*(k%3-j,i1)+z/W*(k/3-j};1)+H; // new C#7 local method syntax (if you don't have C#7, you can test this code with the line below instead)
        //z=0;Systemk=0;System.Func<int[]>P=Func<int>P=()=>new[]{i=z%W,j=z/W,-i,-j,i+j,j-i,-i=>z%W*(k%3-j,i1)+z/W*(k/3-j};1)+H; // old lambda syntax (must pre-assign zk to make static checker happy)
        
        int[]B=null;var B=new int[9]; // our current bounds, each is initially null (must only call P() when on a #)
    
        for(z=H;z-->0;) // for each cell
 B[k] starts off a 0, P() has a +H term, and ifW+(D[z]%7<1H/W)<H //for ifW this>= cell3, so B[k] is #
assigned the first time we compare it (H-i-j always > 0)

        for(B=B??P(;z-->0;), // initfor Beach whencell
 we first hit a #
       for(k=9;k-->0& // for each bound
         k=8;k-->0;       D[z]%7<1;) // for each bound
if this cell is #
                if(B[k]<(q=PB[k]<=P()[k]+1)B[k]=P()B[k]=q;+1; // update bound if necessary (add one so that we define the bound _outside_ the hashes)
    
        // z=-1
        for (;++z<H; // for each cell
                C.Write(q>8q>9?'o':D[z])) // print the cell (if q > 89, then we are on the bounds, otherwise, spit out whatever we were before)
            // check we are not 'outside' any of the bounds, and that we are 'on' atleast one of them
            for(q=k=8;kq=k=9;k-->0;) // for each bound
                q*=B[k]<Pq*=(c=P()[k]-B[k])>0?0: // outside bound (q=0)
     (??0 is cheaper than (int) or .Value)
          B[k]==P()[k]?2: // on bound (if q != 0, then q becomesc<0?1: >// 8inside (preserve q)
                    1;2; // insideon bound (preserveif q != 0, then q becomes > 9)
    }
}

C# 7 - 414 369 bytes

using C=System.Console;class P{static void Main(){string D="",L;int W=0,H=0,i,j,z,k,q;for(;(L=C.ReadLine())!=null;H+=W=L.Length)D+=L+="\n";int[]P()=>new[]{i=z%W,j=z/W,-i,-j,i+j,j-i,-i-j,i-j};int[]B=null;for(z=H;z-->0;)if(D[z]%7<1)for(B=B??P(),k=8;k-->0;)if(B[k]<(q=P()[k]+1))B[k]=q;for (;++z<H;C.Write(q>8?'o':D[z]))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. For each cell, it computes a 'profile' (which is the distance over 8 directions), and records 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.

using C=System.Console;

class P
{
    static void Main()
    {
        // \n 10
        // # 35
        // . 46
        // o 111
 
        string D="", // the whole map
            L; // initally each line of the map, later each line of output
    
        int W=0, // width
            H=0, // length (width * height)
            i, // x-index
            j, // y-index
            z, // position in map (decomposed into i and j by and for P)
            k, // bound index
            q; // bound distance, and later cell condition (0 -> outside, 8 -> inside, >8 -> on boudary)
    
         for(;(L=C.ReadLine())!=null; // read a line, while we can
                H+=W=L.Length) // record the width, and increment height
            D+=L+="\n"; // add a \n to the line (the rest of the code treats this as a . cell), and add the line to the map
    
        // create profile for point
        // converts 1d to 2d very cheaply
        // each entry 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=z%W,j=z/W,-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)
        //z=0;System.Func<int[]>P=()=>new[]{i=z%W,j=z/W,-i,-j,i+j,j-i,-i-j,i-j}; // old lambda syntax (must pre-assign z to make static checker happy)
    
        int[]B=null; // our current bounds, initially null (must only call P() when on a #)
    
        for(z=H;z-->0;) // for each cell
            if(D[z]%7<1) // if this cell is #
                for(B=B??P(), // init B when we first hit a #
                    k=8;k-->0;) // for each bound
                    if(B[k]<(q=P()[k]+1))B[k]=q; // update bound if necessary (add one so that we define the bound _outside_ the hashes)
    
        // z=-1
        for (;++z<H; // for each cell
               C.Write(q>8?'o':D[z])) // print the cell (if q > 8, then we are on the bounds, otherwise, spit out whatever we were before)
            // check we are not 'outside' any 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)
    }
}

C# 7 - 414 369 327 bytes

Edit: changed input method, collapsed lookup table, and switched to well defined initial bounds ...and removed the pointless space in the last outer for-loop

using C=System.Console;class P{static void Main(){var D=C.In.ReadToEnd().Replace("\r","");int W=D.IndexOf('\n')+1,H=D.Length,z=H,k,q,c;int P()=>z%W*(k%3-1)+z/W*(k/3-1)+H;var B=new int[9];for(;z-->0;)for(k=9;k-->0&D[z]%7<1;)if(B[k]<=P())B[k]=P()+1;for(;++z<H;C.Write(q>9?'o':D[z]))for(q=k=9;k-->0;)q*=(c=P()-B[k])>0?0:c<0?1:2;}}

Try It Online

Complete program, takes input to standard in, prints it to standard out, uses #, ., and o. For each cell, it computes a 'profile' (which is the distance over 8 directions (it appears to compute a ninth for convenience, but this is always 0), and records 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 any with a 'o'. The commented code below explains how it all works.

using C=System.Console;

class P
{
    static void Main()
    {
        // \n 10
        // # 35
        // . 46
        // o 111
        
 
        var D=C.In.ReadToEnd().Replace("\r",""); // map

        int W=D.IndexOf('\n')+1, // width
            H=D.Length, // length
            z=H, // position in map (decomposed into i and j by and for P)
            k, // bound index
            q, // bound distance, and later cell condition (0 -> outside, 8 -> inside, >8 -> on boudary)
            c; // (free), comparison store
        
        // 'indexes' into a profile for the point z at index k
        // effectively {i=z%W,j=z/W,-i,-j,i+j,j-i,-i-j,i-j,0}[k] (inside order is a bit different) (0 const is always treated as 'inside bounds')
        // each non-zero-const entry describes the distance in one of the 8 directions: we want to maximise these to find the 'outer bounds'
        // the non-zero-const bounds describe 8 lines, together an octogen
        int P()=>z%W*(k%3-1)+z/W*(k/3-1)+H; // new C#7 local method syntax (if you don't have C#7, you can test this code with the line below instead)
        //k=0;System.Func<int>P=()=>z%W*(k%3-1)+z/W*(k/3-1)+H; // old lambda syntax (must pre-assign k to make static checker happy)
        
        var B=new int[9]; // our current bounds, each is initially null (must only call P() when on a #)
        // B[k] starts off a 0, P() has a +H term, and W+(H/W)<H for W >= 3, so B[k] is assigned the first time we compare it (H-i-j always > 0)

        for(;z-->0;) // for each cell
            for(k=9;k-->0& // for each bound
                D[z]%7<1;) // if this cell is #
                if(B[k]<=P())B[k]=P()+1; // update bound if necessary (add one so that we define the bound _outside_ the hashes)
        // z=-1
        for(;++z<H; // for each cell
                C.Write(q>9?'o':D[z])) // print the cell (if q > 9, then we are on the bounds, otherwise, spit out whatever we were before)
            // check we are not 'outside' any of the bounds, and that we are 'on' atleast one of them
            for(q=k=9;k-->0;) // for each bound
                q*=(c=P()-B[k])>0?0: // outside bound (q=0)    (??0 is cheaper than (int) or .Value)
                    c<0?1: // inside (preserve q)
                    2; // on bound (if q != 0, then q becomes > 9)
    }
}
New code
Source Link
VisualMelon
  • 3.9k
  • 18
  • 39

C# 7 - 414bytes414 369 bytes

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;}}

Edit: switched to 1D looping, computing i and j on the fly

using C=System.Console;class P{static void Main(){string D="",L;int W=0,H=0,i,j,z,k,q;for(;(L=C.ReadLine())!=null;H+=W=L.Length)D+=L+="\n";int[]P()=>new[]{i=z%W,j=z/W,-i,-j,i+j,j-i,-i-j,i-j};int[]B=null;for(z=H;z-->0;)if(D[z]%7<1)for(B=B??P(),k=8;k-->0;)if(B[k]<(q=P()[k]+1))B[k]=q;for (;++z<H;C.Write(q>8?'o':D[z]))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. ComputesFor each cell, it computes a 'profile' (which is the distance over 8 directions), and findsrecords 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.

Note: that for once in my life I'm using something from the current decade, and this code requires C# 7 to compile. If you do not have C# 7, there is one line that will need to be replaced, which is clearly marked in the code.

using C=System.Console;

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

        string D="", // the whole map
            L; // initally each line of the map, later each line of output
    
        int W=0, // width
            H=0, // length (width * height)
            i, // x-index
            j=0j, // y-index
            kz, // generalposition purposein countermap (decomposed into i and j by and for P)
            k, // bound index
            q; // tempbound storedistance, and later cell condition (0 -> outside, 8 -> inside, >8 -> on boudary)
    
        for(;(L=C.ReadLine())!=null; // read a line, while we can
            D+=L,H++) // add the lineH+=W=L.Length) to// record the mapwidth, and increment height
            W=L.Length;D+=L+="\n"; // recordadd a \n to the widthline (the rest of the code treats this as a . cell), and add the line to the map
    
        // create profile for point
        // converts 1d to 2d very cheaply
        // each indexentry 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[]{-ii=z%W,-jj=z/W,-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;Systemz=0;System.Func<int[]>P=()=>new[]{-ii=z%W,-jj=z/W,-i,-j,i+j,j-i,-i-j,i-j}; // old lambda syntax (staticmust checkerpre-assign isz worseto formake lambda,static mustchecker ensurehappy)
 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++z=H;z-->0;) // for each cell
                if(D[W*j+i]<36D[z]%7<1) // if this cell is #
                    for(B=B??P(),
  // init B when we first hit a #
                    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]<(B[k]<(q=P()[k]+1)?q:B[k];)B[k]=q; // update bound 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 resultz=-1
            for(L="", // line of result
                i=0;i<W;(;++z<H; // for each cell
                    L+=q>8C.Write(q>8?'o':D[W*j+i],D[z])) // addprint tothe linecell (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'not all'outside' any 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)
    }
}

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.

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

C# 7 - 414 369 bytes

Edit: switched to 1D looping, computing i and j on the fly

using C=System.Console;class P{static void Main(){string D="",L;int W=0,H=0,i,j,z,k,q;for(;(L=C.ReadLine())!=null;H+=W=L.Length)D+=L+="\n";int[]P()=>new[]{i=z%W,j=z/W,-i,-j,i+j,j-i,-i-j,i-j};int[]B=null;for(z=H;z-->0;)if(D[z]%7<1)for(B=B??P(),k=8;k-->0;)if(B[k]<(q=P()[k]+1))B[k]=q;for (;++z<H;C.Write(q>8?'o':D[z]))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. For each cell, it computes a 'profile' (which is the distance over 8 directions), and records 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.

Note: that for once in my life I'm using something from the current decade, and this code requires C# 7 to compile. If you do not have C# 7, there is one line that will need to be replaced, which is clearly marked in the code.

using C=System.Console;

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

        string D="", // the whole map
            L; // initally each line of the map, later each line of output
    
        int W=0, // width
            H=0, // length (width * height)
            i, // x-index
            j, // y-index
            z, // position in map (decomposed into i and j by and for P)
            k, // bound index
            q; // bound distance, and later cell condition (0 -> outside, 8 -> inside, >8 -> on boudary)
    
        for(;(L=C.ReadLine())!=null; // read a line, while we can
                H+=W=L.Length) // record the width, and increment height
            D+=L+="\n"; // add a \n to the line (the rest of the code treats this as a . cell), and add the line to the map
    
        // create profile for point
        // converts 1d to 2d very cheaply
        // each entry 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=z%W,j=z/W,-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)
        //z=0;System.Func<int[]>P=()=>new[]{i=z%W,j=z/W,-i,-j,i+j,j-i,-i-j,i-j}; // old lambda syntax (must pre-assign z to make static checker happy)
    
        int[]B=null; // our current bounds, initially null (must only call P() when on a #)
    
        for(z=H;z-->0;) // for each cell
            if(D[z]%7<1) // if this cell is #
                for(B=B??P(), // init B when we first hit a #
                    k=8;k-->0;) // for each bound
                    if(B[k]<(q=P()[k]+1))B[k]=q; // update bound if necessary (add one so that we define the bound _outside_ the hashes)
    
        // z=-1
        for (;++z<H; // for each cell
               C.Write(q>8?'o':D[z])) // print the cell (if q > 8, then we are on the bounds, otherwise, spit out whatever we were before)
            // check we are not 'outside' any 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)
    }
}
Thanks Sarge Borsch
Source Link
VisualMelon
  • 3.9k
  • 18
  • 39
Loading
Source Link
VisualMelon
  • 3.9k
  • 18
  • 39
Loading