16
\$\begingroup\$

For my CS class, my instructor has given us the assignment of creating a rectangle made of asterisks with diagonal lines drawn through it in Java.

He also told us to write it in as few bytes as possible. I've gotten it down to 190 bytes, but I need to find a few to simplify this code even more to decrease bytes. Can anyone help me with this?

This code is functional:

interface d{static void main(String[]a){for(int z=0,w=new Byte(a[0]),h=new Byte(a[1]);z<h*w;){int y=z/w,x=z++%w;System.out.print((x>w-2)?"*\n":(y%(h-1)*x*((y-x)%3)==0)?"*":" ");}}}

Input is 10 10.

Output:

**********
**  *  * *
* *  *  **
*  *  *  *
**  *  * *
* *  *  **
*  *  *  *
**  *  * *
* *  *  ** 
\$\endgroup\$
13
  • 32
    \$\begingroup\$ Don't redirect him to SO; he'll get eaten alive. \$\endgroup\$
    – Leaky Nun
    Aug 22 '16 at 15:55
  • 3
    \$\begingroup\$ Can you clarify the output and specs of the program? i.e. example input/output etc \$\endgroup\$ Aug 22 '16 at 15:56
  • 30
    \$\begingroup\$ I'm not entirely sure why this being close voted. This is most definitely not a general programming question. It's pretty much a standard tips question, which is very much on topic. I'm not sure whether we have a policy on homework, although as far as I can see the OP is even showing their own effort, so I don't think there's really anything wrong with this? \$\endgroup\$ Aug 22 '16 at 15:58
  • 3
    \$\begingroup\$ @NathanMerrill Advice for specific golfing problems is most definitely not off topic. \$\endgroup\$ Aug 22 '16 at 15:59
  • 3
    \$\begingroup\$ @LeakyNun we don't eat people alive, we kill them first ;) \$\endgroup\$
    – user51896
    Aug 23 '16 at 0:28
1
\$\begingroup\$

Logically, there should be an asterisk (*) every time i == j and i+j==w-1(for diagonals), i == 0 and j == 0 (for top line and left side) and j == w-1 and i==h-1 (for right side and bottom line).

class d {
    public static void main(String[] a) {
        for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++) {
            for(int j=0;j<w;j++) {
                System.out.print(i==0 || j==0 || i==h-1 || i+j==w-1 || j==w-1 || i==j ? "*":" ");
            }
            System.out.println();
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ I don't know if this is an example of the logic that can be used or a fully golfed code. If it's the second case, there's a lot to golf here. Such as, the useless System.out.println() and all the useless whitespaces between the OR pipes (||) and the ternary operator. \$\endgroup\$
    – Yytsi
    Sep 14 '16 at 6:30
  • 1
    \$\begingroup\$ this is just an example to logic @TuukkaX. \$\endgroup\$ Sep 19 '16 at 7:55
1
\$\begingroup\$

The code you provided could be reduced by doing this:

  • replace * with 42 and " " by ' ' (taking advantage of the charcode for *)
  • move ((y - x) % 3) to the beginning of the things to be multiplied and remove the surrounding parenthesis
  • remove surrounding parenthesis from (x > w - 2) and from ((y - x) % 3 * y % (h - 1) * x == 0)

The resulting code would be:

interface r{static void main(String[]a){for(int z=0,w=new Byte(a[0]),h=new Byte(a[1]);z<h*w;){int y=z/w,x=z++%w;System.out.print(x>w-2?"*\n":(y-x)%3*y%(h-1)*x==0?42:' ');}}}

Note: the last line is missing in the example output of the question! The output of the sample code is different.

\$\endgroup\$
1
\$\begingroup\$

178 174 chars

interface T{static void main(String[]a){for(int w=new Byte(a[0]),h=new Byte(a[1]),j,i=0;i<h;i++)for(j=0;j<w;System.out.print((i-j)%3*i*j!=0&i<h-1&++j<w?" ":j<w?"*":"*\n"));}}

Try it online!

Readable:

interface T{
    static void main(String[]a){
        for(int w=new Byte(a[0]),h=new Byte(a[1]),j,i=0;i<h;i++)
            for(j=0;j<w;System.out.print(
                    (i-j)%3*i*j!=0&i<h-1&++j<w?" ":j<w?"*":"*\n"));
    }
}

Output for field: 30 10:

******************************
**  *  *  *  *  *  *  *  *  **
* *  *  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *  * *
**  *  *  *  *  *  *  *  *  **
* *  *  *  *  *  *  *  *  *  *
*  *  *  *  *  *  *  *  *  * *
**  *  *  *  *  *  *  *  *  **
* *  *  *  *  *  *  *  *  *  *
******************************
\$\endgroup\$
0
0
\$\begingroup\$

I don't actually have Java on my computer so I can't test this but I think it should work for 174 bytes and almost definitely could be golfed more

class d{static void main(String[]a){for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++)for(int j=0;j<w;j++)System.out.print(j==w-1?"*\n":i%h-1==0||!j||i+j%2==0?"*":" ");}}

Whitespace added for clarity:

class d {
    static void string main(String[] a) {
        for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++)
            for(int j=0;j<w;j++)
                System.out.print(j==w-1 ? "*\n" : i % h-1 == 0 || !j || i+j % 2 == 0 ? "*":" ");
    }
}

print *\n for the last char in each line, * for all of the first and last lines and the first column, and * for any time that the sum of the row and column is even, otherwise prints

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.