A Farmer With Four Stones.....

by Rob 20. January 2012 12:08

A farmer had a stone that he used to measure grain on his scale. One day his neighbor borrowed the stone; and when he returned; it was broken into four pieces. The neighbor was very apologetic; but the farmer thanked the neighbor for doing him a big favor. The farmer said that now he can measure his grain in one pound increments starting at one pound all the way to forty pounds (1; 2; 3; 17; 29; 37; etc.) using these four stones. How much do the four stones weigh?

Solution with LINQ...

 var a = ((from myA in Enumerable.Range(1, 40)
		 from myB in Enumerable.Range(1, 40)
		 from myC in Enumerable.Range(1, 40)
		 from myD in Enumerable.Range(1, 40)
			 where myA + myB + myC + myD == 40
				 && myA <= myB
				 && myB <= myC
				 && myC <= myD
		 select new[] {myA, myB, myC, myD}).Where(
					   myNum => Enumerable.Range(1, 40).All(
								target => ((from myn1 in new[] { 0, 1, -1 }
											from myn2 in new[] { 0, 1, -1 }
											from myn3 in new[] { 0, 1, -1 }
											from myn4 in new[] { 0, 1, -1 }
											select myn1*myNum[0]
												   + myn2*myNum[1]
												   + myn3*myNum[2]
												   + myn4*myNum[3]).
											  Any
											  (x => x == target))))).ToList();
Tags: ,
Categories:

Checking Gmail Messages With C#

by Rob 11. December 2011 00:06

 

I used OpenPop.NET for the first time yesterday.  I highly recommend it; it's free, open-source, really simple and it seems to work.

    List<Message> myMail = FetchAllMessages("pop.gmail.com", 995, true, "MyEmail@gmail.com", "mysupersecretpasswordshhhhh!");
    public static List<Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password)
    {
        // The client disconnects from the server when being disposed
        using (Pop3Client client = new Pop3Client())
        {
            // Connect to the server
            client.Connect(hostname, port, useSsl);

            // Authenticate ourselves towards the server
            client.Authenticate(username, password);

            // Get the number of messages in the inbox
            int messageCount = client.GetMessageCount();

            // We want to download all messages
            List<Message> allMessages = new List<Message>(messageCount);

            // Messages are numbered in the interval: [1, messageCount]
            // Ergo: message numbers are 1-based.
            // Most servers give the latest message the highest number
            for (int i = messageCount; i > 0; i--)
            {
                allMessages.Add(client.GetMessage(i));
            }

            // Now return the fetched messages
            return allMessages;
        }
    }
Tags: , ,
Categories:

Sudoku Game

by Rob 16. May 2011 20:05

There's no shortage of Sudoku games, tutorials, and even source code, online....but I had a slow weekend and figured I'd take a stab at it.  One of these days, I'll rewrite it in F#.  Anyway - here's the core of it:

    Public Sub Solve()
        While SetObviousValues()            
        End While

        If IsSolved Then
            Print()
            Exit Sub
        End If

        Dim BestPoint As Point = GetLowestPossibleValuePoint()

        If BestPoint.X <> -1 Then
            Dim GuessVals() As Integer = GetPossibleValues(BestPoint.X, BestPoint.Y)

            If GuessVals.Count <> 0 Then
                For i As Integer = 0 To GuessVals.Count - 1                    
                    Dim newNumbers(,) As Integer = _Numbers.Clone
                    newNumbers(BestPoint.X, BestPoint.Y) = GuessVals(i)
                    Dim newGrid As New Grid(newNumbers)

                    newGrid.Solve()
                Next
            End If
        End If

    End Sub

This will find all possible solutions to a given puzzle.  Here's how'd you use it (as a console application)...

        Dim StartVal(,) As Integer = { _
                                    {0, 0, 0, 0, 0, 0, 0, 7, 6}, _
                                    {0, 0, 0, 0, 7, 8, 0, 0, 2}, _
                                    {8, 0, 0, 0, 4, 2, 3, 0, 0}, _
                                    {1, 0, 9, 0, 0, 0, 0, 0, 0}, _
                                    {0, 2, 0, 7, 8, 9, 0, 4, 0}, _
                                    {0, 0, 0, 0, 0, 0, 9, 0, 5}, _
                                    {0, 0, 2, 8, 6, 0, 0, 0, 7}, _
                                    {7, 0, 0, 2, 1, 0, 0, 0, 0}, _
                                    {4, 5, 0, 0, 0, 0, 0, 0, 0}}

        Dim myGrid As New Grid(StartVal)

        myGrid.Print()
        Console.Read()
        myGrid.Solve()

The source is available here.  

Or, you can play the online version here

Categories: