Feb 00 Challenge

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

# Programmer's Challenge

by Bob Boonstra, Westford, MA

## Web apps with Lasso and FileMaker Pro

### Latin Squares

A Latin Square of order N is an NxN array of numbers, where each number from 0..N-1 occurs exactly once in each row and in each column. Your Challenge this month is to construct Latin Squares that are minimal, in the sense that each must form the smallest NxN digit base N number, where the digits are read off column by column, one row at a time.

The prototype for the code you should write is:

```#if defined(__cplusplus)
extern "C" {
#endif

void LatinSquares(
short n,	/* dimension of the Latin Square to be generated */
short *latinSquare	/* set latinSquare[c + r*n] to square value row r, col c */
);

#if defined(__cplusplus)
}
#endif

For example,

1234
2341
4123
3412
```

is a Latin Square with the value 1234234141233412. It is not the minimal Latin Square of order 4, however, since the following Latin Square,

```				1234
2143
3412
4321
```

forms the number 1234214334124321.

The dimension of the squares you must generate will be less than 2**16. The winner will be the solution that correctly computes minimal Latin Squares in the least amount of execution time. Code size and code elegance, in that order, will be the tiebreakers, in the event two solutions are within 1% of one another in execution time. All Latin Squares must be computed at execution time - entries that precompute solutions will be disqualified.

This month's Challenge was suggested by Aaron Montgomery, who earns 2 Challenge points for making the suggestion

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 Tom Saxton for taking first place in the November, 1999, Putting Green Challenge. While I'm not sure that the next Ryder Cup teams will be beating a path to his door for his four-putting green skills, Tom did soundly beat the three other entries.

The Putting Green Challenge required contestants to analyze a sequence of greens described as three-dimensional points connected into adjoining triangles. Each green had a fixed pin position, and several holes were played on each green from varying initial ball positions. Complicating the Challenger's job was the fact that the propagation model was unknown, available only by observing the results of a putt. For each green, the contestants were given several practice holes that could be used to puzzle out the propagation model. Scoring was based on an award of 100 points for each hole successfully completed, minus 10 points for each stroke taken (not counting practice holes), and minus 10 points for each second of execution time (including practice time).

Two of the solutions submitted did not manage to sink a putt for any of the holes played in my test match. One of those solutions simply hit the ball at the hole very hard, trying to take advantage of a possible ambiguity about ball velocity in the problem statement. I heard from enough real golfers who had rimmed the cup a few times that I had to close the velocity loophole. Tom's winning solution is fairly straightforward. He takes 10 practice shots on the first hole of the first green to estimate the drag coefficients. It does not attempt to analyze the contours of the green or to model the effect of gravity. But the drag model is good enough to put the ball in the hole with only one putt on a level green, and with one or two putts on a flat but gently sloping green.

I used three greens in my test cases, a perfectly flat and level green, a flat but gently sloping green, and a green with more varied terrain. Five practice holes were played on each green, five scored holes on each of the first two greens, and 20 holes on the final green. On the interesting terrain of the last hole, Tom's solution completed each hole successfully, taking up to seven putts to complete the hole.

The table below lists, for each of the solutions submitted, the number of holes successfully completed, the total number of strokes taken on nonpractice holes, total execution time, the total score, and the size and language code parameters. 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.

 Name Holes Strokes Time Score Code Data Lang Tom Saxton (138) 30 117 3.148 1829.969 2224 432 C Brady Duga 10 49 2.435 509.976 1660 380 C R. S. 0 60 0.42 -600.004 568 93 C++ J. T. 0 210 197.47 -2101.975 2580 255 C

### 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.

 Rank Name Points 1. Munter, Ernst 237 2. Saxton, Tom 146 3. Maurer, Sebastian 67 4. Rieken, Willeke 51 5. Heithcock, JG 43 6. Shearer, Rob 41 7. Boring, Randy 39 8. Brown, Pat 20 9. Hostetter, Mat 20 10. Jones, Dennis 12 11. Hart, Alan 11 12. Duga, Brady 10 13. Hewett, Kevin 10 14. Murphy, ACC 10 14. Selengut, Jared 10 16. Smith, Brad 10 17. Strout, Joe 10 18. Varilly, Patrick 10

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 place 20 points 2nd place 10 points 3rd place 7 points 4th place 4 points 5th place 2 points finding bug 2 points suggesting Challenge 2 points

