TweetFollow Us on Twitter

Apr 00 Challenge

Volume Number: 16 (2000)
Issue Number: 4
Column Tag: Programmer's Challenge

Programmer's Challenge

by Bob Boonstra, Westford, MA

Text Compression

This month, we've got some very important messages to send. But we're not living in a world of free bandwidth. In fact, bandwidth in our Challenge world is very expensive, so expensive that we're asking you to compress our messages and save us a few bytes.

The prototype for the code you should write is:

void * /* yourStorage */ InitCompression(void);

long /* compressedLength */ CompressText(
  char *inputText,        /* text to be compressed */
  long numInputChars,     /* length of inputText in bytes */
  char *compressedText,   /* return compressedText here */
  const void *yourStorage /* storage returned by InitCompression */
);

long /* expandedLength */ ExpandText(
  char *compressedText,   /* encoded text to be expanded */
  long compressedLength,  /* length of encoded text in bytes */
  char *expandedText,     /* return expanded text here */
  const void *yourStorage /* storage returned by InitCompression */
);

void TermCompression(
  void *yourStorage       /* storage returned by InitCompression */
);

For this Challenge, you need to provide the four routines indicated above. Your InitCompression and TermCompression routines will be called only once each, at the beginning of the test and at the end of the test, respectively. InitCompression should allocate and return a block of yourStorage where you initialize any information needed by your compression and expansion routines. That storage will be passed back to you unchanged each time you are asked to compress or decompress text. TermCompression will be called at the end of the test and should deallocate the block of yourStorage to avoid a memory leak.

In between the calls to InitCompression and TermCompression, the test code will make multiple calls to CompressText and ExpandText with different inputText values. CompressText should process the inputText, populate the compressedText, and return the number of bytes in the result. ExpandText does the opposite, processing the compressedText, converting it to expandedText, and returning the number of bytes of the original text. Multiple CompressText and ExpandText calls will occur with varying inputText and compressedText, in any order, with the obvious constraint that text must be encoded before it can be decoded.

The inputText may contain any character between 0x00 and 0x7F, inclusive. As a practical matter, the inputText will be drawn from paragraphs of English-language text, computer programs in C, C++, and Pascal, and html pages.

All text-specific information needed to decode the compressedText must be stored in the compressedText itself. Any text-independent decoding information may be stored in yourStorage or in static storage within your program. No text-specific encoding information may be stored in yourStorage or in static variables.

The winner will be the solution that correctly compresses the inputText into the least costly compressedText, where cost is a function of length and execution time. Specifically, each inputText will have a cost equal to theCompressedLength of the corresponding compressedText, plus a penalty of 10% for each 100 milliseconds required to do the encoding and decoding.

This will be a native PowerPC Challenge, using the CodeWarrior Pro 5 environment. Solutions may be coded in C, C++, or Pascal. Solutions in Java will also be accepted, but Java entries must be accompanied by a test driver that uses the interface provided in the problem statement.

Three Months Ago Winner

Congratulations to Sebastian Maurer for winning the January, 2000, Triangle Peg Challenge. The Peg Challenge required entries to solve variously sized games of peg solitaire, a game where pegs are arranged in holes on a triangle board. The objective of the game is to repeatedly jump one peg over an adjacent one, removing the jumped peg each time, with the intent of removing as many pegs as possible. In our Challenge, scoring counted 1000 penalty points for each peg left on the board, plus one penalty point for each millisecond of execution time. Sebastian did not submit the fastest entry - in fact, he ranked third in speed - but it played the Peg game significantly better than the other solutions, resulting in a better overall score.

I evaluated the Pegs entries using 14 test cases, ranging from 5 pegs to 100 pegs on a side. Nine of the test cases were missing only one peg, one of the large puzzles was missing 50 pegs, with the remainder missing a small number of pegs. Sebastian's entry left fewer pegs on the board than the second place solution in 12 of 14 test cases, and fewer than the third place solution in 10 of 14 cases. Sebastian's entry won in all of the test cases involving larger puzzles.

Sebastian's code is rather sparsely commented. The work is done in his Search routine, which iteratively tries valid moves, backtracks when it cannot find a valid move, and saves the best solution found so far. In looking at the code for the top entries, it wasn't obvious why Sebastian's solution did so much better than the others. The entries did use different logic to prune the search and trade execution time against search depth; perhaps those differences explain the performance variation.

The table below lists, for each of the solutions submitted, the overall score, the execution time in microseconds, and the total number of pegs left on the board for all of our test cases. It also indicates the code size, data size, and programming language used by each solution. Two entries did not complete all of the test cases and are listed last. As usual, the number in parentheses after the entrant's name is the total number of Challenge points earned in all Challenges prior to this one.

NameScoreTime (µsecs)Pegs LeftCode SizeData SizeLang
Sebastian Maurer (77)20224644646419764852162C
Andrew Downs (2)3623988109883613187220C
Willeke Rieken (61)3727672146723713302056C++
Randy Boring (112)11211715476714107357308132096C++
M. L.N/AN/A2648218C
J. C.N/AN/A108281021C++

Top Contestants

Listed here are the Top Contestants for the Programmer's Challenge, including everyone who has accumulated 10 or more points during the past two years. The numbers below include points awarded over the 24 most recent contests, including points earned by this month's entrants.

RankNamePoints
1.Munter, Ernst227
2.Saxton, Tom139
3.Maurer, Sebastian87
4.Rieken, Willeke48
5.Boring, Randy43
6.Heithcock, JG43
7.Shearer, Rob43
8.Taylor, Jonathan24
9.Brown, Pat20
10.Downs, Andrew12
11.Jones, Dennis12
12.Hart, Alan11
13.Duga, Brady10
14.Hewett, Kevin10
15.Murphy, ACC10
16.Selengut, Jared10
17.Strout, Joe10
18.Varilly, Patrick10

There are three ways to earn points: (1) scoring in the top 5 of any Challenge, (2) being the first person to find a bug in a published winning solution or, (3) being the first person to suggest a Challenge that I use. The points you can win are:

1st place20 points
2nd place10 points
3rd place7 points
4th place4 points
5th place2 points
finding bug2 points
suggesting Challenge2 points

Here is Sebastian's winning Triangle Peg solution:

Pegs.c
Copyright © 2000 Sebastian Maurer

#include "Pegs.h"
#include "Memory.h"
#include "Timer.h"

#include <stdio.h>

const int kNumTries = 1000;
const UInt32 kStopTime = 500000;
  // in microseconds

const int kNumDirections = 6;
enum {kIllegal = 0, kFull, kEmpty};

char** gGrid;
PegJump *gCurrentJumps;
PegJump *gBestJumps;
int gBestScore;
UnsignedWide gLastImprovement;
unsigned short *gDirection;
short gDeltaRow[kNumDirections];
short gDeltaCol[kNumDirections];

static UInt32 WideDiff(UnsignedWide m1, UnsignedWide m2)
{
  UnsignedWide m;
  m.lo = m2.lo - m1.lo;
  m.hi = m2.hi - m1.hi;
  if (m1.lo > m2.lo)
    m.hi -= 1;
  return m.lo;
}

static void PrintBoard(int triangleSize) {
  for(int r = 0; r < triangleSize; r++) {
    for(int c = - triangleSize; c <= triangleSize; c++)
    {
      switch (gGrid[r][c]) {
        case kIllegal: printf(" "); break;
        case kFull: printf("X"); break;
        case kEmpty: printf("."); break;
        default: printf("?");
      }
    }
    printf("\n");
  }
  printf("\n");
}

AllocateGrid
//////
// Memory allocation for the 2D grid
static char **AllocateGrid(
  int xMin, int xMax,
  int yMin, int yMax)
{
  int i, j;
  int nx = xMax - xMin + 1;
  int ny = yMax - yMin + 1;
  char **array;
  Ptr p;
  int rowSize;

  array = (char **) NewPtr((Size)(nx * sizeof(char*)));
  if (array == 0)
    return 0;
  
  array -= xMin;
  p = NewPtr((Size)(nx * ny * sizeof(char)));
  if (p == 0)
    return 0;
  p -= yMin * sizeof(char);
  rowSize = (int)(ny * sizeof(char));
  for(i = xMin; i <= xMax; i++) {
    array[i] = (char*) p;
    p += rowSize;
  }

  for(i = xMin; i <= xMax; i++)
    for(j = yMin; j <= yMax; j++)
      array[i][j] = 0;

  return array;
}

DeallocateGrid
static void DeallocateGrid(
  char **array, long xMin, long yMin)
{
  DisposePtr((Ptr) (array[xMin] + yMin));
  DisposePtr((Ptr) (array + xMin));
}

FillGrid
//////
// Fill the grid with the initial configuration
static void FillGrid(
  short size,
  short numInitialPegs,
  TrianglePegPosition initialPegPositions[]
) {
  for(int r = -2; r < size + 2; r++)
    for(int c = - size - 2; c <= size + 2; c++)
      gGrid[r][c] = kIllegal;

  for(int r = 0; r < size; r++)
    for(int c = - r; c <= r; c += 2)
      gGrid[r][c] = kEmpty;
  
  for(int p = 0; p < numInitialPegs; p++) {
    int row = initialPegPositions[p].row;
    int col = initialPegPositions[p].col;
    gGrid[row][col] = kFull;
  }
}

SaveSolution
//////
// Store the current best solution
static void SaveSolution(int numMoves) {
  gBestScore = numMoves;

  for(int m = 0; m < numMoves; m++) {
    gBestJumps[m].from.row =
      gCurrentJumps[m].from.row;
    gBestJumps[m].from.col =
      gCurrentJumps[m].from.col;
    gBestJumps[m].to.row =
      gCurrentJumps[m].to.row;
    gBestJumps[m].to.col =
      gCurrentJumps[m].to.col;
  }
    
}

Try
//////
// Perform a move if it is legal, and return true
// If the move is illegal, return false
// I assume position (r,c) already contains a peg
static bool Try(int move, int r, int c, int dir)
{
  int dr = gDeltaRow[dir];
  int dc = gDeltaCol[dir];

  if ((gGrid[r + dr][c + dc] == kFull) &&
    (gGrid[r + 2 * dr][c + 2 * dc] == kEmpty))
  {
    gDirection[move] = dir;
      
    gCurrentJumps[move].from.row = r;
    gCurrentJumps[move].from.col = c;
    
    gCurrentJumps[move].to.row = r + 2 * dr;
    gCurrentJumps[move].to.col = c + 2 * dc;
    
    gGrid[r][c] = kEmpty;
    gGrid[r + dr][c + dc] = kEmpty;
    gGrid[r + 2 * dr][c + 2 * dc] = kFull;

    return true;
  }
  else
    return false;
}

UndoMove
//////
// Undo a move by restoring the grid
static inline void UndoMove(int row, int col, int dir) {
  int dr = gDeltaRow[dir];
  int dc = gDeltaCol[dir];

  gGrid[row][col] = kFull;
  gGrid[row + dr][col + dc] = kFull;
  gGrid[row + 2 * dr][col + 2 * dc] = kEmpty;
}

Search
//////
// This is where the search is done
static void Search(
  int numInitialPegs,
  int triangleSize
) {

  UnsignedWide now;
  int move, row, col, dir;
  bool abort = false;
  bool haveASolution = false;
  
  int numTries = 0;

  row = 0;
  col = 0;
  dir = -1;
  move = 0;

  do {
  
    // find the next valid move
    do {
      dir++;
      if (dir >= kNumDirections) {
        dir = 0;
        col++;
        if (col == row + 1) {
          row++;
          col = - row;
        }
      }
    } while ((row < triangleSize) &&
           ((gGrid[row][col] == kEmpty) ||
            !Try(move, row, col, dir)));
  
    if (row < triangleSize) {
      // we just made a valid move
      // start work on the next move
      
      move++;

      row = 0;
      col = 0;
      dir = 0;
    } else { // we reached a dead end
      
      // see if this is a better solution
      if (move > gBestScore) {
        SaveSolution(move);
        haveASolution = true;

        if (move + 1 == numInitialPegs)
          abort = true;

        Microseconds(&now);

        gLastImprovement = now;
      }
      
      // backtrack
      move-;
      
      if (move >= 0) {
        row = gCurrentJumps[move].from.row;
        col = gCurrentJumps[move].from.col;
        dir = gDirection[move];
        UndoMove(row, col, dir);        
      }
      
    }
    
    if (haveASolution) {
      // if we already reached a dead end once,
      // don't waste time searching more
    
      numTries++;
      if (numTries == kNumTries) {
        numTries = 0;
        Microseconds(&now);
        if (WideDiff(gLastImprovement, now) >
            kStopTime)
        {
          abort = true;
        }
      }
    }

  } while (!abort && (move >= 0));
}

SolvePegTriangle
short /* number of moves */ SolvePegTriangle (
  short triangleSize,
    /* number of rows in triangle to solve */
  short numInitialPegs,
    /* number of pegs in starting puzzle position */
  TrianglePegPosition initialPegPositions[],
    /* peg locations in starting puzzle position */
  PegJump pegJumps[]
    /* return peg moves that solve the puzzle here,
       in sequence */

) {
  // prepare, allocate, initialize structures
  gDeltaRow[0] = -1; gDeltaCol[0] = -1;
  gDeltaRow[1] = -1; gDeltaCol[1] = +1;
  gDeltaRow[2] = 0; gDeltaCol[2] = -2;
  gDeltaRow[3] = 0; gDeltaCol[3] = +2;
  gDeltaRow[4] = +1; gDeltaCol[4] = -1;
  gDeltaRow[5] = +1; gDeltaCol[5] = +1;
  
  gGrid =
    AllocateGrid(-2, triangleSize + 2,
      - triangleSize - 2, triangleSize + 2);
  if (gGrid == 0)
    return 0;
  
  gDirection = (unsigned short*)
    NewPtr(numInitialPegs * sizeof(unsigned short));
  if (gDirection == 0)
    return 0;
  
  gCurrentJumps = (PegJump*)
    NewPtr(numInitialPegs * sizeof(PegJump));
  if (gCurrentJumps == 0)
    return 0;

  gBestJumps = pegJumps;

  Microseconds(&gLastImprovement);
  gBestScore = 0;

  FillGrid(triangleSize, numInitialPegs,
       initialPegPositions);

  // do the work
  Search(numInitialPegs, triangleSize);

  // clean up
  DeallocateGrid(gGrid, -2, - triangleSize - 2);
  DisposePtr((Ptr)gDirection);
  DisposePtr((Ptr)gCurrentJumps);

  return gBestScore;
}
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »

Price Scanner via MacPrices.net

You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more

Jobs Board

Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.