# Crack the safe!

You are given an n by n (n itself is optional input) grid filled with 0s and 1s (or any other character of your choice). You aim is to make every cell the same (either 0 or 1). You can make a series of moves as defined below (note dissimilarity with Puzzling SE link):

• Select a cell.
• Every cell in the same row and column (except the cell itself) gets changed into its opposite. 0 to 1 and 1 to 0.

Output the minimum number of moves required to complete the task. If unsolvable, output anything except a non-negative integer. Shortest code wins.

Sample data

1 0 0
0 0 0
0 0 0


-1

1 1 1
1 1 1
1 1 1


0

1 0 1
0 1 0
1 0 1


1

1 1 1 1
0 0 0 0
0 0 0 0
1 1 1 1


2

0 1 0 1
1 0 1 0
1 0 1 0
0 1 0 1


2

• What to do in case the puzzle is unsolvable? E.g. 1000 (rearranged as a square, doesn't matter how).
– orlp
Dec 4 '15 at 15:08
• Related. Dec 4 '15 at 15:42
• @orlp Any output that is not a number will do. Dec 4 '15 at 15:43
• Do we need to parse the input or can it be any already filled array datatype? Dec 4 '15 at 16:11
• What is the solution to the first test case? I'm getting no solutions for it. Dec 4 '15 at 23:09

# Matlab 171 bytes

The input should be a 2d matrix, so you would call it like c([1,1,1,1;0,0,0,0;0,0,0,0;1,1,1,1]) (semicolons start a new row). This function just bruteforces all possible moves, so we get a runtime of O(2^(n^2)).

### How it is done

This is done by choosing all possible ways to fill another matrix of the same size with ones and zero, this is basically counting in binary wich where each entry of the matrix represents a certain power of 2.

Then we perform the moves on those cells that are 1, this is done by the sum (mod 2) of two two dimensional convolution with an vector of ones of size 1xn and nx1.

Finally we decide if those moves actually produced the desired result, by computing the standard deviation over all entries. The standard deviation is only zeros if all entries are the same. And whenever we actually found the desired result we compare it with the number of moves of previous solutions. The function will return inf if the given problem is not solvable.

### Math?

It is actually worth noting that all those moves together generate an abelian group! If anyone actually manages to calssify those groups please let me know.

Golfed version:

function M=c(a);n=numel(a);p=a;M=inf;o=ones(1,n);for k=0:2^n-1;p(:)=dec2bin(k,n)-'0';b=mod(conv2(p,o,'s')+conv2(p,o','s'),2);m=sum(p(:));if ~std(b(:)-a(:))&m<M;M=m;end;end


Full version (with the output of the actual moves.)

function M = c(a)
n=numel(a);
p=a;
M=inf;                                               %current minimum of number of moves
o=ones(1,n);
for k=0:2^n-1;
p(:) = dec2bin(k,n)-'0';                         %logical array with 1 where we perform moves
b=mod(conv2(p,o,'same')+conv2(p,o','same'),2);   %perform the actual moves
m=sum(p(:));                                     %number of moves;
if ~std(b(:)-a(:))&m<M                           %check if the result of the moves is valid, and better
M=m;
disp('found new minimum:')
disp(M)                                      %display number of moves of the new best solution (not in the golfed version)
disp(p)                                      %display the moves of the new best solution                               (not in the golfed version)
end
end


# Perl 5, 498 bytes

This accepts 'n' and the desired result, and outputs the count, or 'X' if none.

For example:

perl ./crack.golf.pl 3 000111111


gives 2. It will only work when n^2 <= 64, so n <= 8. Although it's pretty slow even with n as low as 5. It builds a n^3 bit array, and sorts a 2^(n^2) array beforehand, because why not?

I have wasted a couple of linefeeds here for readability:

$n=shift;$y=shift;$p=$n*$n;@m=(0..$n-1);@q=(0..$p-1);@v=(0..2**$p-1);@d=map{0}(@q);@b=map{$r=$_;map{$c=$_;$d[$r*$n+$_]^=1 for(@m);$d[$_*$n+$c]^=1 for(@m);$j=0;$k=1;
map{$j|=$k*$d[$_];$k<<=1;}@q;@d=map{0}(@q);$j;}@m}@m;for$k(sort{$a-><=>$b->}map{$z=0;map{$z+=$_}split(//,sprintf"%b",$_);[$z,$_]}@v){$l=sprintf"%0${p}b",$k->;
@m=map{$_}split(//,$l);$s=0;for(@q){$s^=$b[$_]if$m[$_];}$z=0;map{$z+=$_}split(//,sprintf"%b",$_);if($y eq sprintf"%0${p}b",$s){print"$k->\n";exit 0;}}print"X\n";
{"ft_lang_label":"__label__en","ft_lang_prob":0.7770569,"math_prob":0.99642307,"size":2019,"snap":"2021-43-2021-49","text_gpt3_token_len":588,"char_repetition_ratio":0.108188584,"word_repetition_ratio":0.045602605,"special_character_ratio":0.29123327,"punctuation_ratio":0.16869919,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9994481,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-25T16:56:49Z\",\"WARC-Record-ID\":\"<urn:uuid:e74ef724-89fe-49ba-86c2-d8a19f8247e7>\",\"Content-Length\":\"189842\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:808e24c0-eb6c-4003-a583-89fc325a8c23>\",\"WARC-Concurrent-To\":\"<urn:uuid:22bc3459-06c6-4ea0-8bdf-645c230cab46>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://codegolf.stackexchange.com/questions/65738/crack-the-safe\",\"WARC-Payload-Digest\":\"sha1:BXWDUQTIIHEOZS5UMWY7OT3LEYKJJ53W\",\"WARC-Block-Digest\":\"sha1:C5NY3EDDTUGRMPRRPVTBQU26NOSXORSO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587719.64_warc_CC-MAIN-20211025154225-20211025184225-00704.warc.gz\"}"}