TinyCode competition - trial

smacks forehead

http://213.247.55.3:8484/compileandrun/?assignment=hex


for(int a,s=1;(a=in.read())>0;)
if((s=s*16+(a<58?a-48:a-87))>255){out.write(s);s=1;}

(82 bytes)

Can you not reduce the size of those numeric constants by including the -48 in the (a=in.read())>0 ?

I tried, I tried :’(

Hex:


for(int a,s=1;s>0;s=s*16|((a=in.read())<58?a-48:a-87))
if(s>255){out.write(s);s=1;}

(81 bytes)

BinaryShrink:


for(int b=1;b>0;b=b*2|in.read()-48)
if(b>255){out.write(b);b=1;}

(62 bytes)

Look how much they look alike :slight_smile:

another one bytes the dust: 80 bytes :slight_smile:

for(int a,s=1;s>0;s=s*16|-((a=in.read())<58?48:87)+a)
if(s>255){out.write(s);s=1;}

Edit:

Got another one for 79 bytes

for(int a,s=1;s>0;s=s*16|(a=in.read())-(a<58?48:87))
if(s>255){out.write(s);s=1;}

Yeah, I saw you moving from 81 to 80 bytes on the server.
Then I made a 79 using your version, but I didn’t dare to post it, because it felt like cheating.
But now that you posted it, all I can say is that I did it slightly different… but you get the credit :slight_smile:

Would you post your version, just for my enlightment? :slight_smile:

We need no stinking a variable:

for(int s=1;s>0;s=s*16|(s=in.read())-(s<58?48:87))
if(s>255){out.write(s);s=1;}

That makes it 77 bytes.

ouch, nested self-assignment - now things are getting complicated =)

If I saw that in some production code I would likely have to beat someone to death ;D

I’m getting beaten at my own game. >:(

:’(

I’m playing with changing the conditional operator (s<58?48:87) for an arithmetic/shift solution, so that the additional parenthesis can be eliminated…
Doing it without breaking the end condition s>0 is the hard part…
ah, is that what you did for your 79byte version Riven? “…in.read()-48)-(s/49)*39” ?

You (who?) did it, you just need to “-(s/49)" >>> "-s/49


for(int s=1;s>0;s=s*16|(s=in.read()-48)-s/49*39)
if(s>255){out.write(s);s=1;}

(75 bytes)

indeed, well stolen :stuck_out_tongue:

ok, time to sleep ;D

You’ve got until tomorrow to come up with some more challenges =)

I’ll try not to SSH into the server again. :-*

Tomorrow = exams
for(int i=0; i<4; i++)
After that… exams

Gotta say, your first targets were pretty damn good!
It’s taken 4 people, and a dozen or so iterations to get them down to 62 & 75 bytes respectively!

If anybody has a fancy idea…

This is how I implmented the hex-assignment:


public class HexTinyCodeAssignment extends TinyCodeAssignment
{
   @Override
   public String getName()
   {
      return "hex";
   }

   @Override
   public String getTitle()
   {
      return "Hex";
   }

   @Override
   public String getDescription()
   {
      return "There are values encoded into hexadecimals, turn them into bytes again";
   }

   @Override
   public String getExample()
   {
      return "'6e64' -&gt; 'nd'";
   }

   @Override
   public byte[] createQuestion()
   {
      return str2arr("72616e646f6d206d61726b65722036");
   }
   
   @Override
   public String getDefaultSource()
   {
      StringBuilder sb = new StringBuilder();
      sb.append("while (true)\n");
      sb.append("{\n");
      sb.append("   int a = in.read();\n");
      sb.append("   int b = in.read();\n");
      sb.append("   if ((a | b) == -1)\n");
      sb.append("       break;\n");
      sb.append("   int ai = (a <= '9') ? (a-'0') : (a-'a'+10);\n");
      sb.append("   int bi = (b <= '9') ? (b-'0') : (b-'a'+10);\n");
      sb.append("   out.write(ai * 16 + bi);\n");
      sb.append("}");
      return sb.toString();
   }

   @Override
   public void processCorrectly(InputStream in, OutputStream out) throws IOException
   {
      while (true)
      {
         int a = in.read();
         int b = in.read();
         if ((a | b) == -1)
            break;
         int ai = (a <= '9') ? (a-'0') : (a-'a'+10);
         int bi = (b <= '9') ? (b-'0') : (b-'a'+10);
         out.write(ai * 16 + bi);
      }
   }
}

Post your TinyCodeAssignment subclass !

This is the class:

public abstract class TinyCodeAssignment
{
   public abstract String getName();

   public abstract String getTitle();

   public abstract String getDescription();

   public abstract String getExample();

   public abstract byte[] createQuestion();

   public abstract String getDefaultSource();

   public abstract void processCorrectly(InputStream in, OutputStream out) throws IOException;

   public final byte[] createAnswer(byte[] question)
   {
      try
      {
         InputStream in = new ByteArrayInputStream(question);
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         this.processCorrectly(in, out);
         return out.toByteArray();
      }
      catch (IOException exc)
      {
         // never happens
         throw new RuntimeException(exc);
      }
   }

   public final boolean isValid(byte[] correctAnswer, byte[] userAnswer)
   {
      if (correctAnswer.length != userAnswer.length)
         return false;
      for (int i = 0; i < correctAnswer.length; i++)
         if (correctAnswer[i] != userAnswer[i])
            return false;
      return true;
   }

   public static final String arr2str(byte[] arr)
   {
      try
      {
         return new String(arr, "ASCII");
      }
      catch (UnsupportedEncodingException exc)
      {
         throw new RuntimeException(exc);
      }
   }

   public static final byte[] str2arr(String str)
   {
      try
      {
         return str.getBytes("ASCII");
      }
      catch (UnsupportedEncodingException exc)
      {
         throw new RuntimeException(exc);
      }
   }
}

Poll: Can I get away with posting pages of code??

Is your server keeping track of what the best submitted solution is for each assignment?
It’s be nice if we could see the bytesize of the current best, and maybe the solution after say 1 hour. (so they get their 60 minutes of glory =D)

I’m awefully busy with… joining my own contest, building the server, watching SSH scroll by, and studying for exams :slight_smile:

I’ve got the MySQL instance setup, with database/tables, but it aint yet connected…

I’m used to programming this stuff in PHP, and I gotta say that Java is a bit annoying here…
I had to build a dynamic classloader that grabs the latest JARs and runs them.
There aint no CTRL+S, F5 here. Bah.

And I really miss the multi-line strings…


txt = <<<EOF
SELECT *
FROM ..
WHERE id = 3
EOF;

And the more I use PHP, the weirder the + operator looks for concatenating Strings in Java…

How do you guys program a website in Java!!! Aaahhh!!

Anyway, to answer your question: I’m the only one keeping track of anything. I think i’m just going to make it functional. Just that, and don’t give a crap about the looks.