# Blog

## Archive

Show me a random blog post**2019**

**2018**

**2017**

**2016**

**2015**

**2014**

**2013**

**2012**

## Tags

folding paper folding tube maps london underground platonic solids london rhombicuboctahedron raspberry pi weather station programming python php inline code news royal baby probability game show probability christmas flexagons frobel coins reuleaux polygons countdown football world cup sport stickers tennis braiding craft wool electromagnetic field people maths trigonometry logic propositional calculus twitter mathslogicbot oeis matt parker pac-man graph theory video games games chalkdust magazine menace machine learning javascript martin gardner noughts and crosses reddit national lottery rugby puzzles game of life dragon curves fractals pythagoras geometry triangles european cup dates palindromes christmas card ternary bubble bobble asteroids final fantasy curvature binary arithmetic bodmas statistics error bars estimation accuracy misleading statistics pizza cutting captain scarlet gerry anderson light sound speed manchester science festival manchester dataset a gamut of games hexapawn nine men's morris draughts chess go radio 4 data map projections aperiodical big internet math-off sorting polynomials approximation interpolation chebyshev books cross stitch**2015-03-25**

## Optimal Pac-Man

This is an article which I wrote for the
first issue of

*Chalkdust*. I highly recommend reading the rest of the magazine (and trying to solve the crossnumber I wrote for the issue).In the classic arcade game Pac-Man, the player moves the title character through a maze. The aim of
the game is to eat all of the pac-dots that are spread throughout the maze while avoiding the ghosts
that prowl it.

While playing Pac-Man recently, my concentration drifted from the pac-dots and I began to think
about the best route I could take to complete the level.

### Seven bridges of Königsberg

In the 1700s, Swiss mathematician Leonhard Euler studied a related problem. The city of
Königsberg had seven bridges, which the residents would try to cross while walking around the
town. However, they were unable to find a route crossing every bridge without repeating one of them.

In fact, the city dwellers could not find such a route because it is impossible to do so, as
Euler proved in 1735. He first simplified the map of the city, by making the islands into vertices (or nodes)
and the bridges into edges.

This type of diagram has (slightly confusingly) become known as a graph, the study of which is
called graph theory. Euler represented Königsberg in this way as he realised that the shape of the
islands is irrelevant to the problem: representing the problem as a graph gets rid of this useless information
while keeping the important details of how the islands are connected.

Euler next noticed that if a route crossing all the bridges exactly once was possible then whenever
the walker took a bridge onto an island, they must take another bridge off the island. In this way,
the ends of the bridges at each island can be paired off. The only bridge ends that do not need a
pair are those at the start and end of the circuit.

This means that all of the vertices of the graph except two (the first and last in the route) must have an even number of edges connected to them; otherwise there is no route around the graph
travelling along each edge exactly once. In Königsberg, each island is connected to an odd number of bridges. Therefore the route that
the residents were looking for did not exist (a route now exists due to two of the bridges being destroyed during World War II).

This same idea can be applied to Pac-Man. By ignoring the parts of the maze without pac-dots the pac-graph
can be created, with the paths and the junctions forming the edges and vertices respectively. Once this
is done there will be twenty-four vertices, twenty of which will be connected to an odd number of edges, and so it is impossible to eat
all of the pac-dots without repeating some edges or travelling along parts of the maze with no pac-dots.

This is a start, but it does not give us the shortest route we can take to eat all of the pac-dots: in
order to do this, we are going to have to look at the odd vertices in more detail.

### The Chinese postman problem

The task of finding the shortest route covering all the edges of a graph has become known as the
Chinese postman problem as it is faced by postmen—they need to walk along each street to post
letters and want to minimise the time spent walking along roads twice—and it was first studied by
Chinese mathematician Kwan Mei-Ko.

As the seven bridges of Königsberg problem demonstrated, when trying to find a route, Pac-Man
will get stuck at the odd vertices. To prevent this from happening, all the vertices can be made into even
vertices by adding edges to the graph. Adding an edge to the graph corresponds to choosing an edge, or sequence
of edges, for Pac-Man to repeat or including a part of the maze without pac-dots. In order to complete the level
with the shortest distance travelled, Pac-Man wants to add the shortest total length of edges to the graph.
Therefore, in order to find the best route, Pac-Man must look at different ways to pair off the odd vertices and choose
the pairing which will add the least total distance to the graph.

The Chinese postman problem and the Pac-Man problem are slightly different: it is usually assumed
that the postman wants to finish where he started so he can return home. Pac-Man however can finish
the level wherever he likes but his starting point is fixed. Pac-Man may therefore leave one odd
node unpaired but must add an edge to make the starting node odd.

One way to find the required route is to look at all possible ways to pair up the odd vertices. With a low
number of odd vertices this method works fine, but as the number of odd vertices increases, the
method quickly becomes slower.