Here is Tom's winning Putting Green solution:

```PuttingGreen.c

#include "PuttingGreen.h"

#include <stdlib.h>
#include <math.h>

enum
{
fFalse = 0,
fTrue = TRUE
};
#define DIM(a) (sizeof(a)/sizeof((a)[0]))

// disable asserts
#define Assert(f)

// vector types and constants
typedef struct Point3DDouble VEC;
static const double epsilon = 1.0e-7;
static const VEC s_normalZ = { 0, 0, 1.0 };
static const VEC s_vecZero;

// this green

static Point3DDouble *s_paposGrid;
static int s_cposGrid;
static MyTriangle *s_patri;
static int s_ctri;
static int s_itriPin;
static Point3DDouble s_posPin;

static int s_cholePractice;			// number of practice putts
static int s_choleScored;				// number of scored putts

// computed physical constants for this green
static int s_fGetDrag = fTrue;
static double mDrag, bDrag;

// current state

static int s_ihole;						// which hole are we on?
static int s_cputt;						// which putt is this
static int s_fPractice;				// is this a practice putt?
static Point3DDouble s_posCur;	// where's the ball?

// the current putt

static double s_svInitial;
static double s_distExpect;

// vector utilities

static void _SubVec(VEC pos1, VEC pos2, VEC *pposResult);
static double _DotVec(VEC vec1, VEC vec2);
static double _LenVec(VEC vec);
static void _ScaleVec(VEC vec, double s, VEC *pvecResult);

// triangle utilities

static void _GetMidTriPos(int itri, Point3DDouble *ppos);

//
// Entry Points
//
#if 0
static void ___Entry_Points(){}
#endif

InitGreen

void InitGreen(
Point3DDouble paposGrid[],	/* green terrain description */
long cposGrid,	/* number of points */
MyTriangle patri[],	/* triangles comprising the green */
int ctri,	/* number of triangles */
long itriPin,	/* index in triangles[] of the pin on this green */
long cholePractice,	/* number of unscored (but timed) holes to 			   practice on this green */
long choleScored	/* number of holes to be scored on this green */
)

{
s_paposGrid = paposGrid;
s_cposGrid = cposGrid;
s_patri = patri;
s_ctri = ctri;
s_itriPin = itriPin;

s_cholePractice = cholePractice;
s_choleScored = choleScored;

_GetMidTriPos(itriPin, &s_posPin);

s_ihole = 0;
}

#pragma warn_unusedarg off

StartHole

void StartHole(	/* called to start play on this hole */
Point3DDouble posStart, 	/* initial ball position on the green */
Boolean fPractice	/* TRUE if this hole is practice */
)
{
++s_ihole;

s_posCur = posStart;
s_cputt = 0;
s_fPractice = fPractice;
}

#pragma warn_unusedarg reset

#define cputtPractice 10
#define cputtScore    10

typedef struct SD SD;
struct SD
{
double speed;
double dist;
};
static SD s_asd[cputtPractice];
static int s_csd;

MakePutt

Boolean 		/* quit */ MakePutt(
Velocity2DDouble *pvXY		/* return initial ball velocity in the z==0 plane */
)
{
double dist;
Point3DDouble vXY;

if (s_cputt >= (s_fPractice ? cputtPractice : cputtScore))
{
if (s_fGetDrag)
{
int isd;
double sumX, sumY, sumXY, sumXX;

Assert(s_csd > 0);

// do linear regression on the data

sumX = sumY = sumXY = sumXX = 0;
for (isd = 0; isd < s_csd; ++isd)
{
SD *psd = &s_asd[isd];
double y = psd->dist;
double x = fabs(psd->speed);

sumXX += x * x;
sumXY += x * y;
sumX += x;
sumY += y;
}

mDrag = (sumX*sumY - s_csd*sumXY)/(sumX*sumX - s_csd*sumXX);
bDrag = (sumY - mDrag*sumX)/s_csd;

Assert(0.0 <= mDrag && mDrag <= 5.0);
Assert(-5.0 <= bDrag && bDrag <= 5.0);

s_fGetDrag = fFalse;
}

// give up

return fTrue;
}

// just give up after we've finished with practice shots

if (s_fPractice && !s_fGetDrag)
return fTrue;

_SubVec(s_posPin, s_posCur, &vXY);
vXY.z = 0;
dist = _LenVec(vXY);

if (s_fGetDrag)
{
s_svInitial = 3.0*(1.0 + s_cputt/2);
if (s_cputt % 2)
s_svInitial = -s_svInitial;
}
else
{
s_svInitial = (dist + 0.03 - bDrag) / mDrag;
s_distExpect = dist;
}
_ScaleVec(vXY, s_svInitial/dist, &vXY);

pvXY->x = vXY.x;
pvXY->y = vXY.y;

return fFalse; // keep going
}

BallMovement

void BallMovement(
BallPosition papos4[],	/* provides ball movement in response to MakePutt */
int cpos,	/* number of ball positions provided */
Boolean fInHole	/* true if the ball went into the hole */
)
{
Point3DDouble posNew;

posNew = papos4[cpos-1].pt;

if (!fInHole)
{
double dist;
Point3DDouble vecMove;

_SubVec(posNew, s_posCur, &vecMove);
dist = _LenVec(vecMove);

if (s_fGetDrag)
{
Assert(0 <= s_csd && s_csd < DIM(s_asd));
s_asd[s_csd].speed = fabs(s_svInitial);
s_asd[s_csd].dist = dist;
++s_csd;
}
}

s_posCur = posNew;

++s_cputt;
}

//
// Vector Functions
//

#if 0
static void ___Vector_Functions(){}
#endif

_SubVec

static void _SubVec(VEC pos1, VEC pos2, VEC *pposResult)
{
pposResult->x = pos1.x - pos2.x;
pposResult->y = pos1.y - pos2.y;
pposResult->z = pos1.z - pos2.z;
}

_DotVec

static double _DotVec(VEC vec1, VEC vec2)
{
return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
}

_LenVec

static double _LenVec(VEC vec)
{
return sqrt(_DotVec(vec, vec));
}
_ScaleVec

static void _ScaleVec(VEC vec, double s, VEC *pvecResult)
{
pvecResult->x = s * vec.x;
pvecResult->y = s * vec.y;
pvecResult->z = s * vec.z;

}

_GetMidTriPos

static void _GetMidTriPos(int itri, Point3DDouble *pposMid)
{
MyTriangle tri;
int iipos;

Assert(0 <= itri && itri < s_ctri);
tri = s_patri[itri];

*pposMid = s_vecZero;

for (iipos = 0; iipos < DIM(tri.pointIndices); ++iipos)
{
Point3DDouble *ppos;

Assert(0 <= tri.pointIndices[iipos] &&
tri.pointIndices[iipos] < s_cposGrid);
ppos = &s_paposGrid[tri.pointIndices[iipos]];

pposMid->x += ppos->x;
pposMid->y += ppos->y;
pposMid->z += ppos->z;
}

pposMid->x *= 1.0/3.0;
pposMid->y *= 1.0/3.0;
pposMid->z *= 1.0/3.0;
}
```

