Showing posts with label Puzzles. Show all posts
Showing posts with label Puzzles. Show all posts

Tuesday, August 13, 2024

The amazing race

One of the Israeli TV channels broadcasts a reality show called 'the race to the million', a licensed version of the American TV show, 'The amazing race'. The prize from the American show is $1 million dollars, whereas the winners of the Israeli show win only NIS 1 million (about four times less) and in reality, only NIS 750,000 after tax is paid. Such is life. This is the only reality show that I can watch parts of without breaking out with a rash. This is not the sort of TV programme that I would normally write about, but the current episodes display a puzzle that I thought I would share here.

There are three jerrycans: one holding 10 litres, one holding 7 litres and one holding 3 litres. The 10 litre jerrycan is full of petrol. The puzzle is to obtain one jerrycan holding 5 litres petrol (without spilling any, of course).


As can be seen, this blog uses a 'spoiler' button. I had great difficulty with getting this to work; as far as I can understand, the button works when viewed after 'publication', which is what all visitors to the blog can see. But the button doesn't work when I am writing the blog nor when I am previewing it. I'm leaving this note to myself for the next time that I use a 'spoiler' button.


This day in history:

Blog #
Date
TitleTags
4713/08/2006
Graduation showFairport Convention, Canterbury sound, Bar mitzva, Music festivals
106513/08/2017
Road 38 is brought into the 21th century!Personal
125113/08/2019
Night walking (2)Walking
141413/08/2021
Back to the graveyardKibbutz, Father

Saturday, October 29, 2022

The 8-puzzle

I have just received a communication from someone (unknown to me) via LinkedIn, asking about a computer program that I wrote (and continually revised) throughout the 1980s that solved the '8-puzzle'. I replied that I would have to get into my time machine and set it to return 34 years, but first I'll retrieve whatever shreds of memory I can find for the blog.

I came across this puzzle in a book written by David Levy; looking at the list of publications on his wiki page, this was probably 'Computer Gamesmanship: Elements of Intelligent Game Design' , published in the early 80s. One has to remember that I first 'met' a computer at the end of 1982: this was a teletype connected to a phone line that was connected to a PDP-11 computer about 20 miles away. I also had absolutely no background in programming, so this book talked about topics that were far above my ability. The 8-puzzle problem served as an introduction to all kinds of topics in computer science: how to draw a grid on a VT-100 screen, how to get direct input, how to check for legal moves, linked lists, hash tables ... there's a lot in there.

Looking for the 8-puzzle on the Internet now, I see that it's a favourite topic of computer science degree courses, which makes sense. I think that this site gives a good introduction to what the 8-puzzle is, who developed it (I remember it as a plastic toy from my childhood) and how to solve it with the computer. Note that this assignment is numbered COS 226 that probably means that this is an intermediate course in computer science. This site gives another good explanation of the puzzle and how to solve it.

I wish I had access to these web pages back in the day instead of having to stumble in the dark, teaching myself all kinds of techniques. All I had were a few pages in Levy's book and another book about data structures in Pascal, instead of detailed guides.

My program had two modes: user and computer. First the computer would create a random initial configuration, then the user would be given the opportunity to solve the problem, whilst the computer counted how many moves the user required. For my program, this mode taught me about user interface: how to react to user input and how to change the display depending on the input. 

But it was the second mode that was the more interesting: 'teaching' the computer how to solve the puzzle. Levy wrote about the 'minimax' algorithm, but it wasn't until one day I was in the computer science library of Bar Ilan university that I found out what this was. I imagine that I was doing some reserve army service in the lab at Tel Hashomer hospital, as the university was within walking distance (don't forget that I was/am a good walker; this was probably 2 km away). I acted like I had the right to access books and journals, and no one asked me who I was.

Every few months I would have a new idea as to how I could improve the evaluation function of the current configuration (basically, how far the current configuration is from the goal) in order to figure out what the next move should be. I also stored the various configurations for two reasons: I wanted to prevent the computer getting into a loop, and I wanted to look at the configurations myself to see how I would have acted. I remember now that I had a standard configuration that took me something like 37 moves to solve; any program version that took more moves needed to be improved. I also wanted to surprise myself and see whether the computer could solve this test configuration in fewer moves; after all, I'm no expert in this puzzle, and so it would be quite possible that the computer could beat me.

