Find the inside of a Loop

14

Task

Given a ASCII diagram of a loop

e.g.

....................
......@@@@@.........
......@...@.........
.....@@...@@@@@.....
....@@........@.....
....@........@@.....
....@@@@@@@@@@......
....................
....................

And a location on the loop

e.g.

(7,1)

You must find the inside and outside of the loop

e.g.

00000000000000000000
00000011111000000000
00000011111000000000
00000111111111100000
00001111111111100000
00001111111111100000
00001111111111000000
00000000000000000000
00000000000000000000

Specifications

  • You may take input for the diagram as a string separated by newlines or obvious equivalent

  • You will receive a coordinate on the loop (0 or 1 indexed) as part of your input. You may place your origin at any place you wish. You may take this coordinate in (<row>, <column>), (<column>, <row>) or as the linear position on the string. You may receive these data in any reasonable method. All characters on the loop will be the same as the character at that index.

  • Preferred output is a 2 dimensional array of truthy and falsy values, however strings of 1 and 0 separated by newlines or any obvious equivalent of the later two are accepted. The inside and outside must have different truth values but it does not matter which is which.

  • A loop is defined as a group of characters such that they are all the same character (e.g. @) and so that every character in the loop has a path to the original character (The character at the coordinate of input) that only passes through that same character (Taxicab geometry No diagonals).

  • The inside is all the loop itself and the places that cannot reach the edge of the diagram without crossing the loop.

  • The outside is everywhere else

  • This is

Test Cases

PasteBin

Post Rock Garf Hunter

Posted 2017-01-17T15:27:15.207

Reputation: 55 382

Can we also take the coordinates as linear coordinate in the string? – flawr – 2017-01-17T16:15:32.060

@flawr You may. – Post Rock Garf Hunter – 2017-01-17T16:16:45.153

Are we allowed to take the diagram as a matrix of characters, sth. like [['.', '.'],['.', '@']] instead of a string with newlines? – hbaderts – 2017-01-17T16:19:41.263

@hbaderts That is an obvious equivalent – Post Rock Garf Hunter – 2017-01-17T16:20:16.297

1@WheatWizard Thanks for the additional test case! I would however recommend to put them into a snippet or in a gist / pastebin, in order to make the challenge a bit more decluttered=) – flawr – 2017-01-17T21:41:03.843

Can we assume the input will be at least 2x2? Or if it¡s for example 1xN (a single line) how is insideness defined? – Luis Mendo – 2017-01-17T23:40:48.277

@LuisMendo For a line the inside will be the "loop" and nothing else – Post Rock Garf Hunter – 2017-01-17T23:42:36.083

If I take the input as a linear coordinate into the 2D char array, can it be column-major (i.e. down, then across)?

– Luis Mendo – 2017-01-17T23:55:24.870

@LuisMendo Go ahead. I don't care how you take the data in I am not concerned with data processing but more the underlying algorithm. – Post Rock Garf Hunter – 2017-01-18T00:00:56.353

@WheatWizard Thanks! Anyway my algorithm was flawed because it assumed the loop was convex :-( – Luis Mendo – 2017-01-18T00:02:52.710

Can we assume the loop character to be a specific character (like '@')? Also, can we assume the other characters to be the same, and can we assume it will be a specific character (like '.')? – infmagic2047 – 2017-02-01T13:59:33.317

Nevermind, I saw it is disallowed in the test cases. – infmagic2047 – 2017-02-01T14:12:24.273

Answers

6

MATLAB, 163 159 146 78 bytes

function m=f(m,y,x);[~,i]=bwfill(m~=m(y,x),x,y,8);m=m*0;m(i)=1;m=bwfill(m,'h')

Thanks @rahnema1 for -66 bytes!!!

Now it does work on Try it online! BUT a few adjustments were needed, as MATLAB and Octave are not entirely compatible.

Explanation

First we make a binary image that just masks all the characters that are equal to the initial character. Then we determine the connected component the initial character is in.

% determine the connected component that is contains initial character

[~,i]=bwfill(m~=m(y,x),x,y,8);     % i contains the indices of the connected component
m=m*0;m(i)=1;                      % create an image of the connected component

After that we create an image of that connected component and apply fill all the "holes" in the image.

m=bwfill(m,'h')

flawr

Posted 2017-01-17T15:27:15.207

Reputation: 40 560

I think for golfing Octave is better so you may reduce it to at least 72 bytes – rahnema1 – 2017-01-17T22:23:16.080

@rahnema1 I already started in MATLAB, so I'm not gonna change this submission now, but thanks for the suggestion=) – flawr – 2017-01-17T22:28:19.490

5

MATLAB: 67 bytes

function A=f(A,r,c),A=bwlabel(A==A(r,c),4);A=imfill(A==A(r,c),'h');

A couple caveats:

  • A is assumed to be a character array.
  • Indices in MATLAB are 1-based, with rows indexed first. It is assumed these changes would be made to the function input (i.e. the question example would be called as output = f(A,2,8)).
  • bwlabel and imfill are part of the Image Processing Toolbox.

gnovice

Posted 2017-01-17T15:27:15.207

Reputation: 261

1welcome to codegolf! – rahnema1 – 2017-01-17T22:46:59.350

@rahnema1: Amazed I didn't visit sooner, since I did some golfing on SO before this site was born. – gnovice – 2017-01-17T22:48:02.607