32
1
The other day, our team went to an escape room. One of the puzzles involved a board of six mechanical switches where you had to find the correct combination of on and off in order to unlock a box, somewhat like this:
-v-v-v-
-v-v-v-
Being developers, we decided it would be more efficient to try every single one of 2^6=64 combinations than actually solve the puzzle. So we assigned some poor guy to do some binary counting:
-v-v-v-
-v-v-v-
-v-v-v-
-v-v-^-
-v-v-v-
-v-^-v-
-v-v-v-
-v-^-^-
and so on.
The challenge
Write a program that, given the switches all in off position as a string formatted as above, generates all combinations of on and off in any order.
You can write either a full program or a function. Thus, your program can either take in input through stdin, a file, or as a single string argument, and either return or print the output. If returned, the output may be in a list/array/etc. rather than a single string. If the output is a single string, the boards should be separated by newlines (trailing newlines are allowed.)
The input strings will match the regex r'((-v)+-)(\n(-v)+-)*'
and represent one board with all switches off. This means no zero case, and switches are left-aligned. Each row might not have the same number of switches.
Each output board should be of the exact same format as the input, except that the v's may be replaced by ^'s as required. The output boards can be separated by any number of newlines.
Since runtime is naturally O(2^n) in the number of switches, your code will not be tested on any more than 10 switches in any arrangement.
This is code-golf, so shortest code in number of bytes wins.
Sample inputs and outputs
Input:
-v-
Possible output:
-v-
-^-
Input:
-v-
-v-
Possible output:
-^-
-^-
-^-
-v-
-v-
-^-
-v-
-v-
Since it's extremely tedious to check your answer for bigger numbers of switches, here's a Python script as a sanity check tool. (I've included a currently commented-out snippet to generate expected output from a given input file in case you want more test cases.) It's quite a bit less flexible in terms of input and output than the spec, unfortunately; put the input string in a file named 'input' and the newline-separated output (sorry, no list formatting) in a file named 'output' in the same directory and run python3 sanitycheck.py
.
8nice first challenge! – Giuseppe – 2019-07-19T17:51:13.247
12
Hopefully, the "poor guy" knew about Gray code in order to only flip one bit between each combination.
– Eric Duminil – 2019-07-20T12:00:27.7831Time is our most precious asset, don't waste it in vain. – Pedro Lobito – 2019-07-20T12:54:09.807
6Given the theme, I'm disappointed you didn't require an order that requires the least amount of toggles (e.g. 00->01->11->10 has 3 toggles while 00->01->10->11 has 4) -- Fellow brute force escaper – ikegami – 2019-07-20T18:43:54.427
@EricDuminil Lol yeah, we were all a little ignorant, thanks for enlightening me to that though – Rin's Fourier transform – 2019-07-20T19:57:45.290
@ikegami ah, fair, I thought it would be interesting to see the most golfy ways to fit them into each different programming language though rather than just one or two algorithms that everyone uses.
Even so, it looks like bunch of them did turn out to just be binary counting xd – Rin's Fourier transform – 2019-07-20T19:58:59.930
@Giuseppe and five others Thank you so much! I was super pleasantly surprised to wake up to 18 answers :v – Rin's Fourier transform – 2019-07-20T20:01:13.527
1
@ikegami That's the Gray code I was talking about.
– Eric Duminil – 2019-07-21T11:07:26.210@Eric Duminil, Yeah. I only noticed that your comment was talking about the same thing far afterwards. – ikegami – 2019-07-21T11:36:21.707
2@EricDuminil: if the mechanical switches were not buttons (and maybe even if), then most likely, the difference the time needed between switching one, two and three consecutive switches (which you could probably do almost simultaneously) wouldn't be large enough to offset the extra mental work to follow the Gray code. – tomasz – 2019-07-21T15:26:37.217
1
@tomasz: Maybe. But aren't we all here for the "extra mental work"? When I'm afraid that a programming task is too complex or might take too long, I come back to read this answer.
– Eric Duminil – 2019-07-22T08:52:28.277@EricDuminil: My point is, the extra mental work will give you overhead which might negate any (time) advantage that you would gain from not having to do as many individual switches (especially if you can switch several switches simultaneously). Which is bad if you are pressed for time, as you might be in an escape room. Indeed, the whole premise of this problem is doing brute force instead of doing the mental work and solving the puzzle. – tomasz – 2019-07-22T11:03:18.487
@PedroLobito "Don't waste it in vain" nice pleonasm! :) – Teleporting Goat – 2019-07-22T13:14:36.477
@teleporting-goat, time is always "wasted" because you cannot stop it, but you can choose to spend it wisely, not in vain. It may not make too much sense to french speakers, which is normal, since their preception of reality is normally distorted. – Pedro Lobito – 2019-07-23T02:42:21.767
@PedroLobito I'll pass on the personal attack on French speakers being 'not normal' and won't add anything else after this bc comments aren't for this, but no. To "waste time" is to spend time in vain, especially in your context. If it's spent cleverly and usefully, it's just spent. Time is wasted when something could be done faster or when you wait for nothing. You could literally have said "time is precious, don't waste it" or "don't spend it in vain" and the meaning would be basically the same. Perhaps it would help to look at definitions.
– Teleporting Goat – 2019-07-23T08:03:02.027