Showing posts with label Mathematica. Show all posts
Showing posts with label Mathematica. Show all posts

Friday, November 25, 2011

The blue and yellow particles problem

Some time ago, I read the following problem:
In a box, there is a certain amount of blue and yellow particles. The particles can react with each other in three different ways:
  • Two yellow particles can fuse into a blue particle
  • Two blue particles can fuse into a blue particle
  • An yellow and a blue particle can fuse into an yellow particle
Given the initial amount of yellow and blue particles, which color will be the last particle?
I was kinda confused with this problem. My first thought was "How the hell am I supposed to know? Can't the three types of reactions happen in different orders which may eventually lead to last particles with different colors?" The problem wording seemed to tell you that the color of the last particle only depended on the number of initial particles of each color. This appeared to be true, because every time I drew a box with a certain amount of blue and yellow particles, I couldn't find two different sequences of reactions which would produce last particles with different colors.
To solve this problem let's start by writing some math. Hopefully it will lead us to the solution.

The first thing I did was to define three functions to represent the possible reactions. Each function receives and returns a pair of values, nBlues and nYellows which represent the number of particles of each color in the box. The functions argument correspond to the state of the box before the reaction and the function return value represent the state after the reaction. I defined these functions in Mathematica (a computer program) like this:
bb[{nBlues_, nYellows_}] := {nBlues, nYellows} + {-1, 0}
yb[{nBlues_, nYellows_}] := {nBlues, nYellows} + {-1, 0}
yy[{nBlues_, nYellows_}] := {nBlues, nYellows} + {1, -2}
The name of the functions correspond to the reaction they represent.
  • The blue-blue function will return a "box" from which one blue particle was taken because two blue particles fuse into one in the corresponding reaction.
  • The yellow-blue function will return a "box" from which one blue particle was taken because one blue particle and one yellow particle fuse into one yellow particle in the corresponding reaction .
  • The yellow-yellow function will return a "box" from which two yellow particles were taken and one blue particle was added because two yellow particles fuse into one blue particle in the corresponding reaction.
Ok, suppose you want to know the resulting state of having a box with 5 blue particles and 6 yellow particles after a serie of reactions, for example (in left to right order): bb,yb,bb,yy. All you have to do is to write:
yy[bb[yb[bb[{5, 6}]]]] 
Notice that the function at the right is calculated before the function at left. Mathematica will output:
{3, 4}
Mathematica is an algebra system software, so it can also do math with symbols which can represent, for example, variables:
yy[bb[yb[bb[{a, b}]]]]
Output:
{-2 + a, -2 + b}
Now, let's define some notation. Let \(f\) be a function, and \(n\) be a number. We will say that: $$\underbrace{f(f(...f(x)...))}_{f \textrm{ composed } n \textrm{ times}} = \underbrace{f \circ f \circ ...\circ f(x)}_{f \textrm{ composed } n \textrm{ times}} = f^{n}(x) $$ Since all the functions we defined are commutative between each other (this is really easy to prove so let's skip this job and give it to Mathematica):
bb[yb[{a, b}]]
{-2 + a, b}
yb[bb[{a, b}]]
{-2 + a, b}
bb[yy[{a, b}]]
{a, -2 + b}
yy[bb[{a, b}]]
{a, -2 + b}
yy[yb[{a, b}]]
{a, -2 + b}
yb[yy[{a, b}]]
{a, -2 + b}
,we can write any sequence of reactions that happens in the box as:
$$bb^a \circ yb^b \circ yy^c$$ Where \(a\),\(b\) and \(c\) correspond to the number of times each kind of reaction happened. So, for example, the sequence of reactions yb,yy,bb,yy,bb,bb,yb could be represented as \(bb^3 \circ yb^2 \circ yy^2\).
Notice we can only say this because all the functions we have are commutative. Suppose there is another reaction \(dd\) where, for example, the number of yellow particles in the box is doubled. The corresponding function would not be commutative with the rest of the functions we already have, i.e \((dd \circ yy(4,5) = (5,6)) \ne (yy \circ dd(4,5) = (5,8)) \) so we couldn't change the order in a composition of functions and expect the same result.