With four odd vertices, there are three possible pairings. For the Pac-Man problem there will be
over 13 billion (\(1.37\times 10^{10}\)) pairings to check. These pairings can be checked by a
laptop running overnight, but for not too many more vertices this method quickly becomes unfeasible.

With 46 odd nodes there will be more than one pairing per atom in the human body (\(2.53\times 10^{28}\)).
By 110 odd vertices there will be more pairings (\(3.47\times 10^{88}\)) than there are estimated to be atoms in the
universe. Even the greatest supercomputer will be unable to work its way through
all these combinations.

Better algorithms are known for this problem that reduce the amount of work on larger graphs. The number of
pairings to check in the method above increases like the factorial of the number of vertices. Algorithms are
known for which the amount of work to be done increases like a polynomial in the number of vertices. These algorithms
will become unfeasible at a much slower rate but will still be unable to deal with very large graphs.

### Solution of the Pac-Man problem

For the Pac-Man problem, the shortest pairing of the odd vertices requires the edges marked in red to be
repeated. Any route which repeats these edges will be optimal. For example, the route in green will be
optimal.

One important element of the Pac-Man gameplay that I have neglected are the ghosts (Blinky, Pinky,
Inky and Clyde), which Pac-Man must avoid. There is a high chance that the ghosts will at some point
block the route shown above and ruin Pac-Man's optimality. However, any route repeating the red edges
will be optimal: at many junctions Pac-Man will have a choice of edges he could continue along. It may
be possible for a quick thinking player to utilise this freedom to avoid the ghosts and complete an optimal
game.

Additionally, the skilled player may choose when to take the edges that include the power pellets,
which allow Pac-Man to reverse the roles and eat the ghosts. Again cleverly timing these may allow
the player to complete an optimal route.

Unfortunately, as soon as the optimal route is completed, Pac-Man moves to the next level and the
player has to do it all over again ad infinitum.

### A video

Since writing this piece, I have been playing Pac-Man using
MAME (Multiple Arcade Machine Emulator). Here is one game I played along
with the optimal edges to repeat for reference:

### Similar posts

Video game surfaces | Mathsteroids | MENACE at Manchester Science Festival | The Mathematical Games of Martin Gardner |

### Comments

Comments in green were written by me. Comments in blue were not written by me.

**2016-09-30**

William

**Add a Comment**

**2015-03-24**

## Tube map stellated rhombicuboctahedron

A while ago, I made this (a stellated rhombicuboctahedron):

Here are some hastily typed instructions for
Matt Parker, who is making one
at this month's Maths Jam. Other people are
welcome to follow these instructions too.

### You will need

- 48 tube maps
- glue

### Making a module

First, take a tube map and fold the cover over. This will ensure that your
shape will have tube (map and not index) on the outside and you will have
pages to tuck your tabs between later.

Now fold one corner diagonally across to another corner. It does not matter
which diagonal you chose for the first piece but after this all following pieces
must be the same as the first.

Now fold the overlapping bit back over the top.

Turn it over and fold this overlap over too.

You have made one module.

You will need 48 of these and some glue.

### Putting it together

By slotting three or four of these modules together, you can make a
pyramid with a triangle or square as its base.

A stellated rhombicuboctahedron is a rhombicuboctahedron with a pyramid, or
stellation on each face. In other words, you now need to build a
rhombicuboctahedron with the bases of pyramids like these. A rhombicuboctahedron
looks like this:

en.wiki User Cyp, CC BY-SA 3.0

More usefully, its net looks like this:

To build a stellated rhombicuboctahedron, make this net, but with each shape
as the base of a pyramid. This is what it will look like 6/48 tube maps in:

If you make on of these, please tweet me a photo so I can see it!

### Similar posts

Tube map Platonic solids, pt. 2 | Tube map kaleidocycles | Tube map Platonic solids, pt. 3 | Electromagnetic Field talk |

### Comments

Comments in green were written by me. Comments in blue were not written by me.

**2017-10-14**

Roberts, David

**Add a Comment**

**2015-03-15**

## Logic bot, pt. 2

A few months ago, I set
@mathslogicbot going on the
long task of tweeting all the tautologies (containing 140 characters or less)
in propositional calculus with the symbols \(\neg\) (not), \(\rightarrow\)
(implies), \(\leftrightarrow\) (if and only if), \(\wedge\) (and) and \(\vee\)
(or). My first post on logic bot contains a full
explanation of propositional calculus, formulae and tautologies.

### An alternative method

Since writing the original post, I have written an alternative script to
generate all the tautologies.
In this new method, I run through all possible strings of length 1 made
with character in the logical language, then strings of length 2, 3 and so on.
The script then checks if they are valid formulae and, if so, if they are
tautologies.

