r/mylittleprogramming • u/phlogistic • Apr 05 '15
Particularly Perplexing Programming Puzzle #1 : Heads Up [solution and request for feedback]
Here's a link to the puzzle. This thread has the solution, so don't read it if you still want to try solving it yourself.
To start off, I'd like some feedback.
Although people initially seemed interested in the thread where I posted the puzzle, nobody has actually solved it. That fine, but it makes me think maybe It's the wrong sort of puzzle for this place. Perhaps it was too hard? Or too much coding? Or not interesting enough? My intent with these puzzles was to give problems which were tricky, but which also have a range of reasonable answers with varying difficulties. I figure if you want pure right/wrong programming puzzles there are already plenty of sites to go to for them.
I was hoping to do something different with these puzzles, but now I'm less sure, particularly since this one was probably on the easier side of the next few I had planned out. Anyway, I'd like to hear any suggestions you have for how these could be made better, and if you think it's worth posting more of these at all.
Ok, on to the solution!
I had planned on going over the different ways the people solved this problem, and point out any solutions I thought were particularly cool. Unfortunately nobody solved it, so I've had to come up with a bunch of different approaches myself. Probably other people's ideas would have been more interesting, but hopefully my attempts will still give you a bit of an idea.
I came up with four ways of solving the problem, and added on a fifth method based on people comments in the previous post. These appear below, sorted by by how efficient they are at solving the puzzle on large grids, and incidentally also by mostly increasing complexity.
If case you're curious about the runtimes of these methods, I made this nice graph of how long it takes to solve puzzles of different sizes for methods 2, 3, 4, and 5. Also, props to /u/mronosa for recognizing this as a version of a game made by Tiger Electronics.
In some of the following solutions I've provided Python code implementing them. These solutions make use of a class which can be used to create puzzles of different sizes and try solving them. For reference here's the code of that class:
import random
class CoinGrid(object):
def __init__(self, nrows, ncols):
self.nrows, self.ncols = nrows, ncols
self.__grid = [[bool(random.getrandbits(1)) for j in xrange(ncols)] for i in xrange(nrows)]
def copy(self):
result = CoinGrid(0,0)
result.nrows, result.ncols = self.nrows, self.ncols
result.__grid = [list(row) for row in self.__grid]
return result
def __getitem__(self, rc):
r, c = rc
return self.__grid[r][c]
def flip(self, r, c):
self.__assertInBounds(r,c)
for rAdj, cAdj in self.iterAdjacent(r,c):
self.__grid[rAdj][cAdj] = not self.__grid[rAdj][cAdj]
return self
def flipAll(self, coinFlips):
for r, c in coinFlips:
self.flip(r, c)
return self
def iterAdjacent(self, r, c):
self.__assertInBounds(r,c)
yield r, c
if r > 0: yield r-1, c
if c > 0: yield r, c-1
if r+1 < self.nrows: yield r+1, c
if c+1 < self.ncols: yield r, c+1
def isSolved(self):
return all(all(row) for row in self.__grid)
def __str__(self):
return "\n".join("".join("H" if elem else "T" for elem in row) for row in self.__grid)
def __assertInBounds(self, r, c):
if r < 0 or c < 0 or r >= self.nrows or c >= self.ncols:
raise IndexError, "there is no coin there"I can't include all of the solutions here due to reddit length restrictions, so I'll post them in the comments section:
View all comments
Show parent comment
2
u/phlogistic Apr 06 '15
Ok, gotcha. It was your "using some generalization of the Chinese remainder theorem" which made me think maybe you weren't totally sure how it would flesh out, but apparently I misinterpreted that. Good job on figuring out how to solve it in polynomial time!
It's actually really simple to implement a proof-of-concept of this idea. My proof of concept version is about 20 lines of python (and they're short lines).
Yeah, I know, right? That's part of the reason that I like actually implementing things like this. If I were just doing the math, I would have come up with method #4 and called it a day. If was only after I implemented and ran a few things I realized that I'd need to come up with something like method #5.
Yeah, that's sort of inevitable since I want the puzzle to be language-agnostic. That said, if you want to solve this one for really large grids you do have to worry about the nuts and bolts. My C++ implementation isn't very sophisticated, but still uses a few tricks which I suspect give a substantial performance boost. I was hoping to get to talk about this sort of thing with people in relation to their own solutions, but the lack of any actual code for other people's solutions nixed that idea.
Cool! I've occasionally been tempted to write a roguelike, but I know I'd get too carried away in adding sophisticated features to ever finish it. What made you decide to write one?