The following equation now emerges:
$$(b_{\textrm{final}},y_{\textrm{final}}) = bb^a \circ yb^b \circ yy^c(b_{\textrm{initial}},y_{\textrm{initial}})$$ It is also not hard to come up with the conclusion that:
\begin{array}{lcl} bb^a(b_{\textrm{initial}},y_{\textrm{initial}}) = (b_{\textrm{initial}}-a, y_{\textrm{initial}})\\ yb^b(b_{\textrm{initial}},y_{\textrm{initial}}) = (b_{\textrm{initial}}-b, y_{\textrm{initial}})\\ yy^c(b_{\textrm{initial}},y_{\textrm{initial}}) = (b_{\textrm{initial}}+c, y_{\textrm{initial}} - 2c)\\ \end{array} I'll prove the third equality. The others are analogous.
\begin{array}{lcl} yy^c(b_{\textrm{initial}},y_{\textrm{initial}})=((...((b_{\textrm{initial}},y_{\textrm{initial}})+\underbrace{(1,-2))+(1,-2))+...)+(1,-2)}_{(1,-2) \textrm{ is summed } c \textrm{ times}})\Leftrightarrow \\ yy^c(b_{\textrm{initial}},y_{\textrm{initial}})= (b_{\textrm{initial}}+c,y_{\textrm{initial}}-2c) \end{array} Now we can have a more workable equation:
$$(b_{\textrm{final}},y_{\textrm{final}}) = bb^a \circ yb^b \circ yy^c(b_{\textrm{initial}},y_{\textrm{initial}}) \Leftrightarrow (b_{\textrm{final}},y_{\textrm{final}}) = (b_{\textrm{initial}}-a-b+c,y_{\textrm{initial}}-2c)$$ In our problem, we want to find out what color the last particle will be. If it is the last one, then we know that $$b_{\textrm{final}} + y_{\textrm{final}} = 1$$ Using all our equations, we can write the following system where all variables are natural numbers:
\begin{cases} b_{\textrm{final}} = b_{\textrm{initial}}-a-b+c \\ y_{\textrm{final}} = y_{\textrm{initial}}-2c \\ b_{\textrm{final}} + y_{\textrm{final}} = 1 \end{cases} We have two possibilities for the \((b_{\textrm{final}},y_{\textrm{final}})\): \((0,1)\) and \((1,0)\).
If \((b_{\textrm{final}},y_{\textrm{final}})=(1,0)\) then:
$$y_{\textrm{initial}} = 2c \wedge b_{\textrm{initial}} = 1 + a + b - c$$ \(2c\) is even, so we can say that
$$(b_{\textrm{final}},y_{\textrm{final}})=(1,0)\Rightarrow y_{\textrm{initial}} \textrm{ is even} $$ If \((b_{\textrm{final}},y_{\textrm{final}})=(0,1)\) then:
$$y_{\textrm{initial}} = 2c + 1 \wedge b_{\textrm{initial}} = a + b - c$$ \(2c + 1\) is odd, so we can say that
$$(b_{\textrm{final}},y_{\textrm{final}})=(0,1)\Rightarrow y_{\textrm{initial}} \textrm{ is odd} $$ The meaning of an implication is that if \(a\) implies \(b\), every time \(a\) is true, \(b\) is also true. And there is only one final result which leads to an even initial number of yellow particles, and one final result which leads to an odd initial number of yellow particles. So
\begin{array}{lcl} y_{\textrm{initial}} \textrm{ is even} \Rightarrow (b_{\textrm{final}},y_{\textrm{final}})=(1,0) \\ y_{\textrm{initial}} \textrm{ is odd} \Rightarrow (b_{\textrm{final}},y_{\textrm{final}})=(0,1) \end{array} are also true statements. Now we can finally give a justified answer to the problem:
Given the initial amount of yellow and blue particles, which color will be the last particle?
If the amount of yellow particles is even, the last particle will be blue. If it is odd, the last particle will be yellow.

In this case, the last particle will be yellow. I guess that's why the yellow particle is smiling :D




Friday, November 11, 2011

Solving minesweeper using algebra

