Not a too tricky a problem here, took two attempts because I forgot to clear the carry flag in the non-carry case.
Programming Challenges Problems - Easiest to Hardest
After completing the The Stern-Brocot Number System problem the other day, I pondered which problem to solve next.
So I created a Excel spreadsheet to hold all 112 problems, and then sorted it by number of Users Solved to see the most solved puzzles.
Table below:
Number | Name | Users Submitted | Users Solved | % Users | Done
—|—|—|—|—|—|–
110101 | The 3n+1 problem | 9546 | 6059 | 63% | 1
110102 | Minesweeper | 5745 | 3781 | 66% | 1 |
110201 | Jolly Jumpers | 2882 | 2066 | 72% | 1
110103 | The Trip | 3341 | 1644 | 49% | 1
110104 | LC-Display | 2559 | 1592 | 62% | 1
110401 | Vito’s Family | 1584 | 1234 | 78% | 1
110203 | Hartals |1316 | 1189 | 90% |1
110502 | Reverse and Add | 1198 |968 | 81% | 1
110501 | Primary Arithmetic | 1187 | 908 | 76% | 1
110106 | Interpreter | 1253 | 872 | 70% | 1
110107 | Check The Check | 1077 | 739 | 69% | 1
110301 | WERTYU | 1389 | 730 | 53% | 1
110105 | Graphical Editor | 1523 | 704 | 46% | 1
110303 | Common Permutation | 1019 | 692 | 68% | 0
110901 | Bicoloring | 836 | 690 | 83% | 0
110701 | Light. More light | 923 | 667 | 72% | 1
110504 | Ones | 748 | 629 | 84% | 1
110302 | Where’s Waldorf? | 803 | 615 | 77% | 0
110108 | Australian Voting | 947 | 564 | | 60% | 1
110405 | Shoemaker’s Problem | 731 | 564 | 77% | 1
110202 | Poker Hands | 769 | 528 | 69% | 1
110404 | Longest Nap | 649 | 490 | 76% | 1
110402 | Stacks of Flapjacks | 716 | 489 | 68% | 0
110505 | A multiplication game | 595 | 488 | 82% | 0
110205 | Stack ‘em Up | 610 | 455 | 75% | 0
110608 | Steps | 510 | 437 | 86% | 0 |
110207 | Contest Scoreboard | 700 | 417 | 60% | 1
110704 | Factovisors | 690 | 412 | 60% | 0
111001 | Freckles | 554 | 401 | 72% | 0
110601 | How many Fibs? | 545 | 378 | 69% | 0
110603 | Counting | 488 | 378 | 77% | 0
111105 | Cutting Sticks | 463 | 354 | 76% | 0
110304 | Crypt Kicker II | 562 | 350 | 62% | 0
110903 | The Tourist Guide | 473 | 337 | 71% | 1
110801 | Little Bishops | 526 | 332 | 63% | -1
111301 | Dog and Gopher | 424 | 327 | 77% | 0
111201 | Ant on a Chessboard | 352 | 323 | 92% | 0
110607 | Selfdescribing Sequence | 370 | 310 | 84% | 0
110902 | Playing with Wheels | 408 | 301 | 74% | 0
110306 | File Fragmentation | 406 | 284 | 70% | 1
110507 | The Stern-Brocot Number System | 308 | 276 | 90% | 1
110703 | Euclid problem | 365 | 272 | 75% | 0
111103 | Weights and Measures | 328 | 260 | 79% | 0
110204 | Crypt Kicker | 557 | 255 | 46% | 0
110305 | Automated Jude Script | 388 | 247 | 64% | 0
110503 | The Archeologists’s Dilemma | 429 | 215 | 50% | 0
110206 | Erdos Numbers | 508 | 212 | 42% | 1
110506 | Polynomial coefficients | 240 | 210 | 88% | 0
110706 | Smith Numbers | 263 | 199 | 76% | 0
110908 | Hanoi Tower Troubles Again! | 221 | 199 | 90% | 0
111104 | Unidirectional TSP | 342 | 185 | 54% | 0
111204 | Bee Maja | 198 | 172 | 87% | 0
110403 | Bridge | 606 | 162 | 27% | 0
110805 | Tug of War | 318 | 142 | 45% | 1
110406 | CDVII | 204 | 140 | 69% | 1
110602 | How Many Pieces of Land? | 263 | 139 | 53% | 0
111102 | Distinct Subsequences | 286 | 138 | 48% | 0
111303 | The Knights Of the Round Table | 202 | 126 | 62% | 0
110606 | The Priest Mathenmatician | 219 | 121 | 55% | 0
111302 | Rope Crisis in Ropeland | 166 | 113 | 68% | 1
110604 | Expressions | 206 | 108 | 52% | 0
110407 | ShellSort | 333 | 107 | 32% | 0
110705 | Summation of Four Primes | 261 | 105 | 40% | 0
110707 | Marbles | 227 | 98 | 43% | 0
111207 | Dermuba Triangle | 117 | 96 | 82% | 0
111306 | The Largest/Smallest Box.. | 154 | 93 | 60% | 0
110408 | Football (aka Soccer) | 293 | 88 | 30% | 0
110803 | Queue | 127 | 85 | 67% | 0
110806 | Garden of Eden | 137 | 84 | 61% | 0
111006 | Tourist Guide | 165 | 83 | 50% | 0
111402 | The Cloest Pair Problem | 191 | 82 | 43% | 0
110508 | Pairsumonious Numbers | 122 | 80 | 66% | 0
110702 | Carhichael Numbers | 526 | 78 | 15% | 0
110905 | Edit Step Ladders | 173 | 74 | 43% | 0
110904 | Slash Maze | 97 | 74 | 76% | 0
110308 | Fmt | 155 | 72 | 46% | 1
111206 | (2/3/4)-D Sqr/Rects/Cubes/Boxes ? | 75 | 64 | 85% | 0
111202 | The Monocycle | 70 | 54 | 77% | 0
111003 | Fire Station | 156 | 53 | 34% | 0
111108 | Adventures in Moving - Part IV | 93 | 53 | 57% | 0
110208 | Yahtzee | 168 | 50 | 30% | 1
111308 | How Big Is It? | 79 | 48 | 61% | 1
111107 | Chopsticks | 76 | 48 | 63% | 0
110907 | From Dusk Till Dawn | 99 | 47 | 47% | 0
111401 | Herding Frosh | 83 | 44 | 53% | 0
111407 | Trees on My Island | 56 | 43 | 77% | 0
110708 | Repackaging | 62 | 37 | 60% | 0
111405 | Useless Tile Packers | 48 | 32 | 67% | 0
110804 | Servicing Stations | 121 | 30 | 25% | -1
111004 | Railroads | 59 | 27 | 46% | 0
111304 | Chocolate Chop Cookies | 41 | 27 | 66% | 0
111205 | Robbery | 37 | 25 | 68% | 0
111404 | Hotter Colder | 34 | 25 | 74% | 0
110605 | Complete Tree labeling | 51 | 23 | 45% | 0
110807 | Color Hash | 42 | 23 | 55% | 0
111208 | Airlines | 33 | 22 | 67% | 0
111307 | Is This Integration? | 150 | 21 | 14% | 0
110307 | Doublets | 301 | 20 | 7% | -1
111403 | Chainsaw Massacre | 42 | 20 | 48% | 0
111203 | Star | 43 | 16 | 37% | 0
111408 | Nice Milk | 25 | 15 | 60% | 0
111106 | Ferry Loading | 90 | 8 | 9% | 0
111005 | War | 111 | 3 | 3% | 0
111406 | Radar Tracking | 8 | 2 | 25% | 0
111305 | Birthday Cake | 89 | 1 | 1% | 0
111101 | Is Bigger Smarter? | 602 | 0 | 0% | 0
111002 | The Necklace | 252 | 0 | 0% | -1
110802 | 15-Puzzle Problem | 157 | 0 | 0% | 0
111007 | The Grand Dinner | 154 | 0 | 0% | 0
110906 | Tower of Cubes | 95 | 0 | 0% | 0
111008 | The Problem with the Problem Setter | 49 | 0 | 0% | 0
110808 | Bigger Square Please | 30 | 0 | 0% | 0
I then decided to tackle Vito’s Family and Primary Arithmetic (which in the original listing where not completed), and will write them up shortly. After that I’ll slowly work my why through the list, updating the above table as I go.
The -1 in the done column means submitted solutions, but not yet solved.
Programming Challenges:110507 The Stern-Brocot Number System
Last night I was bored, and decided to tackle a Programming Challenges problem: 110507 The Stern-Brocot Number System seemed easy, and in the end it was.
However it took two attempts, due to messing up the termination logic. ie run till m and n are both 1
I first had:
while (m != 1 && n != 1)
instead of:
while (m != 1 || n != 1)
maybe I should have used
while (!(m == 1 && n == 1))
Gosh I dislike double negatives to loop logic.
GetTextMetricsW causing memory corruption
I have been converting our ANSI product to UNICODE, and today during testing I was getting Run-Time Check Failure #2 - Stack around the variable 'tm' was corrupted.
error messages.
Turned out it was this code:
TEXTMETRICW tm;
GetTextMetricsW( hDC, &tm ) ;
I isolated the code into a test function, which then became two functions:
void TestFuncA(HDC hDC)
{
TEXTMETRICA tm;
void* p = &tm;
memset(&tm, -1, sizeof(tm) );
GetTextMetricsA( hDC, &tm ) ;
}
void TestFuncW(HDC hDC)
{
TEXTMETRICW tm;
void* p = &tm;
memset(&tm, 0, sizeof(tm) );
GetTextMetricsW( hDC, &tm ) ;
}
For TestFuncW the sizeof(tm) is 57 bytes, yet GetTextMetricsW is over writing 60, as seen by the Memory debugging window, whereas for TestFuncA sizeof(tm) is only 53 bytes, and GetTextMetricsA overwrites only 53 bytes.
I then created a brand new Win32 Windows C++ application, and placed my test code into this, and strangely enough, for TestFuncW the sizeof(tm) is 60 bytes, yet GetTextMetricsW is over writing 60, and for TestFuncA sizeof(tm) is only 56 bytes, and GetTextMetricsA overwrites only 53 bytes.
Hazarding a guess, “that it is the fact the original product has Struct Member Alignment (same as #pragma pack() ) set to 1 Byte (/Zp1)”, I changed this from the default to 1, and tada!
Now my new project crashes also.
Lesson for others, avoid Struct Member Alignment across full projects.
Lesson for me, I now have weeks of work to unravel the effects/impact of why it was set to 1 in the first place (old legacy code and lazy developers)
Why my file operations fail on the Mac
My first thought for why things fails is the the Environment.SpecialFolder don’t map, and thus I found this page talking about it
So I ran the code on my Windows XP box and got:
Desktop C:\\Documents and Settings\\spilgrim\\Desktop
Programs C:\\Documents and Settings\\spilgrim\\Start Menu\\Programs
Personal C:\\Documents and Settings\\spilgrim\\My Documents
Personal C:\\Documents and Settings\\spilgrim\\My Documents
Favorites C:\\Documents and Settings\\spilgrim\\Favorites
Startup C:\\Documents and Settings\\spilgrim\\Start Menu\\Programs\\Startup
Recent C:\\Documents and Settings\\spilgrim\\Recent
SendTo C:\\Documents and Settings\\spilgrim\\SendTo
StartMenu C:\\Documents and Settings\\spilgrim\\Start Menu
MyMusic C:\\Documents and Settings\\spilgrim\\My Documents\\My Music
DesktopDirectory C:\\Documents and Settings\\spilgrim\\Desktop
MyComputer
Templates C:\\Documents and Settings\\spilgrim\\Templates
ApplicationData C:\\Documents and Settings\\spilgrim\\Application Data
LocalApplicationData C:\\Documents and Settings\\spilgrim\\Local Settings\\Application Data
InternetCache C:\\Documents and Settings\\spilgrim\\Local Settings\\Temporary Internet Files
Cookies C:\\Documents and Settings\\spilgrim\\Cookies
History C:\\Documents and Settings\\spilgrim\\Local Settings\\History
CommonApplicationData C:\\Documents and Settings\\All Users\\Application Data
System C:\\WINDOWS\\system32
ProgramFiles C:\\Program Files
MyPictures C:\\Documents and Settings\\spilgrim\\My Documents\\My Pictures
CommonProgramFiles C:\\Program Files\\Common Files
and on my MacBook I get
Desktop /Users/parents/Desktop
Programs
Personal /Users/parents
Favorites
Startup
Recent
SendTo
StartMenu
MyMusic /Users/parents/Music
DesktopDirectory /Users/parents/Desktop
MyComputer
Templates
ApplicationData /Users/parents/.config
LocalApplicationData /Users/parents/.local/share
InternetCache
Cookies
History
CommonApplicationData /usr/share
System
ProgramFiles
MyPictures /Users/parents/Pictures
CommonProgramFiles
One question raised on that page is why are there two Personal values, and it’s because MyDocuments is the second Enumeration, but .ToString() only names the first in order of an enum set.
So the mystery deepens as I’m using Environment.SpecialFolder.Personal
, and therefore it should work, but it seems to be failing to create the data directory, time to explore more…
After some a bit more playing around I found the file “SAVE/player.cha” in my Mac folder structure, and realised I was doing string concatenation with DOS slashes, etc. hard coded all over the place, so I refactored the code, and now it saves/loads files fine.
I then created a Mac .app bundle, so you can now just download form Google Code the Mac OS X version of the game, un-tar/gzip it, and as long as you have Mono installed, tada, instant game action. Albeit slow…
Only problem to solve is that the save files are case sensitive, in how they are searched and managed, and I’ve not got a standard schema in place. I may have to build some wrapper code to manage this, so odd DOS/Windows casing works fine on Mac/Linux systems.
Also I’ll have to make a Linux tar ball for those Linux/Mono people to test play with.
Sony Walkman NWZ-E345 and MacBook
We purchased a Sony Walkman NWZ-E345 for Michaela’s welcome to the USA present.
We had some concern once we got home because it only mentions support for Windows XP and Windows Vista on the packaging, and nobody on Amazon had mentioned Mac support.
But never fear it works perfectly with our MacBook, it just appears as an USB device, and you just drag’n’drop like an USB hard drive, and when you disconnect (after ejecting via Finder), it rescans the library.
Michaela really likes it, problem solved (the problems was bad music choice in the car).
Curse of the Azure Bonds port working on Mac OS X
Just tried running the game under Mono on my MacBook, and it like works!
Except for
- Saved games/player file IO does not work. I think this is due to the new Vista locations, thus I’ll need a new pick a location code, in which case I could leave Windows XP working in the old location…
- It runs really slow in combat, ie you can see the icon redraws
- There is no sound
- The right click context menu do not seem to work
- You have to lunch it from the terminal prompt
Curse of the Azure Bonds - build 1.1.0 released
I push version 1.1.0 of project to the Google Code project today.
Changes in this version:
- Game now installs and plays on Windows Vista (thus minor version update)
- Saved games and other generated data is now located under “My Documents\Curse of the Azure Bonds”
- Installer now has custom graphics, and cleans up older installers correctly
- Added basic crash logging
The first is the most important, as previously I had just assumed it worked, and not thought much more of it. But Ken posted a comment saying it didn’t work, and well that made me feel bad. So much Virtual PC 2007 time later, it now works.
As always any issues post here in the comments, email me directly, or post on the issue list.
Sugar: The bitter truth
Wow great video on Sugar.
Very much worth the 90 minutes to watch and think about it.
Found via Presentation Zen Visualizing the consequences sugary drinks
Sugar: The bitter truth
Who Cut The Cheese?
I purchased Who Cut The Cheese? - An A-Mazing Parody about Change (and How We Can Get Our Hands on Yours) from a local library books sale for $1, and it was fantastic parody of the Who Moved my Cheese book/program that was ran at a previous employer before lay-off’s.
This book was a very quick read, but so funny in it’s parody. Many frowning looks from my wife as I really laugh-out-loud, and read parts out to her.
Interestingly there seams to a large number of Cheese books on Amazon, and this is probably the best one…
Also if you want a good laugh, read the one star reviews of the original book on Amazon, there are gems like alternative books titles, “It’s Never Easy Letting Valuable Employees Such As Yourself Bill, I mean, Bob“