Wednesday, March 25, 2009

12 coins

This is a real tricky logic puzzle that I found in the 'bonus' section of this site. I was quite amazed that the following is possible, even after deducing the solution with some pencil and paper. This is the gist of the puzzle.
You have 12 coins, each identical in size and shape. One of these coins differs in weight from the rest. You have a scale, and must determine which coin does not the weigh the same and whether that coin is heavier or lighter than the rest. The challenge is to reach this conclusion by making at most three weighings.

To elaborate on the puzzle requirements I will formalize the idea of weighings. The scale is a machine to compare the summed weight of two disjoint subsets of the 12 coins; a single comparison of subsets classifies a weighing. The exact output of the scale is not specified in the problem, so you can probably use any kind of (reasonable) scale output you need.

I will work on turning my solution into a post and getting that out in a few days.

There is a similar and much simpler problem on the same website here.

Hope this one catches your interest as it did mine. Enjoy ^.^

Labels:

Thursday, March 19, 2009

iPhone 3.0

So I just watched the entirety of the iPhone 3.0 sneak peak keynote presentation a few days ago and decided to get this geek's opinion out there with the rest.

I will get the biggies out of the way first. Push notification (finally), MMS(needed), voice memos(ok...), Cut/Copy/Paste AND Landscape keyboard in all the iPhones default apps(glorious!). Fantastic to see that Apple decided to give users these key features. Still waiting for video capture... I'll let it slide on this one Apple... A somewhat unfortunate sidestory is that those apps which simply give you landscape emails/txts and download MMS msgs for you will be worthless. Apple had to make this update though in order to make landscape email/txt work well(be truly integrated in the phone). This was the finale of the keynote, and for good reason. These features sell themselves, and people are cheering for them right when they hear they are coming.

It seems like most of the newest APIs are there to get big corporations who have resisted creating iPhone apps into the game. Most of the keynote felt like a pitch to these companies, trying to finally sway them to jump on the bandwagon.

Things like in-app purchase do not excite me at all. I am, in fact, very worried that this new feature will turn some would-be useful app into something that is not worth the money to use. I could not believe the first-person-shooter they demoed in which the player needed to pay a dollar to be able to use the rocket launcher. The thing that really urks me here for some reason is that, it seemed that the rocket launcher is not downloaded and installed but merely unlocked for use in the game. I am not a big fan of this. The idea that when I buy any app I will, possibly without any prior knowledge, be prompted that 'this app is not complete, would you like to give us multiple $0.99 payments to make it so?' just makes me so mad. One of the things I enjoy about paid apps on my iPhone is that there has NEVER been an update for these apps that requires me to pay again(I have never run into one, don't think they exist). But I guess that time is nearly over.

This takes the idea of micro-payments too far, imho. new level packs for games, magazine subscriptions, and ebooks all seem to be perfectly valid reasons to add in-app purchase. But the line between things which are worth another micro-payment and those which are not is fuzzy indeed, even in my own head. This is why I think that apple should have left this one out and continue to give huge corporations that demand it before making an app the middle finger. Perhaps the app market will balance things out in the long term as time goes on, as I'm sure apple has many well paid people which are foreseeing that as the outcome. There are many apps that can be made now because of this feature, but I think just about every currently existing app is better without it. I just hope that improper use of this feature by overly greedy people will hurt the app store's street cred overall.

The Apple evangelists also mentioned that iPhone is now capable of turn by turn directions via gps. They do not exactly tell how though. and they mention the further caveat that while the standard Google Maps' tiles and interface are now available through APIs, the map tiles for turn by turn are not included in the new version of the OS. I am interested to see exactly what Apple give developers in the end as far as a 'turn-by-turn SDK' goes. But overall I don't forsee myself scrambling to license my own map tiles any time soon, so for this I'm just gonna play consumer and wait for the app ^.^

Apple is also giving apps lower-level access to peripheral control and input. This is a nice feature that could generate a bunch of really cool devices that I am excited to see start rolling out. Being the independent developer that I am however, this doesn't start up any fantastical contemplation because it requires a company to produce a quality pair of device and app. All I can hope is that some iPhone peripherals begin to come out which provide their own APIs for homebrew app interaction. I'll cross my fingers on that because it is sure not be available much earlier than fall or winter.

Finally, I am psyched about bluetooth p2p connectivity using Bonjour. I don't know how to use Bonjour now but I will start to teach myself soon for this feature. This seems like a really easy, hella fast way to connect 2(or more...please apple) iPhones for some sick collaboration and multiplayer gaming.

Labels: ,

Friday, March 13, 2009

Prolog on OSX

In my last post I solved a logical problem with prolog. If you want to get gprolog on your mac to play around with yourself, the easiest way to get it by far is to use macports. There are 4 very simple steps (one of them is optional even!).

Step 0:

open up the Terminal application.

Step 1 (Optional):
update your macports installation with the command:
sudo port -d selfupdate


Step 2:
upgrade your installed programs with the command
sudo port -d upgrade installed


Step 3:
install gprolog with the command
sudo port install gprolog


That's it. Now you can just type 'gprolog' to enter the prolog interactive interpreter. To load a file 'prog.pl', simply type
[prog].
into the interpreter and then you can play around with any functions/axioms defined in that file.

See my last post to look at an example prolog file, and see how to load files and use there functions.

See this post to learn how to install macports on your mac.

enjoy, everyone ^.^

Labels: , ,

Logic Problems in Prolog

Lately, I've been very into the idea of using prolog to solve logical problems/puzzles. In my opinion, prolog is a fantastic tool to simply brute force a solution with very elegant and short code. This is largely because of prolog's automatic backtracking/retrying features. Running time can be fairly bad, but the efficiency with which you can manufacture solutions is a big plus. Here is a very nice explanation of the awesomeness of prolog ^.^

Googling 'classic logic puzzles' lead me here. There were a lot of good puzzles of varying difficulty on this site. Some that I couldn't program I just sat and thought out. It was very enjoyable ^.^

Here is a problem that I was able to write a program for. The problem is simple, but the prolog code is also quite nice and easy to read.

Problem
What is the four-digit number in which the first digit is one-third the second, the third is the sum of the first and second, and the last is three times the second?


Code

digit(1). digit(2). digit(3). digit(4). digit(5).
digit(6). digit(7). digit(8). digit(9). digit(0).

not(X) :- X, !, fail.
not(_) .

fourdigitnum :-
digit(A), digit(B), digit(C), digit(D), not(A = 0),
First is B // 3, Third is A + B, Last is 3 * B,
A = First, C = Third, D = Last,
write(A), write(B), write(C), write(D).


Explanation

The program just grabs four digits(number ABCD), and ensures that A isn't zero (0999 isn't a true four digit number). It new variables First, Third, Last and assigns to them the values that should be equal to the corresponding digits (A, C, and D). Finally prolog tries to pairwise unify the digits A, C, and D with their respective variables First, Third, and Last(unification of two variables succeeds if and only if the two variables have the same value). Unification succeeding implies that the number ABCD meets the requirements of the puzzle; so prolog writes the number and leaves the function.