In the new script, only formulae where the first appearances of variables
are in alphabetical order are considered. This means that duplicate tautologies
are removed. For example, \((b\rightarrow(b\wedge a))\) will not be counted as
it is the same as \((a\rightarrow(a\wedge b))\).

You can view or download this alternative code on
github.
All the terms of the sequence that I have calculated so far can be viewed
here and the tautologies for these terms are
here.

### Sequence

One advantage of this method is that it generates the tautologies sorted by
the number of symbols they contain, meaning we can generate the sequence whose
\(n\)

^{th}term is the number of tautologies of length \(n\).The first ten terms of this sequence are

$$0, 0, 0, 0, 2, 2, 12, 6, 57, 88$$
as there are no tautologies of length less than 5; and, for example two
tautologies of length 6 (\((\neg a\vee a)\) and \((a\vee \neg a)\)).

This sequence is listed as
A256120 on OEIS.

#### Properties

There are a few properties of this sequence that can easily be shown.
Throughout this section I will use \(a_n\) to represent the \(n\)

^{th}term of the sequence.Firstly, \(a_{n+2}\geq a_n\). This can be explained as follows: let \(A\)
be a tautology of length \(n\). \(\neg\neg A\) will be of length \(n+2\) and
is logically equivalent to \(A\).

Another property is \(a_{n+4}\geq 2a_n\): given a tautology \(A\) of length
\(n\), both \((a\vee A)\) and \((A\vee a)\) will be tautologies of length
\(n+4\). Similar properties could be shown for \(\rightarrow\),
\(\leftrightarrow\) and \(\wedge\).

Given properties like this, one might predict that the sequence will be
increasing (\(a_{n+1}\geq a_n\)). However this is not true as \(a_7\) is 12
and \(a_8\) is only 6. It would be interesting to know at how many points in
the sequence there is a term that is less than the previous one. Given the
properties above it is reasonable to conjecture that this is the only one.

Edit: The sequence has been published on OEIS!

### Similar posts

Logical contradictions | Logic bot | How OEISbot works | Raspberry Pi weather station |

### Comments

Comments in green were written by me. Comments in blue were not written by me.

**Add a Comment**

**2015-03-03**

## Design your own flexagon

This post explains how to make a trihexaflexagon with and images you like on the three
faces.

### Making the template

To make a flexagon template with your images on, visit mscroggs.co.uk/flexagons. On this page, you will be
able to choose three images (png, jp(e)g or gif) which will appear on the faces of your flexagon.
Once you have created the template, save and print the image it gives you.

The template may fail to load if your images are too large; so if your template doesn't
appear, resize your images and try again.

### Making the flexagon

First, cut out your printed tempate. For this example, I used plain blue, green and purple
images.

Then fold and glue your template in half lengthways.

Next, fold diagonally across the blue diamond, being careful to line the fold up with the purple
diamond. This will bring two parts of the purple picture together.

Do the same again with the blue diamond which has just been folded into view.

Fold the green triangle under the purple.

And finally tuck the white triangle under the purple triangle it is covering. This will bring the
two white triangles into contact. Glue these white triangles together and you have made a
flexagon.

Before flexing the flexagon, fold it in half through each pair of corners. This will get it
ready to flex in the right places.

Now fold your flexagon into the following position.

Then open it out from the centre to reveal a different face.

This video shows how to flex a
flexagon in more detail.

### Similar posts

Electromagnetic Field talk | Assorted christmaths | Dragon curves II | Tube map kaleidocycles |

### Comments

Comments in green were written by me. Comments in blue were not written by me.

**Add a Comment**

**2015-01-31**

## Tube map Platonic solids, pt. 3

In 2012, I folded all the Platonic solids from tube maps. The dodecahedron I made was a little dissapointing:

After my talk at Electromegnetic Field 2014, I was shown the following better method to fold a dodecahedron.

### Making the modules

First, take a tube map, cut apart all the pages and cut each page in half.

Next, take one of the parts and fold it into four

then lay it flat.

Next, fold the bottom left corner upwards

and the top right corner downwards.

Finally, fold along the line shown below.

You have now made a module which will make up one edge of the dodecahedron. You will need 30 of these to make the full solid.

Once many modules have been made, then can be put together. To do this, tuck one of the corners you folded over into the final fold of another module.

Three of the modules attached like this will make a vertex of the dodecahedron.

By continuing to attach modules, you will get the shell of a dodecahedron.

To make the dodecahedron look more complete, fold some more almost-squares of tube map to be just larger than the holes and tuck them into the modules.

### Similar posts

Tube map kaleidocycles | Electromagnetic Field talk | Tube map Platonic solids, pt. 2 | Tube map Platonic solids |

### Comments

Comments in green were written by me. Comments in blue were not written by me.

**Add a Comment**

2016-09-30There seems to be no world record for just one Pac-Man level (and I don't have time to get good enough to speed run all 255 levels before it crashes!)