Flip the switches

17

After you wake in a dark room, you see a rectangular grid of lights with corresponding switches. You decide to use a coordinate system with the bottom left light representing (1,1) and the coordinates increasing as you go up (y direction) and to the right (x direction). The grids are identical in that flipping the switch at (a,b) toggles (turns from off to on or from on to off) the light at (a,b) as well as the lights in the same column or same row as (a,b).

Your input will be a rectangular grid of two distinct characters representing on and off (I use 1 and 0, respectively). Also, a part of the input will be a series of at least one coordinate pair (with whatever format and separation you want) that will represent switches that you flip.

The output will be the same grid, with the "flip" applied at each of the coordinate pairs that were given as input. You may choose how to format your input, though the output must be a grid, not an array.


Samples

Sample input 1

111111
111111
111111
111111

(3,2)

Sample output 1

110111
110111
000000
110111

Sample input 2

01101
10100
00010
11111
10110

(1,1), (5,5)

Sample output 2

00010
00101
10011
01110
01000

Sample input 3

1

(1,1)

Sample output 3

0

Sample input 4

00000
11111
00000
11111

(2,3), (2,3)

Sample output 4

00000
11111
00000
11111

This is code golf. Standard rules apply. Shortest code in bytes wins.

EEEEEEridan

Posted 2015-12-14T04:52:43.130

Reputation: 181

Related: http://codegolf.stackexchange.com/questions/65738/crack-the-safe. Not the same problem, though. The goal of the other challenge was to find out which moves to make, this one is about applying a given list of moves.

– Reto Koradi – 2015-12-14T05:38:03.393

@RetoKoradi It's also not quite the same operation. (The fact that the chosen cell itself is toggled too makes this significantly trickier.) – Martin Ender – 2015-12-14T15:24:57.687

Can I request the input be like <grid><RETURN><coordinate_x><RETURN><coordinate_y><RETURN>? – cat – 2015-12-14T15:41:19.050

whichever format and separation: Does that mean they can be separate inputs (that is, "separator" is "enter" key)? – Luis Mendo – 2015-12-14T23:04:55.487

@LuisMendo Yes, if that works. – EEEEEEridan – 2015-12-15T00:10:43.733

What do you mean with "output must be a grid"? Is a list of lists a grid? Or do we have to print a graphical representation (newline separated rows)? If the latter, does the "two distinct characters" rule still apply, i.e. no spaces or other separators in-between? – nimi – 2015-12-15T01:27:13.143

Answers

3

CJam, 37 36 bytes

qN/W%(~{1$::!\{1a\Te[f.|z}/..^}/W%N*

Input should have the grid first, using any two non-NULL characters that only differ in the last bit (so 0 and 1 work), followed by a CJam-style list of coordinate pairs.

01101
10100
00010
11111
10110
[[1 1] [5 5]]

Test it here.

Martin Ender

Posted 2015-12-14T04:52:43.130

Reputation: 184 808

3

Dyalog APL, 20 bytes

{⍵≠⊃⊃≠/∘.∨/¨⍺=⊂⍳¨⍴⍵}

This is a dyadic function that takes the initial grid on the right and the list of coordinates on the left.

To input a single coordinate pair, use e.g. (⊂2 3) as the left argument.

Try it here.

lirtosiast

Posted 2015-12-14T04:52:43.130

Reputation: 20 331

1

MATL, 39 bytes

i,-1H$X!tyZ}:XJx:!XIxi"I@1)=J@2)=|+]2\X!

Input is of the following form (the example corresponds to sample input 2 in the challenge):

[0 1 1 0 1; 1 0 1 0 0; 0 0 0 1 0; 1 1 1 1 1; 1 0 1 1 0]  
[1 5; 1 5]

First input is a matrix defining the grid of 0 and 1. ; is the row separator. Second input is a matrix of coordinate pairs, where each column is a pair.

Example

>> matl i1_2$X!tyZ}:XJx:!XIxi"I@1)=J@2)=|+]2\X!
> [0 1 1 0 1; 1 0 1 0 0; 0 0 0 1 0; 1 1 1 1 1; 1 0 1 1 0]
> [1 5; 1 5]
0 0 0 1 0
0 0 1 0 1
1 0 0 1 1
0 1 1 1 0
0 1 0 0 0

Explanation

i,           % input matrix
-1H$X!       % rotate clockwise to accomodate input to matrix coordinates
tyZ}         % get numbers of rows (r) and of cols (c)
:XJx         % row vector 1,2,,...,c. Copy to clipboard J and delete
:!XIx        % col vector 1,2,,...,r. Copy to clipboard I and delete
i            % input coordinates (matrix; each col is a coordinate)
"            % for each col of coordinate matrix
    I@1)=    % compare col vector of rows with row from coordinate
    J@2)=    % compare row vector of cols with col from coordinate
    |        % "or" with singleton expansion to generate mask
    +        % add that to matrix of values
]            % end for
2\           % modulo 2 to transform result into zeros and ones
X!           % undo rotation

Luis Mendo

Posted 2015-12-14T04:52:43.130

Reputation: 87 464

please let me know if there is an online compiler available for matl – Abr001am – 2015-12-15T17:10:43.520

Not online yet, sorry. Only the Matlab-based one I linked in the title – Luis Mendo – 2015-12-15T17:32:34.937

0

Ruby 114 bytes

Takes as input:

g, an array of arrays representing the starting grid.

o, an array of points, each point being an array with two elements, like [x, y].

->g,o{o.map{|i|(r=g[i[1]-1])[e=(i[0]-1)]-=1;r.map!{|j|j+1};g.map{|x|x[e]+=1}};puts g.map{|i|i.map{|j|j%2}.join""}}

MegaTom

Posted 2015-12-14T04:52:43.130

Reputation: 3 787