Here is the output of the function
Diesel$ gprolog
GNU Prolog 1.3.1
By Daniel Diaz
Copyright (C) 1999-2009 Daniel Diaz
| ?- [fourdigit].
compiling /Users/Diesel/Archive/code/prolog/fourdigit/fourdigit.pl for byte code...
/Users/Diesel/Archive/code/prolog/fourdigit/fourdigit.pl compiled, 25 lines read - 2419 bytes written, 17 ms

(1 ms) yes
| ?- fourdigitnum.
1349

true ?


As you can see (and if you hadn't already figured it out), the number in question is 1349. The last line 'true?' allows you to tell the function to try another number or try all possible numbers. It turns out that 1349 is the only number that works (0000 would work if A could be zero), and this can be figured out with just pencil and paper or even in the heads of some bright folks =) either way I enjoyed writing this code because it is a small, quickly written example of prolog's brute forcing power!

Check back for more puzzles and prolog solutions soon.

Labels: ,

Thursday, February 26, 2009

~/.bashrc won't load in OSX terminal

While I was in my /etc/bashrc tonight setting up macports I thought of something that has always bugged me but I never really cared enough to figure out.

Why doesn't by personal bash config file (~/.bashrc) not load when I launch a terminal?

I still don't know the answer to that question, but I at least figured out a really simple hack tonight.

inside of /etc/bashrc you simply need to add the line(I added it at the end)
~/.bashrc

This will load all your personal aliased and settings right bash loads and performs the system-wide aliases and config options. If this solves all your problems, then you can go about your day and I was glad to help ^.^ But this could actually be improved upon if people care to follow along.

Depending on the contents of your ~/.bashrc file, you may need to use this line instead
. ~/.bashrc

that is, <dot><space>~/.bashrc. this executes all the commands within ~/.bashrc within the current shell process. This does not make a difference for most commands. The only only I know of is cd (change directory) which. Scripts which call the system command cd must be executed with <dot><space> in order for that change to be reflected in the shell executing the script after execution ends.

So there you go, now you can make all terminals you launch cd into your code root directory when you launch the terminal. I don't but I don't judge you ^.^

Labels: , ,