Community Search:
MacTech Search:

Macs Fan Control 1.5.14 - Monitor and co...
Macs Fan Control allows you to monitor and control almost any aspect of your computer's fans, with support for controlling fan speed, temperature sensors pane, menu-bar icon, and autostart with... Read more
VueScan 9.7.96 - Scanner software with a...
VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more
FileMaker Pro 19.6.1 - Quickly build cus...
FileMaker Pro is the tool you use to create a custom app. You also use FileMaker Pro to access your app on a computer. Start by importing data from a spreadsheet or using a built-in Starter app to... Read more
Duet is the first app that allows you to use your iDevice as an extra display for your Mac using the Lightning or 30-pin cable. Note: This app requires a iOS companion app. Release notes were... Read more
Firefox 107.0.1 - Fast, safe Web browser...
Firefox offers a fast, safe Web browsing experience. Browse quickly, securely, and effortlessly. With its industry-leading features, Firefox is the choice of Web development professionals and casual... Read more
War Thunder 2.21.1.91 - Multiplayer war...
In War Thunder, aircraft, attack helicopters, ground forces and naval ships collaborate in realistic competitive battles. You can choose from over 1,500 vehicles and an extensive variety of combat... Read more
With Apple Numbers, sophisticated spreadsheets are just the start. The whole sheet is your canvas. Just add dramatic interactive charts, tables, and images that paint a revealing picture of your data... Read more
DEVONthink Pro 3.8.7 - Knowledge base, i...
DEVONthink is DEVONtechnologies' document and information management solution. It supports a large variety of file formats and stores them in a database enhanced by artificial intelligence (AI). Many... Read more
Drive Genius 6.2.3 - \$79.00
Drive Genius features a comprehensive Malware Scan. Automate your malware protection. Protect your investment from any threat. The Malware Scan is part of the automated DrivePulse utility. DrivePulse... Read more
VLC Media Player 3.0.18 - Popular multim...
VLC Media Player is a highly portable multimedia player for various audio and video formats (MPEG-1, MPEG-2, MPEG-4, DivX, MP3, OGG, ...) as well as DVDs, VCDs, and various streaming protocols. It... Read more