On the left is a picture of the solved 8-puzzle, or as we would call it in computing terms, the goal configuration. I note one major difference from this configuration and my goal configuration (that presumably came from the Levy book): mine had the empty space in the middle, so that the second row would be '4/ /5' and the third row would be '6/7/8'. This may seem like a minor point but it's very important.

Looking at the program code that has been stored here (I wonder how; did I give permission for this?), I am sad to say that although there are comments, they rarely state anything that is not obvious. There is certainly no explanation about the algorithm or how the evaluation function works. 

What Levy called IIRC the move generator (i.e. which tile should be moved next) appears to be implemented in the function makenode. Going back to the first picture of a real 8-puzzle, there are only two moves possible in this configuration: either the 1-tile would be moved down or the 5-tile be moved left. I would think that this latter move would be better then the 5-tile would be in the correct column but in the wrong row. Moving the 1-tile wouldn't appear to do much. But that's why the program has its evaluation function in order to calculate which move is better.
function evaluate (var p:square): integer; var i, tmp: integer; ch: char; blank: boolean; begin tmp:= 0; i:= 0; while i < 9 do begin i:= i + 1; ch:= p[i]; blank:= ch = ' '; if not blank then tmp:= tmp + table[(i-1)*9 + ord(ch) - zero] else tmp:= tmp + table[i*9]; if blank then if i <> 5 then tmp:= tmp + 2 else else case i of 5:; 2,4,6,8: if p[5] <> ' ' then if (p[preint[i]] <> prech[ch]) and (ch <> prech[p[5]]) then tmp:= tmp + 5 else else if p[preint[i]] <> prech[ch] then tmp:= tmp + 5; 1,3,7,9: if p[preint[i]] <> prech[ch] then tmp:= tmp + 3 end end; evaluate:= tmp end { evaluate };
The above is not spaghetti code: it's actually presented in quite a logical format, but there's no explanation. Earlier in the table there are definitions of tables used in this function, but these definitions are opaque: I don't remember why certain values were chosen or what they do. The name 'prech' implies to me that there function checked which tile preceeded which; apparently this was an effective approach. 

Monday, May 22, 2017

Puzzle from the Guardian

This puzzle comes from here:

In each of the four sectors of the outer circle, there is a two-digit number which is equal to the sum of the three numbers at the corners of its sector. The numbers in the individual circles can only be 1 to 9 and each number can be used only once. One number has been provided to get you started. Find the remaining four numbers


Tuesday, December 27, 2016

Puzzle

My daughter sent me this puzzle; can you solve it?

What's intriguing to me is not the solution but how I arrived at the solution. I'll try and reconstruct my thought processes, row by row.

Monday, August 12, 2013

Puzzle

Once upon a time, Jews in Israel (and everywhere else) had strong, Biblical names. Boys were named Abraham, Isaac, Jacob, Joseph, Adam, David or Jonathan, whereas girls where named Sarah, Leah, Rachel, Miriam, Rebecca or Hannah. Why, even my grandparents were called David, Rebecca, Joseph and Sarah; my mother was called Hannah. But such days are long past, and nowadays children get all kinds of names; the best names are qualities (No'am = pleasantness) or abstract nouns (Shai = gift, Raz = secret, Vered = rose), whereas some are compound words (Or-Li = light to me) and others are just sounds (Kai).

Whereas at one time a name indicated its owner's gender, these days names tend to be unisex. I know both males and females called No'am, males and females called Shai, and males and females called Raz. In this spirit of unisex names, I present the following puzzle.

Gal, Yuval and Amit* are all in the same family (no incestual relationships). One of the three is the father of Gal; one is the only daughter of Yuval, and one is the sibling of Amit. Amit's sibling is neither the father of Gal nor the daughter of Yuval.

Who is the odd one out in terms of gender?

*These three names are indeed unisex and unfortunately are used by both genders.