Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve this puzzle logically without trial and error

I came across the puzzle slant in Ubuntu. I want to solve the puzzle logically and not by trial and error etc.

The rules are simple:

  1. We have to fill all the boxes with the right or left slant.
  2. The number of slants touching a number must be equal to that number.
  3. No loops are allowed in the board. i.e the slants must not form loops.

Puzzle:

Question

Auto solved answer:

enter image description here

Where do I start?

like image 654
Muthu Ganapathy Nathan Avatar asked Mar 01 '12 17:03

Muthu Ganapathy Nathan


2 Answers

Instead of left and right slant I'll use slash (/) and backslash (\).

Let's take one square with corners (x1)(11), where x is anything but 1. There's one such in top left corner. Assume that slant on that square is slash, which connects two 1's. Those 1's are "used up" and all squares touching them must have lines that do not touch the numbers. But that leads to impossible situation because we would have a slash both left and below our square which means that remaining 1 is touching two slants. The conclusion: if you have a square with three 1's then the line in that square must touch the corner that is not 1. This rule may not apply in edges and corners, but if you have a 1 in the corner you must draw the line touching that corner.

Numbers 1 and 3 are symmetrical and using similar logic we get another rule: if you have a square with three 3's then the line in that square must touch two of those three 3's.

There are more general rules, but they do not apply in corners. There must be squares surrounding the square in question. Let's take a square two opposing 1's (x1)(1y), where x and y are anything, including a no-number. There's one such two squares away from bottom left corner. Assume that slant on that square is slash, which connects two 1's. Those 1's are "used up" and all squares touching them must have lines that do not touch the numbers. But that leads to loop around the 1's. The conclusion: if you have a square with two opposing 1's then the line in that square must not touch those two 1's. This rule may not apply on the board edges.

Numbers 1 and 3 are symmetrical, but previous rule employs "no loops" rule, and there's no symmetrical "no loops of lateral lines" rule, and therefore there is no rule having two opposing 3's.

Now that you know which line touches the 1 you can conclude that no other line can touch it. We can generalize this reasoning to following filling rules: if a number x is touching x lines then all other neighboring squares have lines that do not touch the number. And symmetrically: if a number x is corner of (4-x) squares with lines that do not touch the number then all other neighboring squares must have lines that touch the number.

Googling around for the term "Gokigen Naname" I found more rules. One is about two adjacent 1's (11), but Mweerden already covered it.

These rules are not enough to solve the board. There are other rules probably. But eventually the algorithm may have to make a guess.

like image 171
Dialecticus Avatar answered Nov 15 '22 06:11

Dialecticus


"Logically" is a very broad term. As Orbling mentioned in the comments, backtracking can be considered logical. One can also understand "logically" as meaning how to solve it by translating it to a logical formula. From the comments I gather you are trying to implement a solver, similar to perhaps a common Sudoku solver.

A simple way to implement a solver, similar to one for Sudokus, is to find certain patterns. For the puzzles generated by the program you refer to, I can say with reasonable confidence that this should be sufficient to solve them without guessing and backtracking.

Some examples of obvious patterns are <11> and >33<. Especially 2 has some nice "transitive" properties. For example: <12...23 -> <12...23< (with 2...2 an arbitrary amount of 2s). Try your solver on various examples and when it gets stuck I'm sure you've found an example that can teach you another pattern.

like image 30
mweerden Avatar answered Nov 15 '22 07:11

mweerden