17
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Related: 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. \$\endgroup\$ – Reto Koradi Dec 14 '15 at 5:38
  • \$\begingroup\$ @RetoKoradi It's also not quite the same operation. (The fact that the chosen cell itself is toggled too makes this significantly trickier.) \$\endgroup\$ – Martin Ender Dec 14 '15 at 15:24
  • \$\begingroup\$ Can I request the input be like <grid><RETURN><coordinate_x><RETURN><coordinate_y><RETURN>? \$\endgroup\$ – cat Dec 14 '15 at 15:41
  • \$\begingroup\$ whichever format and separation: Does that mean they can be separate inputs (that is, "separator" is "enter" key)? \$\endgroup\$ – Luis Mendo Dec 14 '15 at 23:04
  • \$\begingroup\$ @LuisMendo Yes, if that works. \$\endgroup\$ – EEEEEEridan Dec 15 '15 at 0:10
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ please let me know if there is an online compiler available for matl \$\endgroup\$ – Abr001am Dec 15 '15 at 17:10
  • \$\begingroup\$ Not online yet, sorry. Only the Matlab-based one I linked in the title \$\endgroup\$ – Luis Mendo Dec 15 '15 at 17:32
0
\$\begingroup\$

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""}}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.