Author Topic: [ENDED] Code Golf Contest #1 (thanks for the many submissions!)  (Read 40181 times)

0 Members and 2 Guests are viewing this topic.

Offline JWinslow23

  • Coder Of Tomorrow
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 556
  • Rating: +43/-6
  • I make quality calculator games...when I have time
    • View Profile
This is the first of what I hope to be many contests I will hold here on Omnimaga: Code Golf.

Code golf is a competition where you have to solve a coding challenge in the fewest bytes possible. For example, a TI-BASIC entry for a prime tester could be:
Code: [Select]
Input N:0:If N and not(fPart(N:2=sum(seq(not(fPart(abs(N)/I)),I,1,abs(N:Ans(note that this is not the speediest it could be, but speed is not factored in your score, only size)
The score would be 34 bytes (for TI-BASIC programs, score=size - 9 - length of name). The lowest score out of the entries will be the winner.

How this tournament will work:
First off, you need to code an actual program that will solve the given problem (or at least give the right result for all the test cases :P ). All languages are allowed, including calc languages and computer languages. When you have an entry, PM it to me, and I will test it if possible (but just in case I don't have an Nspire or I can't download the latest version of Perl or some such problem, try if you can to give back the results of any and all given test cases). I will then save your entry and update the scores accordingly.
After one week, a winner shall be determined in each language category, as well as the smallest overall. In each language category, the winners shall all suggest possible problems for the next competition. I shall pick the next challenge out of these, and present test cases for any possible input or output. Also, you will get to see everyone else's solutions for the previous challenge.

Please, ask any and all questions that you may have about the contest!

NEXT: Here
PREVIOUS: Here

Challenge 1

Problem
Determine if an inputted number is happy. Happy numbers are defined like this: Take any positive integer, replace it with the sum of the squares of its digits, and repeat the process until it equals 1 or it loops indefinitely in a loop that does not include 1. If it ends up with 1, the number is happy, otherwise it's sad.
Deadline
July 21, 2014, 1:00 AM EST
Sample input 1:
1
Sample output 1:
Code: [Select]
HAPPYSample input 2:
1337
Sample output 2:
Code: [Select]
HAPPYSample input 3:
385
Sample output 3:
Either one of
Code: [Select]
SADor
Code: [Select]
UNHAPPY
Ranking

Python
RankUserSizeDateCode
1willrandship947/19/2014 11:16:56 PM
Spoiler For Spoiler:
x=input();b="UNHAPPY"
for a in b:
 z=0
 for y in str(x):z+=eval(y)**2;x=z
print(b[(z==1)*2:7])
2Juju1487/15/2014 4:22:50 PM
Spoiler For Spoiler:
def h(n):
 while n>1 and n!=4:
  n=sum(dict([(c,int(c)**2)for c in"0123456789"])[d] for d in str(n))
 return n==1
print(("SAD","HAPPY")[h(input())])

Golfscript
RankUserSizeDateCode
1Runer112327/15/2014 5:17:50 PM
Spoiler For Spoiler:
~{`0\{48-.*+}/}9*("SAD""HAPPY"if

CJam
RankUserSizeDateCode
1Runer112307/15/2014 5:17:50 PM
Spoiler For Spoiler:
q~{Ab0\{_*+}/}9*("SAD""HAPPY"?

TI-83+ BASIC
RankUserSizeDateCode
1calc84maniac467/16/2014 5:03:49 PM
Spoiler For Spoiler:
Repeat Ans<5
sum(.5×√int(10fPart(Ans/10^(cumSum(binomcdf(98,0→A
End
"HAPPY
If log(A
"SAD
Ans
2Runer112467/16/2014 5:09:28 PM
Spoiler For Spoiler:
Repeat A≤4
iPart(10fPart(Ans10^(~cumSum(binomcdf(14,0
sum(Ans²→A
End
"HAPPY
If log(A
"SAD
Ans
3Hayleia717/16/2014 2:18:36 AM
Spoiler For Spoiler:
Prompt N
Repeat N=1 or N=4
sum(seq((10fPart(iPart(N10^(~I))/10))²,I,0,14→N
End
"HAPPY
If N=4
"SAD
Disp Ans

TI-83+ z80
RankUserSizeDateCode
1Runer112587/20/2014 9:32:08 PM
Spoiler For Spoiler:
;#SECTION "MAIN", CODE

   org   userMem - 2
   db   0BBh, 6Dh
Start:
   B_CALL   _RclAns
StepLoop:
   push   bc
   sbc   hl, hl
   ld   b, h
DigitPairLoop:
   dec   e
DigitLoop:
   ex   de, hl
   xor   a
   rrd
   ex   de, hl
   ld   c, a
SquareLoop:
   add   hl, bc
   dec   a
   jq   nz, SquareLoop
   ld   a, (de)
   or   a
   jq   nz, DigitLoop
   ld   a, e
   cp   (OP1 + 2) & 0FFh
   jq   nz, DigitPairLoop
   push   hl
   B_CALL   _SetXXXXOP2
   rst   30h
   pop   hl
   pop   bc
   djnz   StepLoop
   dec   l
   ld   hl, UnhappyStr
   jq   nz, Unhappy
   inc   hl
   inc   hl
Unhappy:
   B_CALL   _PutS
   ret

;#SECTION "StrData", DATA

UnhappyStr:
   db   "UNHAPPY", 0
2calc84maniac607/16/2014 12:12:44 PM
Spoiler For Spoiler:
#define bcall(xxxx) rst 28h \\ .dw xxxx
#define _RclAns $4AD7
#define _PutS $450A
#define OP1 $8478
   
    .org $9D93
    .db $BB,$6D
    bcall(_RclAns)
    ex de,hl
HappyCalcLoop:
    xor a
    ld c,a
    ld d,a
    ld e,a
HappyByteLoop:
    ;Carry is reset, upper nibble of A is 0
    dec l
HappyNibbleLoop:
    rrd
    ld b,a
HappyMulLoop:
    push af
     add a,e
     daa
     ld e,a
     ld a,c
     adc a,d
     daa
     ld d,a
    pop af
    djnz HappyMulLoop
    ccf
    jr c,HappyNibbleLoop
    ld a,l
    sub (OP1+2)&$FF
    jr nz,HappyByteLoop
    ld (hl),d
    inc l
    ld (hl),e
    inc l
    inc (hl)
    jr nz,HappyCalcLoop
    ld hl,HappyString
    dec e
    jr z,$+4
    dec hl
    dec hl
    bcall(_PutS)
    ret
   
UnhappyString:
    .db "UN"
HappyString:
    .db "HAPPY",0

Batch
RankUserSizeDateCode
1JWinslow231827/19/2014 7:26:37 PM
Spoiler For Spoiler:
@set/p#=
:@
@set $=0&@for /f "delims=" %%a in ('cmd /U /C echo %#%^|find /V ""')do @set/a$+=%%a*%%a
@set #=%$%&@if %$% neq 1 if %$% neq 4 goto @
@if %$%==1 (echo happy) else echo sad

Language Ranking
RankLangUserSizeDate
1CJamRuner112307/15/2014 5:17:50 PM
2GolfscriptRuner112327/15/2014 5:17:50 PM
3TI-83+ BASICcalc84maniac467/16/2014 5:03:49 PM
4TI-83+ z80Runer112587/20/2014 9:32:08 PM
5Pythonwillrandship947/19/2014 11:16:56 PM
6BatchJWinslow231827/19/2014 7:26:37 PM
« Last Edit: June 11, 2015, 08:59:10 am by pimathbrainiac »
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
Click here for the only set of games you'll ever need
= ?

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Code Golf Contest #1
« Reply #1 on: July 14, 2014, 08:50:54 pm »
Can I assume that since the problem states that the input is a positive integer, that the program does not have to check for these properties?




Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Code Golf Contest #1
« Reply #2 on: July 14, 2014, 09:13:06 pm »
Do you/others think it's better for entries to be messaged to you privately, rather than just posted here in a spoiler?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Code Golf Contest #1
« Reply #3 on: July 14, 2014, 09:51:30 pm »
For z80 assembly, will it be judged on the size of the executable? I guess that's the only thing that makes sense...

Also, I guess we can probably take floating-point input from Ans, and work with the precision available? That is, 14 digits of precision in TI-OS floats.

One last thing, can we make it print HAPPY/UNHAPPY rather than HAPPY/SAD? This would actually be slightly more size-optimized :P
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Code Golf Contest #1
« Reply #4 on: July 15, 2014, 12:54:34 am »

Do you/others think it's better for entries to be messaged to you privately, rather than just posted here in a spoiler?
OP does say to PM entries to him, so I'd assume so. I think it makes sense to PM them anyway. We could share the code in the topic after the deadline, maybe




Offline JWinslow23

  • Coder Of Tomorrow
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 556
  • Rating: +43/-6
  • I make quality calculator games...when I have time
    • View Profile
Re: Code Golf Contest #1
« Reply #5 on: July 15, 2014, 11:01:42 am »
No, do not check for positive-integer-ness unless you really want to. It at least has to give the right result for the test cases.
Yes, Xeda, whatever you want. Just make it work at least with the test cases.
Yes, PM me any solutions.
Yes, z80 is judged by its final size. So would Axe.
Yes, printing HAPPY/UNHAPPY would also be acceptable.

EDIT: Oh, and I will reveal all solutions in spoilers in the OP.
My own solution in TI-BASIC is 71 bytes (minus the name). Let's see who can do better...
« Last Edit: July 15, 2014, 12:33:58 pm by JWinslow23 »
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
Click here for the only set of games you'll ever need
= ?

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Code Golf Contest #1
« Reply #6 on: July 15, 2014, 01:19:21 pm »
In some cases, printing HAPPY/SAD would be less characters.

Also can I send you more than one solution? As in, I can send you a PM as I improve?

Also my current solution is 159 bytes of Python. ._.
« Last Edit: July 15, 2014, 01:22:49 pm by Juju »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Code Golf Contest #1
« Reply #7 on: July 15, 2014, 01:38:08 pm »
Lol, I have 71 bytes too in TI Basic :P
Not bad for someone whose main language is Axe :P

edit just noticed how similar it is with Opti-Défis on TI Planet o.o
« Last Edit: July 15, 2014, 01:41:38 pm by Hayleia »
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline JWinslow23

  • Coder Of Tomorrow
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 556
  • Rating: +43/-6
  • I make quality calculator games...when I have time
    • View Profile
Re: Code Golf Contest #1
« Reply #8 on: July 15, 2014, 02:15:15 pm »
Juju, you then shall be the winner in Python...if nobody else does it smaller. :P But you will still get credit.
And yes, you can send me multiple PMs.

Hayleia, PM it to me so I can test it, and if it works for my test cases, you will be one of the first people to participate.
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
Click here for the only set of games you'll ever need
= ?

Offline JWinslow23

  • Coder Of Tomorrow
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 556
  • Rating: +43/-6
  • I make quality calculator games...when I have time
    • View Profile
Re: Code Golf Contest #1
« Reply #9 on: July 15, 2014, 03:34:22 pm »
First entry!

Thanks to Juju with his 159 byte solution in Python! (counting newline as CRLF, as my OS does)
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
Click here for the only set of games you'll ever need
= ?

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: Code Golf Contest #1
« Reply #10 on: July 15, 2014, 04:21:45 pm »
I've beaten Juju's score with python. Will PM shortly.


On my system it comes to 128 bytes, but that's with Unix line endings, so add another 10 bytes or so to that.


Edit: Sent another version saving another 5 bytes worth of spaces.
« Last Edit: July 15, 2014, 04:32:48 pm by willrandship »

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Code Golf Contest #1
« Reply #11 on: July 15, 2014, 04:29:15 pm »
Oh, right, the line endings. So we're gonna count with UNIX or Windows line endings? Converted to UNIX line endings my entry only weight 155 bytes.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: Code Golf Contest #1
« Reply #12 on: July 15, 2014, 04:33:09 pm »
Looks like he's counting in windows line endings, which is why I added the disclaimer.

Offline JWinslow23

  • Coder Of Tomorrow
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 556
  • Rating: +43/-6
  • I make quality calculator games...when I have time
    • View Profile
Re: Code Golf Contest #1
« Reply #13 on: July 15, 2014, 05:02:37 pm »
I am counting Windows (CRLF=2 bytes). willrandship, your score is 116 bytes. It does work, and I am now adding yours as we speak.

EDIT: Miscount. Redone!
« Last Edit: July 15, 2014, 05:08:49 pm by JWinslow23 »
Did you know that "Ammonia Gas" rearranged is "As Omnimaga"?
Click here for the only set of games you'll ever need
= ?

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Code Golf Contest #1
« Reply #14 on: July 15, 2014, 05:10:25 pm »
Why count newlines as two characters? I believe that basically any modern interpreter/compiler accepts either Unix-style or Windows-style newlines, so surely in the spirit of golfing you'd want to use the one-byte option?