<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: Category fun stuff</title>
    <link>http://blog.moertel.com/articles/category/fun-stuff</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Fun stuff: Video of Type B Erie steam shovel in action!</title>
      <description>&lt;p&gt;As an update to my &lt;a href="http://blog.moertel.com/articles/2008/08/09/fun-stuff-historical-construction-equipment-associations-2008-convention"&gt;previous post on the 2008 convention of the Historical Construction Equipment Association&lt;/a&gt;, I have posted an action-packed video of a Type-B Erie steam-powered shovel! Wait until you see this old beast belch steam and smoke and you hear it &lt;em&gt;chug, clank,&lt;/em&gt; and &lt;em&gt;huff and puff&lt;/em&gt; &amp;#8211; it&amp;#8217;s like stepping back in time. And it&amp;#8217;s definately &lt;em&gt;fun stuff&lt;/em&gt;!&lt;/p&gt;


&lt;div class="slide"&gt;
&lt;a href="http://www.flickr.com/photos/tmoertel/2750568956/"&gt;&lt;img src="http://farm4.static.flickr.com/3094/2750568956_9da340eddb_m.jpg" alt="" /&gt;&lt;/a&gt;
&lt;/div&gt;

	&lt;p&gt;I took this footage on 8 August 2008 in Brownsville, Pennsylvania. (I also have footage of other equipment &amp;#8211; dozers, draglines, trucks, shovels, and more. Let me know if you&amp;#8217;re interested, and I&amp;#8217;ll upload those, too.)&lt;/p&gt;


&lt;div class="update"&gt;
&lt;strong&gt;Update:&lt;/strong&gt; I have uploaded the rest of my footage to flickr:

	&lt;ul&gt;
	&lt;li&gt;Thew &amp;#8220;O&amp;#8221; steam shovel&lt;/li&gt;
		&lt;li&gt;Dragline&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;CAT 955&lt;/span&gt; bulldozer&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;CAT D8&lt;/span&gt; bulldozer&lt;/li&gt;
		&lt;li&gt;Vintage Army dump truck&lt;/li&gt;
	&lt;/ul&gt;


See them all in &lt;a href="http://flickr.com/photos/tmoertel/sets/72157606638346521/"&gt;my &lt;span class="caps"&gt;HCEA 2008&lt;/span&gt; photostream&lt;/a&gt;.
&lt;/div&gt;</description>
      <pubDate>Sun, 10 Aug 2008 14:12:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:fc0aa415-ec6c-44aa-9e5b-86150afd55f1</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2008/08/10/fun-stuff-video-of-type-b-erie-steam-shovel-in-action</link>
      <category>fun stuff</category>
      <category>hcea</category>
      <category>hcea2008</category>
      <category>fun_stuff</category>
      <category>video</category>
      <category>steam_shovel</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/760</trackback:ping>
    </item>
    <item>
      <title>Fun stuff: Historical Construction Equipment Association's 2008 convention</title>
      <description>&lt;p&gt;Today, my dad and I went to the 2008 annual convention of the &lt;a href="http://www.hcea.net/"&gt;Historical Construction Equipment Association&lt;/a&gt;.  We were impressed with the quantity and quality of the machinery on &lt;em&gt;active&lt;/em&gt; display: steam shovels, dozers, graders, crawlers, scrapers, cranes, steamrollers, and a bunch of other old but well-maintained construction equipment. I&amp;#8217;m talking dozens of massive machines &amp;#8211; not just sitting there, but &lt;em&gt;working&lt;/em&gt;!&lt;/p&gt;


	&lt;p&gt;This year&amp;#8217;s convention is in Brownsville, Pennsylvania and runs through August 10, 2008. If you are within driving distance and think smoke-belching, earth-shaking construction equipment is fun stuff, don&amp;#8217;t miss it. There&amp;#8217;s still time to go.&lt;/p&gt;


	&lt;p&gt;If you can&amp;#8217;t make it, &lt;a href="http://flickr.com/photos/tmoertel/sets/72157606638346521/"&gt;I took some photos for you&lt;/a&gt;. Not quite the real thing, but better than nothing.&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://community.moertel.com/~thor/pix/blog-20080809/hcea2008-steam-shovel.jpg" alt="" /&gt;&lt;/p&gt;


&lt;div class="update"&gt;
&lt;strong&gt;Update:&lt;/strong&gt; I have posted a &lt;a href="http://blog.moertel.com/articles/2008/08/10/fun-stuff-video-of-type-b-erie-steam-shovel-in-action"&gt;video of a Type B Erie steam shovel&lt;/a&gt; &amp;#8211; the one pictured above &amp;#8211; and it&amp;#8217;s &lt;em&gt;in action&lt;/em&gt;! Don&amp;#8217;t miss it.
&lt;/div&gt;</description>
      <pubDate>Sat, 09 Aug 2008 20:23:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:48dfe866-fb16-480d-b502-4425c1874c9b</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2008/08/09/fun-stuff-historical-construction-equipment-associations-2008-convention</link>
      <category>fun stuff</category>
      <category>hcea</category>
      <category>hcea2008</category>
      <category>fun_stuff</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/759</trackback:ping>
    </item>
    <item>
      <title>Solving the Google Code Jam &amp;quot;countPaths&amp;quot; problem in Perl</title>
      <description>&lt;p&gt;As promised, here&amp;#8217;s a Perl version of a dynamic-programming-based solver
for the Google Code Jam &amp;#8220;countPaths&amp;#8221; problem.  It is a straight
translation of my &lt;a href="http://blog.moertel.com/articles/2006/08/16/solving-the-google-code-jam-countpaths-problem-in-ruby"&gt;improved Ruby implementation&lt;/a&gt;.
As you might expect, the Perl version was pretty fast.  It proved faster than the
other scripting-language implementations I tried (in this rather unscientific benchmark, not to be taken seriously):&lt;/p&gt;


&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Implementation&lt;/th&gt;&lt;th&gt;Run time (s)&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="http://blog.moertel.com/articles/2006/08/15/solving-the-google-code-jam-countpaths-problem-in-haskell"&gt;Haskell&lt;/a&gt;&lt;/td&gt;
    &lt;td&gt;0.9&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Perl (code below)&lt;/td&gt;
    &lt;td&gt;1.7&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336"&gt;Python&lt;/a&gt;&lt;/td&gt;
    &lt;td&gt;2.8&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="http://blog.moertel.com/articles/2006/08/16/solving-the-google-code-jam-countpaths-problem-in-ruby"&gt;Ruby&lt;/a&gt;&lt;/td&gt;
    &lt;td&gt;4.2&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

	&lt;p&gt;All timings were taken while solving the maximum-size, all-the-same-letter
problem on my 1.8-GHz Opteron box.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s the Perl implementation:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;#!/usr/bin/perl

# Tom Moertel &amp;lt;tom@moertel.com&amp;gt;
# 2006-08-16
#
# Perl-based solution to the Google Code Jam problem "countPaths".
# See http://www.cs.uic.edu/~hnagaraj/articles/code-jam/ for more.

use strict;
use warnings;

use List::Util 'sum';
use Math::BigInt;

sub count_paths {

  my ($grid, $word) = @_;

  my $rword  = reverse $word;
  my $rowmax = $#$grid;
  my $colmax = length($grid-&amp;gt;[0]);
  my ($slab, $sum);

  for my $i (0 .. length($rword) - 1) {
    my $char = substr $rword, $i, 1;
    ($slab, my $previous_slab) = ([], $slab);
    for my $r (0 .. $rowmax) {
      my ($row, $line) = ($grid-&amp;gt;[$r], $slab-&amp;gt;[$r] ||= []);
      for my $c (0 .. $colmax) {
        $line-&amp;gt;[$c] = $char ne substr($row,$c,1) ? 0 : $i == 0 ? 1 : do {
          $sum = 0;
          my $clo = $c &amp;gt; 0 ? $c - 1 : $c;
          my $chi = $c &amp;lt; $colmax ? $c + 1 : $c;
          for my $nr (($r&amp;gt;0 ? $r-1 : $r) .. ($r&amp;lt;$rowmax ? $r+1 : $r)) {
            for my $nc ($clo .. $chi) {
              $sum += $previous_slab-&amp;gt;[$nr][$nc]
                if $nr != $r || $nc != $c;
            }
          }
          $sum;
        }
      }
    }
  }

  sum map @$_, @$slab;
}

print count_paths([("A"x50)x50], "A"x50), $/;
# 3.03835410591851e+47
&lt;/code&gt;&lt;/pre&gt;

&lt;div class="update"&gt; &lt;strong&gt;Update:&lt;/strong&gt; I simplified the code a whisper by
removing an unnecessary variable &lt;code&gt;$counts&lt;/code&gt;.  Here&amp;#8217;s a diff
if you&amp;#8217;re curious about what&amp;#8217;s changed:

&lt;pre&gt;&lt;code&gt;--- countpaths.pl.orig  2006-08-18 00:16:56.000000000 -0400
+++ /countpaths.pl      2006-08-18 00:19:30.000000000 -0400
@@ -19,11 +19,11 @@
   my $rword  = reverse $word;
   my $rowmax = $#$grid;
   my $colmax = length($grid-&amp;gt;[0]);
-  my ($counts, $slab, $sum);
+  my ($slab, $sum);

   for my $i (0 .. length($rword) - 1) {
     my $char = substr $rword, $i, 1;
-    ($slab, my $previous_slab) = ($counts-&amp;gt;[$i] ||= [], $slab);
+    ($slab, my $previous_slab) = ([], $slab);
     for my $r (0 .. $rowmax) {
       my ($row, $line) = ($grid-&amp;gt;[$r], $slab-&amp;gt;[$r] ||= []);
       for my $c (0 .. $colmax) {
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;Update 2:&lt;/strong&gt; Augmented the introductory paragraph with a parenthetical
comment that reminds readers that these single-fuzzy-data-point-style
timings should not be taken seriously.  Also removed the word
&amp;#8220;bested,&amp;#8221; which might suggest that there is an optimization
contest in play.  Please, no wagering.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update 3:&lt;/strong&gt; Stripped another variable (&lt;code&gt;$j&lt;/code&gt;), which was
completely unused and leftover from previous implementation.  (See
why you shouldn&amp;#8217;t code late at night?)&lt;/p&gt;


&lt;/div&gt;</description>
      <pubDate>Thu, 17 Aug 2006 02:21:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:53c5a877-68a2-450b-8d93-6bdbbd5ffd59</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/08/17/solving-the-google-code-jam-countpaths-problem-in-perl</link>
      <category>programming</category>
      <category>perl</category>
      <category>fun stuff</category>
      <category>google</category>
      <category>code</category>
      <category>jam</category>
      <category>wordpaths</category>
      <category>countpaths</category>
      <category>puzzles</category>
      <category>perl</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/160</trackback:ping>
    </item>
    <item>
      <title>Solving the Google Code Jam &amp;quot;countPaths&amp;quot; problem in Ruby</title>
      <description>&lt;p&gt;Here&amp;#8217;s a Ruby version of a dynamic-programming-based solver
for the Google Code Jam &amp;#8220;countPaths&amp;#8221; problem.  It is essentially
the same as my &lt;a href="http://blog.moertel.com/articles/2006/08/15/solving-the-google-code-jam-countpaths-problem-in-haskell"&gt;earlier Haskell-based solution&lt;/a&gt; (see Update 2), but much slower.  Whereas the Haskell version solves the maximum-size, all-the-same-letter problem in about 0.9 second, the Ruby version requires about 71 seconds.  Maybe somebody who understands Ruby&amp;#8217;s internals better than I do can come up with some optimizations.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s the code:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# Tom Moertel &amp;lt;tom@moertel.com&amp;gt;
# 2006-08-16
#
# Ruby-based solution to the Google Code Jam problem "countPaths" 
# See http://www.cs.uic.edu/~hnagaraj/articles/code-jam/ for more.

class WordPath

  include Enumerable

  def initialize(grid, word)
    @grid, @rword, @counts = grid, word.reverse, {}
  end

  def self.count_paths(grid, word)
    new(grid, word).solve
  end

  def solve
    final_index = @rword.length - 1
    inject(0) { |sum, rc| sum + count_from(final_index, *rc) }
  end

  private

  def count_from(i, r, c)
    @counts[[r, c, i]] ||= begin
      match = @rword[i] == @grid[r][c]
      case
        when i == 0 &amp;#38;&amp;#38; match then 1
        when match then subsum_of_neighbors(r, c, i - 1)
        else 0
      end
    end
  end

  def subsum_of_neighbors(r, c, i)
    sum = 0
    rowlen = @grid[0].size
    for nr in [r - 1, r, r + 1]
      next if nr &amp;lt; 0 or nr &amp;gt;= @grid.size
      for nc in [c - 1, c, c + 1]
        next if nc &amp;lt; 0 || nc &amp;gt;= rowlen
        next unless r != nr || c != nc
        if count = count_from(i, nr, nc)
          sum += count
        end
      end
    end
    sum
  end

  def each
    @grid.each_index do |r|
      @grid[0].size.times { |c| yield([r, c]) }
    end
  end

end

# TESTS

if ENV["TEST"] || ENV["BIG_TEST"]

  require "test/unit" 

  class TestWordPath &amp;lt; Test::Unit::TestCase

    if ENV["BIG_TEST"]
      def test_big_problem
        assert_equal \
          303835410591851117616135618108340196903254429200,
          WordPath.count_paths(["A"*50] * 50, "A"*50)
      end
    end

    if ENV["TEST"]
      def test_count_paths
        w = WordPath
        assert_equal 1,
          w.count_paths(%w{ABC FED GHI}, "ABCDEFGHI")
        assert_equal 2,
          w.count_paths(%w{ABC FED GAI}, "ABCDEA")
        assert_equal 0,
          w.count_paths(%w{ABC DEF GHI}, "ABCD")
        assert_equal 108,
          w.count_paths(%w{AA AA}, "AAAA")
        assert_equal 56448,
          w.count_paths(%w{ABABA BABAB ABABA BABAB ABABA}, "ABABABBA")
        assert_equal 2745564336,
          w.count_paths(%w{AAAAA AAAAA AAAAA AAAAA AAAAA}, "AAAAAAAAAAA")
        assert_equal 0,
          w.count_paths(%w{AB CD}, "AA" )
        assert_equal 1,
          w.count_paths(%w{A}, "A")
      end
    end

  end

end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Set the &lt;code&gt;BIG_TEST&lt;/code&gt; and/or &lt;code&gt;TEST&lt;/code&gt; environment
variables to run the test suites.  For example:&lt;/p&gt;


&lt;pre&gt;$ &lt;code&gt;TEST=1 ./countpaths.rb&lt;/code&gt;
&lt;/pre&gt;

&lt;pre&gt;Loaded suite countpaths
Started
.
Finished in 0.02062 seconds.

1 tests, 8 assertions, 0 failures, 0 errors
&lt;/pre&gt;

	&lt;p&gt;Unless somebody beats me to it,
I&amp;#8217;ll whip up a Perl version for comparison.&lt;/p&gt;


&lt;div class="update"&gt;
&lt;strong&gt;Update:&lt;/strong&gt; I managed to speed up my code by a
factor of 17.  Now the execution time for the maximum-size,
all-the-same-letter problem is down to 4.2 seconds,
which is comparable with implementations in other
languages.  &lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336"&gt;Ivan Peev&amp;#8217;s Python implementation&lt;/a&gt;, for example, is only slightly faster
at 2.8 seconds.

	&lt;p&gt;A performance killer in the previous version was using
a single big hash for my cache.  Now I use a 3D array:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
counts[[i,r,c]]   # one big hash (slower)
counts[i][r][c]   # 3D-array (faster)
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;An additional advantage of the 3D-array is that I can peel off slabs
as I descend the outer layers of nested loops.  For instance,
instead of writing:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;for i in 0 .. 10
  for j in 0 .. 10
    sum += counts[i][j]
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I can lift the &lt;code&gt;counts[i]&lt;/code&gt; slab out of the inner
loop to eliminate &lt;em&gt;j&lt;/em&gt; array-indexing operations:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;for i in 0 .. 10
  slab = counts[i]
  for j in 0 .. 10
    sum += slab[j]
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Here&amp;#8217;s the new code (sans the unit tests, which haven&amp;#8217;t changed):&lt;/p&gt;


&lt;pre&gt;&lt;code style="font-size: smaller"&gt;class WordPath

  A = Array

  def self.count_paths(grid, word)

    rword  = word.reverse
    rowmax = grid.size - 1
    colmax = grid.first.size - 1

    for i in 0 .. rword.size - 1
      letter = rword[i]
      previous_slab, slab = slab, A.new(rowmax+1) { A.new(colmax+1) }
      for r in 0 .. rowmax
        row, line = grid[r], slab[r]
        for c in 0 .. colmax
          line[c] = unless letter == row[c]
            0
          else
            if i == 0
              1
            else
              sum = 0
              clo = c &amp;gt; 0 ? c - 1 : c
              chi = c &amp;lt; colmax ? c + 1 : c
              for nr in (r &amp;gt; 0 ? r - 1 : r) .. (r &amp;lt; rowmax ? r + 1 : r)
                for nc in clo .. chi
                  sum += previous_slab[nr][nc] if nr != r || nc != c
                end
              end
              sum
            end
          end
        end
      end
    end

    sum = 0
    for r in 0 .. rowmax
      for c in 0 .. colmax
        sum += slab[r][c]
      end
    end

    sum

  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;Update 2:&lt;/strong&gt; I tweaked the code snippet above to remove a variable
that I just noticed wasn&amp;#8217;t actually doing anything.&lt;/p&gt;


&lt;/div&gt;</description>
      <pubDate>Wed, 16 Aug 2006 18:54:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:1bf9c8cf-5b20-4039-a64d-020e9ab52830</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/08/16/solving-the-google-code-jam-countpaths-problem-in-ruby</link>
      <category>ruby</category>
      <category>fun stuff</category>
      <category>ruby</category>
      <category>google</category>
      <category>code</category>
      <category>jam</category>
      <category>countpaths</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/155</trackback:ping>
    </item>
    <item>
      <title>Solving the Google Code Jam &amp;quot;countPaths&amp;quot; problem in Haskell</title>
      <description>&lt;p&gt;Via the &lt;a href="http://developers.slashdot.org/article.pl?sid=06/08/14/2146220"&gt;article on this year&amp;#8217;s Google Code
Jam&lt;/a&gt; on
&lt;a href="http://slashdot.org/"&gt;Slashdot&lt;/a&gt; earlier today, I found &lt;a href="http://www.cs.uic.edu/~hnagaraj/"&gt;Hareesh Nagarajan&lt;/a&gt;&amp;#8217;s &lt;a href="http://www.cs.uic.edu/~hnagaraj/articles/code-jam/"&gt;write-up of a previous
year&amp;#8217;s Code-Jam
problem&lt;/a&gt;.  Since
Google often comes up with interesting problems, I decided to give
this one a go.&lt;/p&gt;


	&lt;h3&gt;The problem: count the ways to find a word by walking on a grid&lt;/h3&gt;


	&lt;p&gt;You are given a rectangular grid of letters and a word to find.
You must compute the number of ways to find the word within the grid
using the following rules:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;start at any cell within the grid&lt;/li&gt;
		&lt;li&gt;from there, move to any of the cell&amp;#8217;s eight neighboring cells&lt;/li&gt;
		&lt;li&gt;continue moving from that neighbor to its neighbors, and so on,
  until you have spelled out the word&lt;/li&gt;
		&lt;li&gt;you may visit cells more than once, but you cannot visit
  the same cell twice in a row (i.e., you must &lt;em&gt;move&lt;/em&gt; for each turn)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;For instance, consider the following grid, taken from the examples in
the problem statement:&lt;/p&gt;


&lt;pre&gt;ABC
FED
GAI
&lt;/pre&gt;

	&lt;p&gt;If you were asked to find the word &amp;#8220;AEA&amp;#8221; on this grid, you could do it in
four ways:&lt;/p&gt;


&lt;pre&gt;Way  --Move---
     1   2   3

1:  *BC ABC *BC
    FED F*D FED
    GAI GAI GAI

2:  *BC ABC ABC
    FED F*D FED
    GAI GAI G*I

3:  ABC ABC *BC
    FED F*D FED
    G*I GAI GAI

4:  ABC ABC ABC
    FED F*D FED
    G*I GAI G*I
&lt;/pre&gt;

	&lt;p&gt;If you were asked to find &amp;#8220;ABCD&amp;#8221;, you could do it in only one way:&lt;/p&gt;


&lt;pre&gt;Way  --Move-------- 
     1   2   3   4 

1:  *BC A*C AB* ABC
    FED FED FED FE*
    GAI GAI GAI GAI
&lt;/pre&gt;

	&lt;p&gt;If you were asked to find &amp;#8220;AAB&amp;#8221;, you could not:
there are no &amp;#8220;A&amp;#8221; cells on the grid that have other &amp;#8220;A&amp;#8221; cells
as neighbors.&lt;/p&gt;


	&lt;h3&gt;The tricksy nature of the problem&lt;/h3&gt;


	&lt;p&gt;As you might expect from Google, this puzzle was designed to see
whether your solution can scale.  A simple search will quickly bog
down because each step in the search can expand into vastly more
possibilities, as searching for &amp;#8220;AAAA&amp;#8221; on a seemingly harmless 2&amp;#xD7;2
grid of all &amp;#8220;A&amp;#8221; cells shows &amp;#8211; there are 108 solutions.&lt;/p&gt;


	&lt;p&gt;The problem statement says that the grid may be up to 50&amp;#xD7;50 in
size and the word to find may be up to 50 letters long.  Imagine,
then, that you are asked to find a word composed of 50 &amp;#8220;A&amp;#8221; letters
within a 50&amp;#xd7;50 grid of &amp;#8220;A&amp;#8221; cells.  All of the cells will be valid
starting points, and each will have, on average, slightly less than 8
valid neighbors.  Thus there will be about
50&amp;#160;&amp;#xd7;&amp;#160;50&amp;#160;&amp;#xd7;&amp;#160;8^49 = 4.5e47 ways to find
the word&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;.  Tracing them all would take forever.&lt;/p&gt;


	&lt;p&gt;The trick is figuring out a more efficient way to solve the problem.
Since that&amp;#8217;s the fun part of this problem, I won&amp;#8217;t spoil it for you
by telling you how I did it.  (If you truly want spoilers, you can study
my code.)&lt;/p&gt;


	&lt;h3&gt;My solution&lt;/h3&gt;


	&lt;p&gt;Here is what I came up with.  I&amp;#8217;ll present the code first and then
discuss how to use it.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;The code below is out of date but printed here for
continuity.  See Update 5 for the most-recent revision.&lt;/em&gt;&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;{-

Tom Moertel &amp;lt;tom@moertel.com&amp;gt;
2006-08-15

Haskell-based solution to the Google Code Jam problem "countPaths";
see http://www.cs.uic.edu/~hnagaraj/articles/code-jam/ for more.

-}

module Main (main) where

import Control.Monad
import Data.Array
import qualified Data.Map as M

main = do
    word:gridspec &amp;lt;- liftM words getContents
    print $ (countPaths word (toGridArray gridspec) :: Integer)

countPaths word@(p:_) gridArray =
    sum . M.elems $ foldl step state0 (zip word (tail word))
  where
    state0 = M.fromList [(cell, 1) | (cell, q) &amp;lt;- assocs gridArray, p == q]
    neighbors = toNeighborMap gridArray
    step state fromto = M.fromListWith (+) $ do
        steps &amp;lt;- M.lookup fromto neighbors
        (start, count) &amp;lt;- M.assocs state
        cells &amp;lt;- M.lookup start steps
        cell &amp;lt;- cells
        return (cell, count)

toGridArray gridspec@(l1:_) =
    listArray ((1,1), (length gridspec, length l1)) (concat gridspec)

toNeighborMap gridArray =
    M.fromListWith (M.unionWith (flip (++))) $ do
        (cell, p) &amp;lt;- assocs gridArray
        cell' &amp;lt;- neighbors8 cell
        guard $ inRange (bounds gridArray) cell'
        return ((p, gridArray!cell'), M.singleton cell [cell'])

neighbors8 (r,c) =
    [(r+h, c+v) | h &amp;lt;- [-1..1], v &amp;lt;- [-1..1], h /= 0 || v /= 0]

-- Local Variables:  ***
-- compile-command: "ghc -O2 -o wordpath --make WordPath.hs" ***
-- End: ***
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;My solution generalizes upon the problem statement in a few ways:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;the grid can be any size and the word any length&lt;/li&gt;
		&lt;li&gt;the grid and word can be composed of any comparable data type, not just A&amp;#8211;Z letters (if you use the stdin interface, the code will use Unicode characters)&lt;/li&gt;
		&lt;li&gt;the code will compute exact counts instead of returning -1 for counts greater than 1e9&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;You can enter problems from the command line.  Enter the word first
and then the grid, each row separated by whitespace.  For example:&lt;/p&gt;


&lt;pre&gt;$ &lt;code&gt;./wordpath
AAAAAAAAAAA

AAAAA
AAAAA
AAAAA
AAAAA
AAAAA
^D&lt;/code&gt;

2745564336
&lt;/pre&gt;

	&lt;h3&gt; Give it a try&lt;/h3&gt;


	&lt;p&gt;This was a fun problem to solve.  If you have a little spare time,
give it a try.  I would love to compare results and talk about
strategies.&lt;/p&gt;


&lt;div class="update"&gt;

	&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Fixed typo: Finding &amp;#8220;AAAA&amp;#8221; &amp;#8211; not &amp;#8220;AA&amp;#8221; &amp;#8211; on
a 2&amp;#xd7;2 grid of all &amp;#8220;A&amp;#8221; letters results in a count of 108.  Thanks to Joshua Volz for pointing out my mistake.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update 2:&lt;/strong&gt;  Here&amp;#8217;s a dynamic-programming-based implementation of &lt;em&gt;countPaths&lt;/em&gt; that is
about six times faster than my original implementation when solving the
maximum-size, all-the-same-letter problem:&lt;/p&gt;


&lt;pre&gt;&lt;code style="font-size: smaller"&gt;countPaths word gridArray =
    sum [counts ! (length word, cell) | cell &amp;lt;- cells]
  where
    counts = listArray ((1, (1, 1)), (length word, gridSize)) $
             [countFrom i cell | i &amp;lt;- [1..length word], cell &amp;lt;- cells]

    countFrom i cell
        | i == 1 &amp;#38;&amp;#38; match = 1
        | match           = sum [counts!((i-1),n) | n &amp;lt;- neighbors!cell]
        | otherwise       = 0
      where
        match = rword ! i == gridArray ! cell

    neighbors = listArray (bounds gridArray) $
        [filter (inRange (bounds gridArray)) (neighbors8 cell)
            | cell &amp;lt;- cells ]

    rword    = listArray (1, length word) (reverse word)
    cells    = indices gridArray
    gridSize = snd (bounds gridArray)
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;See &lt;a href="http://programming.reddit.com/info/dni1/comments/cdp59"&gt;the thread started by &amp;#8216;psykotic&amp;#8217; on reddit.com&lt;/a&gt; for more.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update 3:&lt;/strong&gt;  &lt;a href="http://my.opera.com/ipeev/blog/"&gt;Ivan Peev&lt;/a&gt; has solved the problem in Python: &lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336"&gt;Solving the Google Code Jam &amp;#8216;countPaths&amp;#8217; problem in Python&lt;/a&gt;.  Because his implementation uses the same algorithm that my implementation in Update 2 does, it makes a good vehicle for Haskell-versus-Python speed comparisons, an interesting topic in light of the warning Google provides about using Python in the Google Code Jam:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: All submissions have a maximum of 2 seconds of runtime
per test case. This limit is used in harder problems to
force submissions to be of a certain complexity. Because of
the inherent speed differences between Python and the other
offered languages is large, some problems may require extra
optimization or not be solvable using the Python language.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Ivan reports that his Python implementation solves the maximum-size, all-the-same-letter problem in about 8 seconds on an old 1-GHz &lt;span class="caps"&gt;AMD&lt;/span&gt; Athlon.  The Haskell version comes in somewhat faster at 0.9 second on a 1.8-GHz &lt;span class="caps"&gt;AMD&lt;/span&gt; Opteron.  (On the same Opteron, Ivan&amp;#8217;s code clocks in at 2.8 seconds, which is impressive.)&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update 4:&lt;/strong&gt; I have added &lt;a href="http://blog.moertel.com/articles/2006/08/16/solving-the-google-code-jam-countpaths-problem-in-ruby"&gt;a Ruby implementation&lt;/a&gt; and &lt;a href="http://blog.moertel.com/articles/2006/08/17/solving-the-google-code-jam-countpaths-problem-in-perl"&gt;a Perl implementation&lt;/a&gt; and timings, too.  On the the maximum-size, all-the-same-letter problem, Ruby clocks in at 4.2 seconds; Perl in 1.7 seconds.  See the Perl implementation for a summary table of the timings.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update 5:&lt;/strong&gt; As I promised reader Kartik in a comment, here is a
further-simplified, yet 25-percent-faster, version of my
implementation in Update 2.  This version eliminates the cache in
favor of a current-state array that is folded through the successive
letters of the target word.  The result of the fold operation is the
final state array, whose elements are summed to yield the final
result.  Here&amp;#8217;s the complete code:&lt;/p&gt;


&lt;pre&gt;&lt;code style="font-size: smaller"&gt;{-

Tom Moertel &amp;lt;tom@moertel.com&amp;gt;
2006-08-15 (revised 2006-09-01)

Haskell-based solution to the Google Code Jam problem "countPaths" 
See http://www.cs.uic.edu/~hnagaraj/articles/code-jam/ for more.

This implementation is based on the dynamic-programming strategy
mentioned by reddit.com user "psykotic":
http://programming.reddit.com/info/dni1/comments/cdp59.

-}

module Main (main) where

import Control.Monad
import Data.Array

main = do
    word:gridspec &amp;lt;- liftM words getContents
    print $ (countPaths word (toGridArray gridspec) :: Integer)

countPaths word grid =
    sum . elems $ foldl move counts0 (tail (reverse word))
  where
    move counts c  = step c $ sum . map (counts!) . neighbors
    counts0        = step (last word) (const 1)
    step c f       = listArray (bounds grid) $ map (match c f) cells
    match c f cell = if c == grid!cell then f cell else 0
    neighbors cell = filter (inRange (bounds grid)) (neighbors8 cell)
    cells          = indices grid

toGridArray gridspec@(l1:_) =
    listArray ((1,1), (length gridspec, length l1)) (concat gridspec)

neighbors8 (r,c) =
    [(h, v) | h &amp;lt;- [r-1..r+1], v &amp;lt;- [c-1..c+1], h /= r || v /= c]

-- Local Variables:  ***
-- compile-command: "ghc -O2 -o wordpathdp --make WordPathDP.hs" ***
-- End: ***
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;hr /&gt;

	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt;  I believe that the exact count is
303&amp;#160;835&amp;#160;410&amp;#160;591&amp;#160;851&amp;#160;117&amp;#160;616&amp;#160;135&amp;#160;618&amp;#160;108&amp;#160;340&amp;#160;196&amp;#160;903&amp;#160;254&amp;#160;429&amp;#160;200 (approx. 3.04e47).  It takes about &lt;del&gt;six seconds&lt;/del&gt; 0.75 second to compute on a 1.8-GHz &lt;span class="caps"&gt;AMD64&lt;/span&gt; box running Linux.&lt;/p&gt;</description>
      <pubDate>Tue, 15 Aug 2006 17:01:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:2521f94c-63b3-4a99-8595-0dfa3db7a120</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/08/15/solving-the-google-code-jam-countpaths-problem-in-haskell</link>
      <category>programming</category>
      <category>haskell</category>
      <category>fun stuff</category>
      <category>haskell</category>
      <category>google</category>
      <category>code</category>
      <category>jam</category>
      <category>wordpaths</category>
      <category>countpaths</category>
      <category>puzzles</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/154</trackback:ping>
    </item>
    <item>
      <title>LectroTest: new release, new talk, and the new LectroTest Emporium!</title>
      <description>&lt;div class="centered"&gt;
&lt;img src="http://community.moertel.com/ss/space/LectroTest/LectroTestLogoSmall.png" title="LectroTest Robot" alt="LectroTest Robot" /&gt;
&lt;/div&gt;

	&lt;p&gt;I have a bunch of &lt;a href="http://community.moertel.com/LectroTest"&gt;LectroTest&lt;/a&gt; news.  LectroTest, as you may know, is a specification-based,
automatic testing system for Perl.  It may look like Haskell&amp;#8217;s &lt;a href="http://www.cs.chalmers.se/~rjmh/QuickCheck/"&gt;QuickCheck&lt;/a&gt;, but it tastes like sweet, sweet Perl.&lt;/p&gt;


	&lt;h3&gt;LectroTest 0.3500 was released&lt;/h3&gt;


	&lt;p&gt;This version adds automatic tools for recording and playing back
failures.  Using them, you can automatically build regression-testing
suites and incorporate them into your testing plan.  All it takes
is one new line of code:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;use Test::LectroTest
    regressions =&amp;gt; "regressions.txt";   # &amp;lt;-- that's it!
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;See the &lt;a href="http://search.cpan.org/dist/Test-LectroTest/"&gt;docs on &lt;span class="caps"&gt;CPAN&lt;/span&gt;&lt;/a&gt; for more.&lt;/p&gt;


	&lt;p&gt;My thanks to Steffen Müller, who suggested the feature and is already
using it in cool stuff such as &lt;a href="http://search.cpan.org/dist/Number-WithError/"&gt;Number::WithError&lt;/a&gt;.&lt;/p&gt;


	&lt;h3&gt;Slides from &amp;#8220;Testing Tips with LectroTest&amp;#8221; are now online&lt;/h3&gt;


	&lt;p&gt;You can get the slides from my talk to the Pittsburgh Perl Mongers on
2006-06-14 here: &lt;a href="http://community.moertel.com/ss/space/Talks/Testing+Tips+with+LectroTest"&gt;Talk / Testing Tips with
LectroTest&lt;/a&gt;.
In the talk, I covered some of the newer LectroTest features, such as
regression testing and Test::LectroTest::Compat, which lets you mix
LectroTest with other Perl testing modules.&lt;/p&gt;


	&lt;h3&gt;The LectroTest Emporium opens!&lt;/h3&gt;


	&lt;p&gt;I have very little artistic ability.  Nevertheless, alarming numbers
of people seem to love the fiercely metallic mascot I created for
LectroTest.&lt;/p&gt;


	&lt;p&gt;At the last Perl Mongers meeting, for example, people
actually told me (somewhat sternly) I should put the adorable LectroTest Robot on
t-shirts.  I am now delighted to announce that I have taken their
advice:&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Introducing: &lt;em&gt;&lt;a href="http://www.cafepress.com/lectrotest"&gt;The LectroTest Emporium&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Some important points:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Yes, it&amp;#8217;s a CafePress store&lt;/li&gt;
		&lt;li&gt;I&amp;#8217;m not making any money on these things&lt;/li&gt;
		&lt;li&gt;I&amp;#8217;m using &lt;em&gt;direct printing&lt;/em&gt;, not heat-transfer printing, so
  the Robot won&amp;#8217;t crack, feel stiff, or suffer from a yellowish
  transfer background.  (CafePress has a &lt;a href="http://www.cafepress.com/cp/info/help/help_printingprocess.aspx"&gt;comparison of the methods&lt;/a&gt; if you want the full details.)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Some items I have moral reservations about offering:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://www.cafepress.com/lectrotest.63672177"&gt;LectroTest Robot Teddy Bear&lt;/a&gt; -
  Who would be so reckless as to allow something as fierce and as powerful
  as the LectroTest Robot to come into &lt;em&gt;direct contact&lt;/em&gt; with a defenseless, cuddly
  teddy bear?&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.cafepress.com/lectrotest.63669666"&gt;LectroTest Robot Baby Bib&lt;/a&gt; -
  Actually, this is a great idea: your infant and the Robot 
  exist in a symbiotic relationship.  When your baby gets food all over the
  bib, the Robot will consume it (using a electrochemical process not
  entirely dissimilar to our human concept of &amp;#8220;digestion&amp;#8221;).  Thus is the
  baby cleaned and the Robot fueled.  It&amp;#8217;s win-win.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.cafepress.com/lectrotest.63672178"&gt;LectroTest Robot Dog T-Shirt&lt;/a&gt; -
  I am fairly certain that the immense weight of the Robot would easily
  crush any smaller animal.  This product strikes me as a very bad idea.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;&lt;a href="http://www.cafepress.com/lectrotest#apparel"&gt;The T-shirts&lt;/a&gt;, on the other hand, are the robot&amp;#8217;s meow.  Check out the
full collection at &lt;a href="http://www.cafepress.com/lectrotest"&gt;The LectroTest Emporium&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Tue, 27 Jun 2006 14:21:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:eb99eca33b05593075a39ab873549308</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/06/27/lectrotest-new-release-new-talk-and-the-new-lectrotest-emporium</link>
      <category>perl</category>
      <category>fun stuff</category>
      <category>testing</category>
      <category>marketing</category>
      <category>talks</category>
      <category>perl</category>
      <category>testing</category>
      <category>lectrotest</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/72</trackback:ping>
    </item>
    <item>
      <title>The &amp;quot;perfect shuffles&amp;quot; puzzle (solved in Haskell)</title>
      <description>&lt;p&gt;I ran across a fun programming puzzle (via
&lt;a href="http://weblog.raganwald.com/2006/03/encounter-with-programming-interview.html"&gt;Raganwald&lt;/a&gt;):&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Given a deck of &lt;em&gt;n&lt;/em&gt; unique cards, cut the deck &lt;em&gt;i&lt;/em&gt; cards from top
    and perform a perfect shuffle. A perfect shuffle begins by putting
    down the bottom card from the top portion of the deck followed by
    the bottom card from the bottom portion of the deck followed by
    the next card from the top portion, etc., alternating cards until
    one portion is used up. The remaining cards go on top. The problem
    is to find the number of perfect shuffles required to return the
    deck to its original order. Your function should be declared as:&lt;/p&gt;
    &lt;p&gt;&lt;code&gt;static long shuffles(int nCards,int iCut);&lt;/code&gt;&lt;/p&gt;
    &lt;p&gt;Please send the result of shuffles(1002,101) along with your
    program and your resume to &amp;#8216;resume&amp;#8217; at nextag.com.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;It&amp;#8217;s a fun problem, so give it a try before reading on.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Warning: small spoilers ahead&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The first thing I usually do when I encounter a new problem is explore
it until I have a feel for its essence.  For this problem, the
shuffle algorithm seemed like a good starting point:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;shuffle&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;reverse&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;uncurry&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;flip&lt;/span&gt; &lt;span class='varid'&gt;interleave&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;splitAt&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='comment'&gt;-&lt;/span&gt;&lt;span class='varid'&gt;i&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;reverse&lt;/span&gt;

&lt;span class='varid'&gt;interleave&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;xs&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;ys&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;interleave&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
&lt;span class='varid'&gt;interleave&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;     &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
&lt;span class='varid'&gt;interleave&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;     &lt;span class='varid'&gt;ys&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;From within GHCi, I watched a few iterations, using a ten-card deck as
input:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varop'&gt;*&lt;/span&gt;&lt;span class='conid'&gt;Main&lt;/span&gt;&lt;span class='varop'&gt;&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;mapM_&lt;/span&gt; &lt;span class='varid'&gt;print&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;take&lt;/span&gt; &lt;span class='num'&gt;10&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='varid'&gt;iterate&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;shuffle&lt;/span&gt; &lt;span class='num'&gt;10&lt;/span&gt; &lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='keyglyph'&gt;..&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;6&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;8&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;4&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;3&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;9&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;7&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;2&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='num'&gt;5&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;After a bit of study, a light-bulb appeared over my head, and I
realized something about the fundamental nature of the problem that I
had overlooked earlier.  (I won&amp;#8217;t spoil the fun by saying what it
was.)  From that point, the solution was easy to implement:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;Main&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;main&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;

&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Control&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;Monad&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;liftM&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Data&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;Array&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Data&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;Set&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Set&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;empty&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;insert&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;member&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;System&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;Environment&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;getArgs&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;

&lt;span class='varid'&gt;main&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;i&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='keyword'&gt;_&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;liftM&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;map&lt;/span&gt; &lt;span class='varid'&gt;read&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varid'&gt;getArgs&lt;/span&gt;
    &lt;span class='varid'&gt;print&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;shuffles&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;

&lt;span class='varid'&gt;shuffles&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Int&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Int&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Integer&lt;/span&gt;
&lt;span class='varid'&gt;shuffles&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;foldl&lt;/span&gt; &lt;span class='varid'&gt;lcm&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;map&lt;/span&gt; &lt;span class='varid'&gt;toInteger&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;fst&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;foldl&lt;/span&gt; &lt;span class='varid'&gt;dfs&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;empty&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='varid'&gt;deck'&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;deck&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;deck'&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt; &lt;span class='keyglyph'&gt;..&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='comment'&gt;-&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;shuffle&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='varid'&gt;deck&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;perms&lt;/span&gt;          &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;array&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='comment'&gt;-&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;zip&lt;/span&gt; &lt;span class='varid'&gt;deck'&lt;/span&gt; &lt;span class='varid'&gt;deck&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;dfs&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;ls&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;vs&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varid'&gt;j&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;if&lt;/span&gt; &lt;span class='varid'&gt;member&lt;/span&gt; &lt;span class='varid'&gt;j&lt;/span&gt; &lt;span class='varid'&gt;vs&lt;/span&gt; &lt;span class='keyword'&gt;then&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;ls&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;vs&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyword'&gt;else&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;l&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;ls&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;vs'&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
                     &lt;span class='keyword'&gt;where&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;l&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;vs'&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;follow&lt;/span&gt; &lt;span class='num'&gt;0&lt;/span&gt; &lt;span class='varid'&gt;j&lt;/span&gt; &lt;span class='varid'&gt;vs&lt;/span&gt;
    &lt;span class='varid'&gt;follow&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='varid'&gt;vs&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;if&lt;/span&gt; &lt;span class='varid'&gt;member&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='varid'&gt;vs&lt;/span&gt; &lt;span class='keyword'&gt;then&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;&lt;span class='varid'&gt;vs&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
                     &lt;span class='keyword'&gt;else&lt;/span&gt; &lt;span class='varid'&gt;follow&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='varop'&gt;+&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;perms&lt;/span&gt;&lt;span class='varop'&gt;!&lt;/span&gt;&lt;span class='varid'&gt;i&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;insert&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='varid'&gt;vs&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;

&lt;span class='varid'&gt;shuffle&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;reverse&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;uncurry&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;flip&lt;/span&gt; &lt;span class='varid'&gt;interleave&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;splitAt&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='comment'&gt;-&lt;/span&gt;&lt;span class='varid'&gt;i&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;reverse&lt;/span&gt;

&lt;span class='varid'&gt;interleave&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;xs&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;ys&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;interleave&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
&lt;span class='varid'&gt;interleave&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;     &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
&lt;span class='varid'&gt;interleave&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;     &lt;span class='varid'&gt;ys&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The following command compiles the program:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;$ ghc -O2 -o shuffle --make PerfectShuffle.hs
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The program computes the requested solution in about 4&amp;nbsp;ms on my
1.8-GHz box:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;$ time ./shuffle 1002 101
5812104600

real    0m0.006s
user    0m0.004s
sys     0m0.004s
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you Google around, you can find other solutions, most of them
implemented in Java.&lt;/p&gt;


	&lt;h3&gt;Solve puzzles!  They&amp;#8217;re good for you.&lt;/h3&gt;


	&lt;p&gt;Solving programming puzzles is a fun way to exercise parts of the
brain that day-to-day coding rarely uses.  If you are looking for
some more puzzles, check out what&amp;#8217;s archived at the &lt;a href="http://community.moertel.com/ss/space/Programming+Fun+Challenge"&gt;Programming
Fun Challenge&lt;/a&gt;
page.  Some are simple but others are downright tricksy.&lt;/p&gt;


&lt;div class="update"&gt;
&lt;strong&gt;Update 2006-03-24:&lt;/strong&gt; Last night I thought of a simple optimization to
my original implementation, and this morning I revised my code.  The
optimization reduces the run time for the (1002, 101) case from about
16&amp;nbsp;ms to about 4&amp;nbsp;ms.  I have replaced the original code from
this article with the new code, which is slightly longer.  For comparison, here is the original implementation:

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;shuffles&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Int&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Int&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Integer&lt;/span&gt;
&lt;span class='varid'&gt;shuffles&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;foldl&lt;/span&gt; &lt;span class='varid'&gt;lcm&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;map&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;fromIntegral&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;cycleLength&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='varid'&gt;deck'&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;deck&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;deck'&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='keyglyph'&gt;..&lt;/span&gt;&lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='comment'&gt;-&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;shuffle&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='varid'&gt;deck&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;perms&lt;/span&gt;         &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;array&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='comment'&gt;-&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;zip&lt;/span&gt; &lt;span class='varid'&gt;deck'&lt;/span&gt; &lt;span class='varid'&gt;deck&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='varid'&gt;cycleLength&lt;/span&gt; &lt;span class='varid'&gt;j&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;follow&lt;/span&gt; &lt;span class='varid'&gt;j&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;perms&lt;/span&gt;&lt;span class='varop'&gt;!&lt;/span&gt;&lt;span class='varid'&gt;j&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Int&lt;/span&gt;
    &lt;span class='varid'&gt;follow&lt;/span&gt; &lt;span class='varid'&gt;i0&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;if&lt;/span&gt; &lt;span class='varid'&gt;i&lt;/span&gt; &lt;span class='varop'&gt;==&lt;/span&gt; &lt;span class='varid'&gt;i0&lt;/span&gt; &lt;span class='keyword'&gt;then&lt;/span&gt; &lt;span class='varid'&gt;n&lt;/span&gt; &lt;span class='keyword'&gt;else&lt;/span&gt; &lt;span class='varid'&gt;follow&lt;/span&gt; &lt;span class='varid'&gt;i0&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;n&lt;/span&gt;&lt;span class='varop'&gt;+&lt;/span&gt;&lt;span class='num'&gt;1&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;perms&lt;/span&gt;&lt;span class='varop'&gt;!&lt;/span&gt;&lt;span class='varid'&gt;i&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;&lt;strong&gt;Update 2006-03-25:&lt;/strong&gt; I replaced the optimized code with a slightly more idiomatic version.
I did this because I was guilt-tripped by a comment on Reddit saying that Haskell looked &amp;#8220;ugly.&amp;#8221; This code is, in fact, pretty ugly as far as Haskell goes.  (Tip: Don&amp;#8217;t judge a language based on a single sample, especially if it&amp;#8217;s this one. If you want to see more beautiful code, &lt;a href="http://community.moertel.com/~thor/acm-contest-1996/"&gt;my  Haskell solutions&lt;/a&gt; to &lt;a href="http://www.ntnu.edu.tw/acm/ProblemSetArchive/A_FINALS/1996/Finals96html/"&gt;the 1996 &lt;span class="caps"&gt;ACM&lt;/span&gt; International Collegiate Programming Contest&lt;/a&gt; are much less offensive.)&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Update 2006-11-04:&lt;/strong&gt; Colorized the Haskell snippets.&lt;/p&gt;


&lt;/div&gt;</description>
      <pubDate>Thu, 23 Mar 2006 16:51:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ce0388053c2b36cae46a6675af744f40</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/03/23/the-perfect-shuffles-puzzle-solved-in-haskell</link>
      <category>programming</category>
      <category>haskell</category>
      <category>fun stuff</category>
      <category>haskell</category>
      <category>puzzles</category>
      <category>shuffles</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/60</trackback:ping>
    </item>
    <item>
      <title>Perl Mongers meet at PAPA-8 pinball tournament kickoff!</title>
      <description>&lt;p&gt;Tonight&amp;#8217;s meeting of the &lt;a href="http://pgh.pm.org/"&gt;Pittsburgh Perl Mongers&lt;/a&gt;
was held at the World Headquarters of the &lt;a href="http://papa.org/"&gt;Professional Amateur
Pinball Association&lt;/a&gt; to coincide with the 
&lt;a href="http://papa.org/papa8/"&gt;&lt;span class="caps"&gt;PAPA 8&lt;/span&gt; World Pinball Championships&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;To say it was a cool meeting doesn&amp;#8217;t do it justice. Not only did we
hear a fun talk on writing &lt;span class="caps"&gt;CGI&lt;/span&gt;-contained Mason applications by our own
&lt;a href="http://www.dwright.org/me/me.html"&gt;Dan Wright&lt;/a&gt;, but after the meeting
we wandered around and
&lt;em&gt;played fabulous, well-maintained vintage pinball machines and fine 1980s-era video games&lt;/em&gt;.
And we watched the world&amp;#8217;s best pinball players compete.
And we ate junk food. Yeah!&lt;/p&gt;


	&lt;p&gt;By all means, do look at &lt;a href="http://community.moertel.com/~thor/papa8/"&gt;the &lt;span class="caps"&gt;PAPA 8&lt;/span&gt; photos&lt;/a&gt;. And if you need the love
that only good pinball can provide, experience &lt;span class="caps"&gt;PAPA 8&lt;/span&gt; for yourself:
the tournament runs for the next few days.&lt;/p&gt;


	&lt;p&gt;A big thanks to &lt;span class="caps"&gt;PAPA&lt;/span&gt; for hosting us!&lt;/p&gt;


	&lt;p&gt;&amp;#8212;Tom&lt;/p&gt;</description>
      <pubDate>Fri, 12 Aug 2005 12:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:2ece269de30b705a4eafa8541256eca2</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/08/12/perl-mongers-meet-at-papa-8-pinball-tournament-kickoff</link>
      <category>perl</category>
      <category>fun stuff</category>
      <category>pittsburgh</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/48</trackback:ping>
    </item>
    <item>
      <title>Math makes tea taste better</title>
      <description>&lt;p&gt;I enjoy tea, especially green varieties, which taste best when infused
at less-than-boiling temperatures. The problem is that my electric
water kettle can reach only one temperature reliably: a full-boiling
212 degF. To infuse my tea, then, I have devised a simple, reliable
way of heating water to other temperatures.&lt;/p&gt;


	&lt;p&gt;I start by boiling three cups of water in the kettle. (I know that I
need at least that much to warm my cup and infuse my tea.) Then I cool
the boiling water to the desired temperature by adding just the right
amount of tap water.&lt;/p&gt;


	&lt;p&gt;The trick, of course, is determining the right amount of tap water to
add. In my house the tap water is about 80 degF. Thus to infuse at a
green-tea-friendly 180 degF, I must solve the following equation:&lt;/p&gt;


&lt;div style="text-align: center; margin: 1ex;"&gt;
3 cups · 212 degF + &lt;em&gt;x&lt;/em&gt; cups · 80 degF
= (3 + &lt;em&gt;x&lt;/em&gt;) cups · 180 degF
&lt;/div&gt;

	&lt;p&gt;Solving, I get &lt;em&gt;x&lt;/em&gt; = 0.96, and so I draw just shy of a cup from the tap.&lt;/p&gt;


	&lt;p&gt;This boil-and-cool method is easy and effective. And it costs less
than buying a temperature-adjustable water kettle, which would
probably be inaccurate anyway.&lt;/p&gt;


	&lt;p&gt;Just one more example of how math makes life better.&lt;/p&gt;</description>
      <pubDate>Mon, 11 Jul 2005 12:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:3d4c32b7386334cc45eea9a0975a73ee</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/07/11/math-makes-tea-taste-better</link>
      <category>fun stuff</category>
      <category>math</category>
      <category>food</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/56</trackback:ping>
    </item>
    <item>
      <title>Where have the IEEE t-shirts gone?</title>
      <description>&lt;p&gt;The &lt;a href="http://www.ieee.org/"&gt;&lt;span class="caps"&gt;IEEE&lt;/span&gt;&lt;/a&gt; can always use a little grass-roots
advertising, and so I often wear my &lt;span class="caps"&gt;IEEE&lt;/span&gt; shirts when hanging out with
other technical folk. This is a great way to support the &lt;span class="caps"&gt;IEEE&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Upon seeing my shirts, for example, people sometimes ask me what the
&lt;span class="caps"&gt;IEEE&lt;/span&gt; logo means. I take these opportunities to explain that the logo
symbolizes the electrical engineer&amp;#8217;s &amp;#8220;right-hand rule,&amp;#8221; relating
electrical and magnetic fields. When my listeners invariably lose
consciousness from boredom, I take their wallets and mail the money
therein to the &lt;span class="caps"&gt;IEEE&lt;/span&gt;. Like I said, it&amp;#8217;s a great way to support the home
team.&lt;/p&gt;


	&lt;p&gt;The problem is, my shirts are getting a bit old, and it&amp;#8217;s time to
replace them. So I went to the &lt;span class="caps"&gt;IEEE&lt;/span&gt; web site to order a new batch.&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;And they don&amp;#8217;t sell t-shirts anymore.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;A quick call to &lt;span class="caps"&gt;IEEE&lt;/span&gt; headquarters confirmed that it&amp;#8217;s been this way since 2002.&lt;/p&gt;


	&lt;p&gt;A &lt;a href="http://www.google.com/search?hl=en&amp;#38;q=ieee+t-shirts"&gt;Google search on &lt;em&gt;&lt;span class="caps"&gt;IEEE&lt;/span&gt; t-shirts&lt;/em&gt;&lt;/a&gt; shows
that many &lt;span class="caps"&gt;IEEE&lt;/span&gt; student chapters still sell t-shirts to raise
funds. Unfortunately, the shirts often lack the simple, professional
style that I seek. &lt;a href="http://www.engr.psu.edu/ee/ieee/default4-11.stm"&gt;The Penn State
chapter&lt;/a&gt;, for
instance, sold t-shirts boldly proclaiming the practice of &amp;#8220;Coed Naked
Electrical Engineering: Do it with frequency &amp;#8217;till it hertz.&amp;#8221; While
I&amp;#8217;m sure it works wonders with the ladies, it&amp;#8217;s just not my
style.&lt;/p&gt;


	&lt;p&gt;Anyway, if you know where to get high quality, old-style &lt;span class="caps"&gt;IEEE&lt;/span&gt;
t-shirts, please let me know.&lt;/p&gt;</description>
      <pubDate>Fri, 10 Jun 2005 12:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:3181eb39806759f8b8fdbb35f351d6fb</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/06/10/where-have-the-ieee-t-shirts-gone</link>
      <category>fun stuff</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/45</trackback:ping>
    </item>
  </channel>
</rss>
