[BASIC Challenge # 1] Destroy the LURD code!

A General Discussion forum for TI calculators

Moderator: MaxCoderz Staff

User avatar
ProphetsDementia
Regular Member
Posts: 81
Joined: Wed 06 Dec, 2006 2:20 am
Location: Tokyo, Kanto, Japan

[BASIC Challenge # 1] Destroy the LURD code!

Post by ProphetsDementia »

The first of many challenges to keep people sharp in BASIC.
Regarding the subject, the LURD code. Everyone's used it in one time in their lives. But, understand that the code can be optimised.

Your mission:

1) Optimise the code and make it in as little lines as possible.

Code: Select all

ClrHome
1->A
1->B
0->C
0->D
0->K
Repeat 0
Output(A,B,"X
A->C
B->D
While K=0
getKey->K
End
If K=24
Then
If B>1
Then
B-1->B
End
End
If K=25
Then
If A>1
Then
A-1->A
End
End
If K=26
Then
If B<16
Then
B+1->B
End
End
If K=34
Then
If A<8
Then
A+1->A
End
End
Output(C,D,"  (1 space)
End
The scary thing is that this code was uber at one time! :x
User avatar
Delnar_Ersike
Sir Posts-A-Lot
Posts: 153
Joined: Mon 22 Jan, 2007 3:05 am
Location: UNATCO Headquarters

Post by Delnar_Ersike »

Here's my optimization work:

Code: Select all

ClrHome
1->A
1->B
DelVar CDelVar DDelVar KRepeat 0
Output(A,B,"X
Repeat getkey
End
Ans->K
Output(A,B,"  (1 space)
B-(K=24 and B>1)+(K=26 and B<16->B
A-(K=25 and A>1)+(K=34 and A<8->A
End
User avatar
ProphetsDementia
Regular Member
Posts: 81
Joined: Wed 06 Dec, 2006 2:20 am
Location: Tokyo, Kanto, Japan

Post by ProphetsDementia »

Nice. But the answer is an optimization that is in eight lines. :x
"Choice. The problem is choice."
"Quite correct. Strange; that was quicker than the others."
"Others?"

"In your case, this is the sixth version of the Matrix."
"Sixth version? Then, either no one told me or no one knew."
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

well I can get 1 off..
..without cheating that is, this could all be 1 line with colons

Code: Select all

1->A
1->B
Repeat 0
ClrHome
Output(A,B,"X
Repeat getkey
End
Ans->K
B-(K=24 and B>1)+(K=26 and B<16->B
A-(K=25 and A>1)+(K=34 and A<8->A
End
edit: with blinking:

Code: Select all

1->A
1->B
Repeat 0
ClrHome
Output(A,B,"X
getkey->K
B-(k=24 and B>1)+(K=26 and B<16->B
A-(k=25 and A>1)+(K=34 and A<8->A
End
with cheating:
note: that isn't a linebreak, it's your window being too small

Code: Select all

1->A:1->B:Repeat 0:ClrHome:Output(A,B,"X"):getkey->K:B-(k=24 and B>1)+(K=26 and B<16->B:A-(k=25 and A>1)+(K=34 and A<8->A:End
if A and B are 1 to begin with and you have a lot of free ram:

Code: Select all

ClrHome
Output(A,B,"X
getkey->K
B-(k=24 and B>1)+(K=26 and B<16->B
A-(k=25 and A>1)+(K=34 and A<8->A
prgmTHISPROG
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

Here's 8 lines
:DelVar ARepeat 0
:ClrHome
:Output(real(A)+1,imag(A)+1,"X
:Repeat Ans
:getkey
:End
:A-(Ans=25 and real(A)>0)+(Ans=34 and real(A)<7)-i(Ans=24 and imag(A)>0)+i(Ans=26 and imag(A)<15->A
:End

And 5 lines.
:DelVar ARepeat 0
:ClrHome
:Output(real(A)+1,imag(A)+1,"X
:A-(getkey=25 and real(A)>0)+(getkey=34 and real(A)<7)-i(getkey=24 and imag(A)>0)+i(getkey=26 and imag(A)<15->A
:End

Been a while since I've done basic.
Image
merthsoft
Extreme Poster
Posts: 496
Joined: Tue 21 Dec, 2004 6:49 am
Location: WI

Post by merthsoft »

Jim e: Your 8 line one works a lot better, and is 106 bytes, on top of that it only uses one variable, very impressive... The 5 line one works poorly, and is 99 bytes... The performance is defiantly worth the 7 bytes.
In contrast, King Harold's non-blinking one (which I had to modify to work) takes 94 bytes, but takes 3 variables.
If you don't take into consideration variables (which most people don't), I'm gonna take King Harold's, though it does take on more line...
Shaun
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

You had to modify it? :o
Well I hadn't tested it but I thought it'd work..
User avatar
kalan_vod
Calc King
Posts: 2932
Joined: Sat 18 Dec, 2004 6:46 am
Contact:

Post by kalan_vod »

Jim e wrote:Here's 8 lines
:DelVar ARepeat 0
:ClrHome
:Output(real(A)+1,imag(A)+1,"X
:Repeat Ans
:getkey
:End
:A-(Ans=25 and real(A)>0)+(Ans=34 and real(A)<7)-i(Ans=24 and imag(A)>0)+i(Ans=26 and imag(A)<15->A
:End

And 5 lines.
:DelVar ARepeat 0
:ClrHome
:Output(real(A)+1,imag(A)+1,"X
:A-(getkey=25 and real(A)>0)+(getkey=34 and real(A)<7)-i(getkey=24 and imag(A)>0)+i(getkey=26 and imag(A)<15->A
:End
Change real(A)>0 to real(A), imag(A)>0 also. Then you can change imag(A)<15 to 15>imag(A, do these and you shave a whole 5 bytes!

Now, if you wanted to become less complicated (which would make it faster and smaller in this case).

Code: Select all

:1->A
:1->B
:ClrHome
:Output(Ans,B,"X
:Repeat Ans
:getKey->K
:End
:B+(Ans=26 and B<16)-(Ans=24 and B>1->B
:A+(K=34 and A<8)-(K=25 and A>1->A
:End
Now, I know this is 10 lines, but it is 94 bytes and is faster (12 smaller).
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

That looks an awful lot like my code though, except you store getKey -> K instead of Ans -> K and your use of and in output, which looks cool, but it is useful?

Code: Select all

:Delvar ADelvarBRepeat 0
:ClrHome
:Output(A+1,B+1,"X
:Repeat getkey
:End
:Ans->K
:B+(Ans=26 and B<15)-(Ans=24 and B->B
:A+(K=34 and A<7)-(K=25 and A->A 
:End
how about that
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

King Harold wrote:That looks an awful lot like my code though, except you store getKey -> K instead of Ans -> K and your use of and in output, which looks cool, but it is useful?

Code: Select all

:Delvar ADelvarBRepeat 0
:ClrHome
:Output(A+1,B+1,"X
:Repeat getkey
:End
:Ans->K
:B+(Ans=26 and B<15)-(Ans=24 and B->B
:A+(K=34 and A<7)-(K=25 and A->A 
:End
how about that
it doesn't work though.
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

why not?
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

Have you tried it? Getkey on Repeat doesn't set Ans.
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

No I didn't try it, I just copied that from Delnar_Ersike and assumed it would work..
merthsoft
Extreme Poster
Posts: 496
Joined: Tue 21 Dec, 2004 6:49 am
Location: WI

Post by merthsoft »

That's what I had to modify, it needs to be
Repeat ans
getKey->ans
End
But either way, it was 94 bytes, just like kalan_vod's...
Shaun
User avatar
Super Speler
Regular Member
Posts: 113
Joined: Fri 09 Feb, 2007 2:20 am
Location: Alpha Centuri

Post by Super Speler »

getKey->ans, that doesn't work, just use getKey.
Post Reply