Showing posts with label Competition. Show all posts
Showing posts with label Competition. Show all posts

Friday, 6 February 2009

functional => prizes #1: We have a winner

TrophyThe closing date of the the inaugural Functional Fun programming competition has passed, and I’m relieved to say I actually had some judging to do. Further to my post on Monday, two bits are now required to hold the number of submissions I have to choose between - though not used to full capacity.

The Winner

Our winner is Jamie, a High School Student from Massachusetts, whose code surpasses in elegance my attempt at solving my own problem. Jamie gets the prize of £25 in Amazon gift vouchers. Honourable mention goes to Dan Dumitru who was the first to submit a solution.

Congratulations to Jamie, and thanks to all 10 of you who entered!

The Solution

The puzzle that Jamie and Dan solved was as follows;

Which Mathematicians have found themselves prime positions in the grid below?

HiddenMathematiciansImage

So who were the coy Mathematicians, and how did they conceal themselves? Well, given the maths connection, “prime positions” must surely have something to do with prime numbers? So what happens if we count along the cells of the grid (wrapping over from the end of one row to the beginning of the following row) and note down the characters we find at indices which are prime-numbered?

Interesting! We get the string

tinyurl#teyuir#gqrtyh#hyirtq##

Teyuir? What theorem did he prove? must be Slovakian with a name like that. Now tinyurl – that rings a bell. Hey! I wonder what tinyurl.com/ teyuir looks like?

WikipediaRestApiResult

Huh! XML?

But wait: Leonhard Euler. There’s a name I recognise. Didn’t he dabble in mathematics, and prove a lemma or two?

And having reasoned thus, all that remains is to craft a handful of LINQ queries to winkle all three mathematicians out of the hiding places: Euler, Euclid and Cantor.

Here’s Jamie’s solution in its entirety. The code speaks for itself.

// Gets all the nonwhitespace characters
var rawChars = from ch in File.ReadAllText("HiddenMathematicians.txt") 
               where !char.IsWhiteSpace(ch)
               select ch;

// Gets only the chars at prime indices (char[0] is at index 1)
var primeChars = from num in Enumerable.Range(2, rawChars.Count() - 1) // creates all indices from 1 to numRawChars
                 let divisors = Enumerable.Range(2, num - 2) // all possible divisors, 2 to num - 1
                 let isPrime = !divisors.Any(div => num % div == 0)
                 where isPrime
                 select rawChars.ElementAt(num - 1);

// All the words, including the beginning hint
var words = from str in primeChars.Aggregate(string.Empty, (str, ch) => str += ch).Split('#')
            where !string.IsNullOrEmpty(str)
            select str;

var names = from word in words.Skip(1)
            let url = "http://" + words.First() + ".com/" + word
            let xmlData = XDocument.Load(url)
            let element = xmlData.XPathSelectElement("./api/query/pages/page")
            let attribute = element.Attribute("title")
            select attribute.Value;

foreach (var name in names)
    Console.WriteLine(name);

Console.ReadLine();

Monday, 2 February 2009

Deadline fast approaching

Don’t forget that Wednesday Noon o’clock sharp GMT is the deadline for functional => prizes #1.

So far the number of entries I’ve received can be recorded in one bit: if you enter you have a statistically very good chance of walking away with the lavish prize.

Don’t let the riddle put you off. Dan, in the comments gives you a clue that is more or less along the right lines, though you might find that the Mathematicians are rather coy about using their names.

Tuesday, 20 January 2009

functional => prizes #1

Ladies and Gentlemen, roll up, roll up for the first ever Functional Fun programming competition, where Functional => Prizes.

Many moons ago, at the Microsoft PDC, I won a copy of Windows Vista Ultimate; said software being superfluous to my requirements, I held an auction, and I now have a pot of dosh which I intend to distribute via this blog. And what better way to do that than to hold competitions? Hopefully the stash will stretch to cover a couple throughout the course of the year, so stay subscribed.

Almost a year ago, I launched this blog with a series of posts on solving Project Euler. This competition gives me a chance to turn the tables and try my hand at problem setting.

Thus without further ado:

The Puzzle

Which Mathematicians have found themselves prime positions in the grid below?

HiddenMathematiciansImage

You’ll probably want to download this grid in text form.

The Rules

  1. All solutions must be emailed to (samuel [dot] jack [at] gmail [dot] com) before Midday (GMT) February 4th 2009.
  2. Don’t just give the answer (I’ve already got a pretty good idea what it might be!): what I want to see is code - especially code that goes all the way from input to final answer.
  3. The prize will be awarded, not to the first correct answer, but to the solution which, in the opinion of the judge, is the most elegant.
  4. Competitors should be aware that C#, particularly in its Version 3 and 4 incarnations, tops the judge’s elegance rankings; this is not to dissuade them from making entries in other languages – merely to let them know that if they choose to enter using an inferior alternative language they’ll have to work harder to make the elegance apparent.
  5. The prize will be £25 in Amazon vouchers – but if you are lucky enough to win, and unfortunate enough to live in one of those foreign places (you know, the kind that get rid of their Head of State every few years, and have to choose a new one) then I’ll do my best to accommodate you with a prize in your local currency.
  6. If you submit a solution, but think you can better it, don’t hesitate to send another.

Your Choice

Now what are you going to do?

A. Keep quiet about the competition in the hope of increasing your chances of winning?

B. Tell as many of your closest friends as possible in order to increase the publicity and your standing in the community when you win the Inaugural Functional => Prizes challenge?