Programming

Post navigation

The puzzle is called Skyline and it’s a packing puzzle. The objective is to place the metal rod in one of the holes in the base and place the nine wooden pieces around it. It was designed by Jean Claude Constantin. When solved, the puzzle looks something like this:

Sometimes with these kinds of puzzles it’s quicker to write a program that finds a solution than trying to solve it by hand. Check out this github repository for a Prolog program that finds solutions for a given rod location.

To use this program open the file skyline.pl in your favorite Prolog interpreter (e.g. SWI-Prolog) and execute the following:

1

2

3

4

5

6

7

8

9

10

11

?- print_solution(pos(4,4)).

+-------+

|ggeeeeh|

|gggdehh|

|ggiddhh|

|iii ddh|

|iiicfff|

|bicccff|

|baaaaaf|

+-------+

true

You can press ; to find alternative solutions. The pos(X,Y) part refers to the location of the metal rod.

I mainly wrote this to get some practice with some of the new C++11 features such as variadic templates and lambda functions. It uses template metaprogramming to construct (but not train) the neural network at compile time. You can download the code from its github repository. It’s lacking proper documentation, but I’ve included two examples that should get you started: the xor problem and Fisher’s Iris data set.

A while ago I was looking for a simple lightweight c++ library that could produce some basic charts. I quickly found the PPlot library which is designed to easily integrate with different GUI toolkits and also comes with bindings for ruby and python. To make the library usable within different GUI frameworks the drawing part has been abstracted to a Painter class. For each GUI framework a different Painter class must be implemented. The library comes with ready made implementations for GUI toolkits like wxWidgets and QT. However, I wanted to use the PPlot library in a pure win32 application and had to implement my own Painter class. If you’re in a similar situation check out my source for an example.

I’ve always been interested in emulators and just finished my Chip-8/SCHIP emulator. Chip-8 is an interpreted programming language that was first used on some early homecomputers and later on the HP48 calculator. The emulator allows you to play some retro games like pong and invaders.

The emulator is written in C++ and is available for download here. Besides the program and source code I’ve also included some games that seem to work ok. The original machines that ran a chip-8 interpreter had a 16 key hex-keypad that looked like this:

Four people need to cross a bridge at night which only supports two people at the same time. Person A needs 1 minute to cross the bridge, B needs 2 minutes, C needs 5 minutes and D needs 10 minutes. When two people cross the bridge they move at the slowest person’s pace. They have a torch which has battery left for only 17 minutes. They can’t cross the bridge without light. How can they manage to cross the bridge?

One might guess that an obvious solution would be to let the fastest person (A) shuttle each other person over the bridge and return alone with the torch. This would give the following schedule:

A, B

->

2

A

<-

1

A,C

->

5

A

<-

1

A,D

->

10

The total duration of this schedule would be 19 minutes, so the torch would run out of battery while person A and D are still on the bridge.

The optimal solution consists of letting the two slowest people (C and D) cross the bridge together, giving the following schedule:

Long, long time ago, when I was still a full time student, I once became first in a checkers cup with a program I wrote. Alas, those days of glory are over. Looking back, the program was pretty simple. Choosing an efficient board representation made generating the moves a piece of cake and my program didn’t make use of any opening libraries or endgame strategies. It just implemented minimax search with alpha-beta pruning. Although it wasn’t really sophisticated, I still think the program was pretty sweet and I felt disappointed when I realized I had lost the source code. That’s why I decided to implement this game again only this time in Java (the original was written in C++). I fired up NetBeans and this is the result.

Today the father of fractal geometry turns 83. Benoît Mandelbrot is known for the Mandelbrot set, a set of points in the complex plane that forms a fractal. To see if a point c belongs to the Mandelbrot set start with z0 = 0 and generate the sequence z1, z2, z3,.. by iterating the function zn+1 = zn2 + c. If the value z remains close to the origin, the value c belongs to the Mandelbrot set. If it runs away to infinity, it doesn’t. Plotting the set of points in the complex plane gives you this picture.

The picture is the result of running a small program I wrote on the TI-83+ calculator. You can download it from here. The program runs for several hours. The zip-file also includes two of my other TI-Basic fractal programs. A Julia fractal and the Sierpinski triangle.

The following puzzle is said to be invented by Einstein. Supposedly, he also claimed that only 2% of the world’s population would be smart enough to solve it.

There are 5 houses in 5 different colors in a row. In each house lives a person with a different nationality. These 5 owners drink a certain drink, smoke a certain brand of cigar, and keep a certain pet. No owners have the same pet, smoke the same brand of cigar or drink the same drink.

The question is: WHO OWNS THE FISH?

HINTS:

the Brit lives in the red house

the Swede keeps dogs as pets

the Dane drinks tea

the green house is on the immediate left of the white house

the green house owner drinks coffee

the person who smokes Pall Mall rears birds

the owner of the yellow house smokes Dunhill

the man living in the house right in the center drinks milk

the Norwegian lives in the first house

the man who smokes blends lives next to the one who keeps cats

the man who keeps horses lives next to the one who smokes Dunhill

the owner who smokes Bluemaster drinks beer

the German smokes prince

the Norwegian lives next to the blue house

the man who smokes blends has a neighbor who drinks water

Working out the solution with nothing more that a pen and some paper is certainly doable by, I suspect hope, a larger percentage of people than the 2 % mentioned above. But as an example of how to solve these kinds of logic puzzles using Prolog, I wrote this code.

It’s pretty easy to define an interpreter in Prolog. How this can be done is shown in this paper.

The language brainfuck is extremely minimalistic, consists of only eight commands and is Turing complete. Writing an interpreter for it in Prolog is a breeze.

You can download my interpreter here. It takes a slightly different approach than described in the paper to allow for tail recursion. Without this optimization, running any brainfuck program more complex than ‘hello world’, would result in a Prolog ‘Out of Stack’ error. For some tips on writing efficient Prolog programs check out this document. The Prolog environment I used is SWI-Prolog.