HHC 2021 Programming Contests - Surprise !
|
10-12-2021, 03:42 PM
(This post was last modified: 10-25-2021 02:45 PM by Jeff O..)
Post: #30
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Checking this thread for the first time after sleeping on the problem for several nights, working out an approach, starting to write code on a DM42 a few days ago, then fussing with it intermittently until I got a version that worked on all inputs. I first considered the “add 1, see if it is a palindrome, if not, add one and so on” method, but worried that that method could take a while to run on real machines, and surely there was a better way, so I tried to arrive at a direct approach. Eventually I arrived at the method described by Dave B. (Before checking this thread I assumed that I did not come up with a novel method among this group and was not surprised to see it used.) It took lots of tricks (for me at least) to handle all the cases, e.g. even number of digits vs. odd, all nines. The working version is 74 steps, 127 bytes. But I also used the ALENG instruction which I now see violated the rules* so I would have to replace all of those with LOG IP 1 + sequences. But at least it works and I think would be relatively fast on a real 42s since it is a direct approach with no looping required (except a small number of loops to build the palindromes).
Program listing is below. I may try to correct the above and optimize the code (which is quite clunky, I’m sure), if so I’ll post an updated listing. High level comments: Steps 1 through 20 set a flag if the input has an odd number of digits, leaving it clear for an even number, and lops off the rightmost n/2 or (n-1)/2 digits for inputs with an even or odd number of digits, respectively. This leaves an n/2 or (n+1)/2 digit number for even/odd digit inputs. Step 21 calls a subroutine which creates a palindrome by reversing the n/2 digits and tacking on the end for even digit inputs, or reversing the first (n-1)/2 digits and tacking on the end, leaving the rightmost digit (which was the middle digit of the input number) alone for odd-digit inputs. Then compare to the original, if larger, we have succeeded in finding the next largest palindromic number and stop. If equal or smaller, we have not succeeded, so steps 26 through 49 create the next higher palindromic number by incrementing the rightmost digit of the truncated number and creating a palindrome with the subroutine. It took some fussing to enable the routine to handle the "all nines" cases using the method used for all other inputs. It would be simpler and shorter to simply detect that case and add 2 to the input number to find the next higher palindrome. That seems a little "brute-force-y", not as "elegant" as using the same method for all inputs. But is mathematically valid, and I had to handle them as special cases anyway, so it is probably better. Code for that option is provided in the second code block below. I will admit to not having studied all entries discussed above in detail, so if I have used any techniques from those routines, I give full credit to the author and acknowledge priority. I also credit Dejan R. for much of the palindrome building subroutine. While cogitating on my approach, I knew that I would need a routine to reverse digits. Then Dejan handily posted a routine to the HHC group that was easily adapted. I also acknowledge that I worked on it for a week-and-a half, not continuously of course, but not one night as intended for the contest, or the couple of days for other solutions posted above. edit - slightly improved (2 steps shorter) versions developed before I saw Werner's very nice technique that allowed for a significant improvement detailed in a separate post below. Code: 00 { 124-Byte Prgm } Version that simply adds 2 for the all nines cases, "only" 67 steps, 108 bytes: Code: 00 { 108-Byte Prgm } * - Correction, ALENG does not violate the rules, see Gene's post below. Dave - My mind is going - I can feel it. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 10 Guest(s)