Language: English/Inglês What is in this post: Minesweeper linear systemMathematica solving the system
Have you ever wondered how to solve minesweeper in a mathematical way?
I have. It is pretty easy and straightforward. I'll show you a possible algorithm for this.
Consider the following  minesweeper puzzle:


I suppose you know the rules of minesweeper:  when a cell has a number 'x', it means that in the 8 cells around that cell, there is a total of 'x' mines. For example, around the '3' cell, there are 3 mines. The objective of the game is to pinpoint the location of all the mines. But for this, you need to make sure which squares don't have mines so you can click on them to get a number which will give you a new clue.

Ok, let's give names to the cells: I'll call \(x_1y_1\) to the bottom left cell, \(x_1y_2\) to the one above it, and \(x_2y_1\) to the one at it's right, etc...

We need to express mathematically the information that is given to us. Our variables are gonna be the unknown cells around the number cells that are visible.

Unknown cells around the number cells

The name of the each variable will be the name of the cell it represents. Each of the cell variables will have the value 1, if it has a mine, or 0 if it doesn't. It can't have the value 0.5 because a cell can't have half a mine, neither the value 2, because a cell has, at most, one mine.


Now it is easy to write what we see!

\begin{array}{lcl} \forall i,j : x_iy_j = 0 \lor x_iy_j = 1 \\ x_1y_1 + x_1y_2 = 1\\ x_1y_1 + x_1y_2 + x_1y_3 + x_2y_3 + x_3y_3 = 3\\ x_2y_3 + x_3y_3 + x_4y_3 = 1\\ x_3y_3 + x_4y_3 + x_5y_3 + x_5y_2 + x_5y_1 = 2\\ x_5y_2 + x_5y_1 = 2\\ \end{array}
Now I'll tell Wolfram Mathematica (a computer program) to solve this for me:

Simplify[Reduce[
  {x1y1 + x1y2 == 1 && 
    x1y1 + x1y2 + x1y3 + x2y3 + x3y3 == 3 &&
    x2y3 + x3y3 + x4y3 == 1 &&
    x3y3 + x4y3 + x5y3 + x5y2 + x5y1 == 2 &&
    x5y2 + x5y1 == 2 &&
    (x1y1 == 1 || x1y1 == 0) &&
    (x1y2 == 1 || x1y2 == 0) &&
    (x1y3 == 1 || x1y3 == 0) &&
    (x2y3 == 1 || x2y3 == 0) &&
    (x3y3 == 1 || x3y3 == 0) &&
    (x4y3 == 1 || x4y3 == 0) &&
    (x5y3 == 1 || x5y3 == 0) &&
    (x5y2 == 1 || x5y2 == 0) &&
    (x5y1 == 1 || x5y1 == 0) 
     }
  , {x1y1, x1y2, x1y3, x2y3, x3y3, x4y3, x5y3, x5y2, x5y1}
  ]]
The output is:
x1y3 == 1 && x2y3 == 1 && x3y3 == 0 && 
x4y3 == 0 && x5y1 == 1 && x5y2 == 1 && 
x5y3 == 0 && 
((x1y1 == 0 && x1y2 == 1) || (x1y1 == 1 && x1y2 == 0))
This means that \(x_1y_3, x_2y_3, x_5y_1\) and \(x_5y_2\) cells have a mine while \(x_3y_3, x_4y_3\) and \(x_5y_3\) cells don't.
Whether or not the \(x_1y_1\) and \(x_1y_2\) cells have a mine is undetermined, but we know that if \(x_1y_1\) has a mine, \(x_1y_2\) doesn't and vice-versa.

What we do at this moment is to fill the board with the results we are 100% sure about. If we do so we get the following board:


As you can see, when we looked at the beginning board we couldn't immediately say where were the mines. It was not obvious. But using algebra gave us some help.
Now you just have to repeat this process until the board is solved.
Note that not every minesweeper puzzles have a determinable solution: which means sometimes every variable will be undetermined when you solve the system.
Also, in Microsoft minesweeper, there is a counter for how many mines are left not flagged at each moment. This could be helpful in near end game situations where you can't find out any cell value to be determined. However, i didn't include this data in the resolution I purposed for matters of simplicity. If you are curious about how to do it, post a comment.