## Latest Forum Discussions

‘Genshin Impact’ Version 3.3 Pre-Install...
Following the reveal of the release date and more for Genshin Impact (Free) version 3.3 ‘All Senses Clear, All Existence Void’, HoYoverse showcased the Genius Invokation TCG that arrives this week in the update. | Read more »
TouchArcade Game of the Week: ‘Sling Min...
The world of PC games has always blown my mind because there’s just SO MUCH stuff out there that it’s not uncommon at all for there to be a game that’s well-liked and well-reviewed, and seemingly quite popular with a solid fanbase, and have it be... | Read more »
Hello gentle readers, and welcome to the SwitchArcade Round-Up for December 2nd, 2022. So, today turned out a little quieter than the usual Friday. It was so quiet, in fact, that I decided to pen a few reviews. The Knight Witch, Railbound, and Donut... | Read more »
Blue Archive reveals its latest event st...
Nexon has announced the new update for Blue Archive, under the name of An Unconcealed Heart. Featuring a battle between two academies, the story will follow a group struggling to gain recognition, and will bring three new students to recruit. [... | Read more »
Following the major update for Dead Cells on iOS and Android a few days ago, Playdigious has brought Dead Cells+ () to Apple Arcade. As an App Store Great, Dead Cells+ includes all prior paid DLC and content updates. It also has exclusive mobile... | Read more »
Hello gentle readers, and welcome to the SwitchArcade Round-Up for December 1st, 2022. Wow, December. We’re already at the last month of the year? Phew. I have a lot of work to finish in the next few weeks. As for today, we’ve got a little news, a... | Read more »
One of our favorite puzzlers released this year is Railbound from Afterburn Games, which hit in early September and earned our Game of the Week recognition for being an absolutely ace logic puzzler. The goal is to place rail pieces down in order to... | Read more »
The Seven Deadly Sins: Grand Cross celeb...
Netmarble Corporation has pulled out all the stops to celebrate the 3 and a half year anniversary of The Seven Deadly Sins: Grand Cross. The Grand Cross 3.5th Year Anniversary the Ultimate One, a rather wordy title, brings with it a brand new... | Read more »
‘Skullgirls Mobile’ Major Update 5.2 Out...
Developer Hidden Variable pushed out a major update for Skullgirls Mobile (Free) a few hours ago. The version 5.2 update brings in Black Dahlia (before the console and PC game), Retakes, XP Treats, free gifts, and more. Since launch, Skullgirls... | Read more »
Out Now: ‘Disgaea 4’, ‘Romancing SaGa: M...
Each and every day new mobile games are hitting the App Store, and so each week we put together a big old list of all the best new releases of the past seven days. Back in the day the App Store would showcase the same games for a week, and then... | Read more »

## Price Scanner via MacPrices.net

Holiday Sale: Apple AirPods Pro for only \$199...
Amazon has new 2022 AirPods Pro in stock and on sale for \$199.99 shipped as part of their Holiday sale. Their price is \$50 off Apple’s MSRP, equaling their Black Friday price, and it’s the lowest... Read more
New Holiday Sale: Apple retailers are offerin...
Several Apple retailers lowered prices on 10.9″ iPad Airs overnight to lows of \$100 off MSRP starting at \$499. Their prices are the lowest available for iPad Airs anywhere this Holiday season right... Read more
New Holiday sale at Amazon: Take \$50 off Appl...
Amazon has Apple’s new 10th-generation iPads in stock and on sale, for the first time, for \$50 off MSRP starting at only \$399. Their discount applies to all models and all colors. With the discount,... Read more
Holiday Sale: Get an 8.3″ Apple iPad mini for...
Sams Club has 10.9″ 64GB iPad minis on Holiday sale for \$80-\$100 off MSRP through December 7, 2022. With their discount, prices start at \$399 — the cheapest price for a new iPad mini from any of the... Read more
Sams Club Holiday December Event sale: Apple...
Apple AirPods Max headphones are on sale at Sams Club for \$110 off MSRP (\$439) as part of their December Event sale, ending on December 7, 2022, valid for all colors. Sale price for online orders... Read more
Apple’s 10.2″ 64GB 9th-generation iPads are o...
Sams Club has 9th-generation 64GB iPads on Holiday sale for \$60 off MSRP through December 7, 2022. With their discount, prices start at \$259 — the cheapest price for a new iPad from any of the Apple... Read more
11″ 128GB WiFi M2 iPad Pro on sale for \$749,...
B&H Photo has the new 11″ 128GB WiFi M2-powered iPad Pro (in Space Gray or Silver) on Holiday sale for \$749 including free 1-2 day shipping to most US addresses. Their price is \$50 off MSRP and... Read more
Find the best Holiday sale price on an iPad u...
Apple retailers are offering \$100-\$150 Holida...
Apple retailers have posted their most-recent Holiday sale prices on 13″ MacBook Airs. Take up to \$150 off MSRP on M2-powered Airs with these sales with prices starting at only \$1099. Free shipping... Read more
Holiday Sale: Apple’s 14″ MacBook Pros with M...
B&H Photo is offering \$200-\$300 discounts on Apple’s 14″ MacBook Pros with M1 Pro CPUs as part of their Holiday 2022 sale, with prices starting at \$1799. Free 1-2 day shipping is available to... Read more

## Jobs Board

Support Technician II - *Apple* Support - O...
…problems and acting as a liaison between customers and resolving groups. As an Apple Technical Specialist, you will be supporting many of our popular Apple Read more
*Apple* Electronic Repair Technician - PlanI...
…a highly motivated individual to join our Production Department as an Apple Electronic Repair Technician. The computer repair technician will diagnose, assemble, Read more
Lead Developer - *Apple* tvOS - Rumble (Uni...
…earnings, and positive sentiment About the role: We are looking for a Lead Apple tvOS Developer to join our application engineering team to expand our video centric Read more
Tier 1 Endpoint Engineer - *Apple* - Red Ri...
…Desk on site, at our Client's location, with a focus on support to Apple products. This position will handle technical support requests directly from customers and Read more
Product Manager II - *Apple* - DISH (United...
…you will be doing We seek an ambitious, data-driven thinker to assist the Apple Product Development team as our new Retail Wireless division continues to grow and Read more