Skip to main content
Clarification on side-effect
Source Link
CAD97
  • 1.3k
  • 10
  • 17

A side goal was being able to print every iteration of the board: just remove the middle ; in while(g.i()) ; System.out.print(g.s()); (Though this does disable the last print which has the 0->power conversion). Unfortunately, in this version, gravity works in strangely. Each pass I take the first non-stuck ball and move it. Short circuiting iterate() there is less bytes than going over the whole board then returning if anything changed.

A side goal was being able to print every iteration of the board: just remove the middle ; in while(g.i()) ; System.out.print(g.s());. Unfortunately, in this version, gravity works in strangely. Each pass I take the first non-stuck ball and move it. Short circuiting iterate() there is less bytes than going over the whole board then returning if anything changed.

A side goal was being able to print every iteration of the board: just remove the middle ; in while(g.i()) ; System.out.print(g.s()); (Though this does disable the last print which has the 0->power conversion). Unfortunately, in this version, gravity works in strangely. Each pass I take the first non-stuck ball and move it. Short circuiting iterate() there is less bytes than going over the whole board then returning if anything changed.

Fixed an edge case + better golf
Source Link
CAD97
  • 1.3k
  • 10
  • 17

Java, Too Many 11021102 987 bytes

Realistically, this could be golfed further. I was more interested in implementing the spec, however. I'll take another stab at golfing it tomorrow (maybe even make it into triple digits ^.^).\o/ It's under 1000!

class G{static class T{final char s;int p=0;T(char c){s=c;}}static T F=new T('-');static T R=new T('\\');static T L=new T('/');static T W=new T('#');static T A=new T(' ');static class B{T[][]o;B(T[][]t){o=t;}boolean;T[][]o;boolean i(){boolean r=1<0;for for(int i=oi=1;i<o.length-2; i>0; i--length;i++)for (int j=0; j<o[i]j=1;j<o[i].length; j++length;j++)if (o[i][j].p>0){if (m(i,j,i+1,j)||o[i+1][j]==L&&m||o[i+1][j].s=='/'&&m(i,j,i+1,j-1)||o[i+1][j]==R&&m||o[i+1][j].s=='\\'&&m(i,j,i+1,j+1))r =return 1>0;else1>0;int o[i][j]=neww=o[i][j].p;o[i][j]=new T((o[i][j]Integer.p+""toString(w).charAt(0));return{{p=w;}};}return r;1<0;}boolean m(int a, int b, int c, int d){if (o[c][d]==A||o[c][d].p>0){o[a][b].p+=o[c][d].p;o[c][d]=o[a][b];o[a][b]=A;return 1>0;}return 1<0;}public String toStrings(){String s="";for(T[]r:o){for(T t:r)s+=t.s;s+="\n";}return s;}}static Bvoid pf(String s) {String[]r=s.split("\\\\n");T[][]b=new;o=new T[r.length][r[0].length()];for(int i=0;i<bi=0;i<o.length;i++)for(int j=0;j<b[i]j=0;j<o[i].length;j++)switch(r[i].charAt(j)){case'-':b[i][j]=F;break;case'\\'o[i][j]=new T('-');break;case'\\':b[i][j]=R;break;case'o[i][j]=new T('\\');break;case'/':b[i][j]=L;break;case'o'o[i][j]=new T('/');break;case'o':b[i][j]=newo[i][j]=new T('o'){{p=1;}};break;case'#':b[i][j]=W;break;default:b[i][j]=A;}return newo[i][j]=new BT(b'#');;break;default:o[i][j]=A;}}public static void main(String[]a){BG b=pg=new G();g.f(a[0]);while(bg.i());System.out.printprintln(bg.s());}}

A side goal was being able to print every iteration of the board: just remove the middle ; in while(bg.i()) ; System.out.print(bg.s());. Unfortunately, in this version, gravity works in strangely. Each pass I take the first non-stuck ball and move it. Short circuiting iterate() there is less bytes than going over the whole board then returning if anything changed.

java -jar G.jar "##########\n# \o-/    #\n#-  #\n#o/ -/ #\n# oo   o #\n#  - -\o #\n#/-    \\o #\n# o \oo-/\#\n#-/ \ o \#\n# /#\n#   \o \  #\n#   -\o  #\n#\  \\ o/#\n##########"
public class GravitySimulator {
    static class Token {
        static Token FLOOR = newchar Token('-');symbol;
        static Tokenint SLIDE_RIGHTpower = new Token('\\');0;
        static Token SLIDE_LEFT = new Token('/');
        static Token WALL = new Token('#');
        static Token AIR = new Token('char 'c);

    {
    final char symbol;
       symbol int= power=0;c;
        Token(char s) {symbol=s;}
    }

    static class Board {
        Token[][] board;
    Token A = new BoardToken(Token[][]' tokens') {;
            board = tokens;
       Token[][] }board;

        boolean iterate() {
            boolean didSomething = false;

            for (int i=board.length-2;i=1; i>0;i<board.length; i--i++) {
                for (int j=0;j=1; j<board[i].length; j++) {
                    if (board[i][j].power>0) {
                        if (move(i,j,i+1,j) || board[i+1][j]==Tokenboard[i+1][j].SLIDE_LEFTsymbol=='/' && move(i,j,i+1,j-1) || board[i+1][j]==Tokenboard[i+1][j].SLIDE_RIGHTsymbol=='\\' && move(i,j,i+1,j+1))
      return true;
                    int didSomethingpow = true;board[i][j].power;
                        else board[i][j] = new Token(Integer.toString(board[i][j].powerpow).charAt(0));
                    {{power=pow;}};
                }
            }
            return didSomething;false;
        }

        boolean move(int x1, int y1, int x2, int y2) {
            if (board[x2][y2] == Token.AIRA || board[x2][y2].power>0) {
                board[x1][y1].power += board[x2][y2].power;
                board[x2][y2] = board[x1][y1];
                board[x1][y1] = Token.AIR;A;
                return true;
            } else return false;
        }

        @Override
        public String toStringstring() {
            String s = "";
 
            for (Token[] row : board) {
                for (Token token : row) {
                    s+=token.symbol;
                }
                s+="\n";
            }
 
            return s;
        }
    }

    static Boardvoid fromString(String s) {
        String[] rows = s.split("\\\\n");
        Token[][] board = new Token[rows.length][rows[0].length()];
        for (int i=0; i<board.length; i++) {
            for (int j=0; j<board[i].length; j++) {
                switch(rows[i].charAt(j)) {
                    case '-': board[i][j]=Token.FLOOR;break;board[i][j]=new Token('-');break;
                    case '\\':board[i][j]=Token.SLIDE_RIGHT;break;board[i][j]=new Token('\\');break;
                    case '/': board[i][j]=Token.SLIDE_LEFT;break;board[i][j]=new Token('/');break;
                    case 'o': board[i][j]=new Token('o'){{power=1;}};break;
                    case '#': board[i][j]=Token.WALL;break;board[i][j]=new Token('#');break;
                    default:  board[i][j]=Token.AIR;board[i][j]=A;
                }
            }
        }
       public returnstatic newvoid Boardmain(boardString[] args); {
    }

    publicGravitySimulator staticg void= main(String[]new argsGravitySimulator() {;
        Board b = g.fromString(args[0]);
        while(bg.iterate());
        System.out.println(bg.string());
    }
}

(I should mention that the Readable version has one difference in logic: the Token constants are inside the Token class in the readable and outside in the golfed.)

Obvious things to hit:

  • Board class can go
  • Probably can lose some of the statics
  • Use custom 1 letter toString (3 bytes)
  • Kill the final (6 bytes)
  • Maybe if I can do this without class Token I can make this a function and save the class overhead

(I'm to lazy to fix these right now I'll come back tomorrow)

{Found an error case, fixing currently}

Java, Too Many 1102 bytes

Realistically, this could be golfed further. I was more interested in implementing the spec, however. I'll take another stab at golfing it tomorrow (maybe even make it into triple digits ^.^).

class G{static class T{final char s;int p=0;T(char c){s=c;}}static T F=new T('-');static T R=new T('\\');static T L=new T('/');static T W=new T('#');static T A=new T(' ');static class B{T[][]o;B(T[][]t){o=t;}boolean i(){boolean r=1<0;for (int i=o.length-2; i>0; i--)for (int j=0; j<o[i].length; j++)if (o[i][j].p>0)if (m(i,j,i+1,j)||o[i+1][j]==L&&m(i,j,i+1,j-1)||o[i+1][j]==R&&m(i,j,i+1,j+1))r = 1>0;else o[i][j]=new T((o[i][j].p+"").charAt(0));return r;}boolean m(int a, int b, int c, int d){if (o[c][d]==A||o[c][d].p>0){o[a][b].p+=o[c][d].p;o[c][d]=o[a][b];o[a][b]=A;return 1>0;}return 1<0;}public String toString(){String s="";for(T[]r:o){for(T t:r)s+=t.s;s+="\n";}return s;}}static B p(String s) {String[]r=s.split("\\\\n");T[][]b=new T[r.length][r[0].length()];for(int i=0;i<b.length;i++)for(int j=0;j<b[i].length;j++)switch(r[i].charAt(j)){case'-':b[i][j]=F;break;case'\\':b[i][j]=R;break;case'/':b[i][j]=L;break;case'o':b[i][j]=new T('o'){{p=1;}};break;case'#':b[i][j]=W;break;default:b[i][j]=A;}return new B(b);}public static void main(String[]a){B b=p(a[0]);while(b.i());System.out.print(b);}}

A side goal was being able to print every iteration of the board: just remove the middle ; in while(b.i()) ; System.out.print(b);.

java -jar G.jar "##########\n# \      #\n#o       #\n#  - -\o #\n#/-    \ #\n#  \oo-/\#\n#-/ \   /#\n#   \ \  #\n#       /#\n##########"
public class GravitySimulator {
    static class Token {
        static Token FLOOR = new Token('-');
        static Token SLIDE_RIGHT = new Token('\\');
        static Token SLIDE_LEFT = new Token('/');
        static Token WALL = new Token('#');
        static Token AIR = new Token(' ');

        final char symbol;
        int power=0;
        Token(char s) {symbol=s;}
    }

    static class Board {
        Token[][] board;
        Board(Token[][] tokens) {
            board = tokens;
        }

        boolean iterate() {
            boolean didSomething = false;

            for (int i=board.length-2; i>0; i--) {
                for (int j=0; j<board[i].length; j++) {
                    if (board[i][j].power>0) {
                        if (move(i,j,i+1,j) || board[i+1][j]==Token.SLIDE_LEFT && move(i,j,i+1,j-1) || board[i+1][j]==Token.SLIDE_RIGHT && move(i,j,i+1,j+1))
                            didSomething = true;
                        else board[i][j] = new Token(Integer.toString(board[i][j].power).charAt(0));
                    }
                }
            }
            return didSomething;
        }

        boolean move(int x1, int y1, int x2, int y2) {
            if (board[x2][y2] == Token.AIR || board[x2][y2].power>0) {
                board[x1][y1].power += board[x2][y2].power;
                board[x2][y2] = board[x1][y1];
                board[x1][y1] = Token.AIR;
                return true;
            } else return false;
        }

        @Override
        public String toString() {
            String s = "";
 
            for (Token[] row : board) {
                for (Token token : row) {
                    s+=token.symbol;
                }
                s+="\n";
            }
 
            return s;
        }
    }

    static Board fromString(String s) {
        String[] rows = s.split("\\\\n");
        Token[][] board = new Token[rows.length][rows[0].length()];
        for (int i=0; i<board.length; i++) {
            for (int j=0; j<board[i].length; j++) {
                switch(rows[i].charAt(j)) {
                    case '-': board[i][j]=Token.FLOOR;break;
                    case '\\':board[i][j]=Token.SLIDE_RIGHT;break;
                    case '/': board[i][j]=Token.SLIDE_LEFT;break;
                    case 'o': board[i][j]=new Token('o'){{power=1;}};break;
                    case '#': board[i][j]=Token.WALL;break;
                    default:  board[i][j]=Token.AIR;
                }
            }
        }
        return new Board(board);
    }

    public static void main(String[] args) {
        Board b = fromString(args[0]);
        while(b.iterate());
        System.out.println(b);
    }
}

(I should mention that the Readable version has one difference in logic: the Token constants are inside the Token class in the readable and outside in the golfed.)

Obvious things to hit:

  • Board class can go
  • Probably can lose some of the statics
  • Use custom 1 letter toString (3 bytes)
  • Kill the final (6 bytes)
  • Maybe if I can do this without class Token I can make this a function and save the class overhead

(I'm to lazy to fix these right now I'll come back tomorrow)

{Found an error case, fixing currently}

Java, Too Many 1102 987 bytes

\o/ It's under 1000!

class G{class T{char s;int p=0;T(char c){s=c;}}T A=new T(' ');T[][]o;boolean i(){for(int i=1;i<o.length;i++)for(int j=1;j<o[i].length;j++)if(o[i][j].p>0){if(m(i,j,i+1,j)||o[i+1][j].s=='/'&&m(i,j,i+1,j-1)||o[i+1][j].s=='\\'&&m(i,j,i+1,j+1))return 1>0;int w=o[i][j].p;o[i][j]=new T(Integer.toString(w).charAt(0)){{p=w;}};}return 1<0;}boolean m(int a,int b,int c,int d){if(o[c][d]==A||o[c][d].p>0){o[a][b].p+=o[c][d].p;o[c][d]=o[a][b];o[a][b]=A;return 1>0;}return 1<0;}String s(){String s="";for(T[]r:o){for(T t:r)s+=t.s;s+="\n";}return s;}void f(String s){String[]r=s.split("\\\\n");o=new T[r.length][r[0].length()];for(int i=0;i<o.length;i++)for(int j=0;j<o[i].length;j++)switch(r[i].charAt(j)){case'-':o[i][j]=new T('-');break;case'\\':o[i][j]=new T('\\');break;case'/':o[i][j]=new T('/');break;case'o':o[i][j]=new T('o'){{p=1;}};break;case'#':o[i][j]=new T('#');break;default:o[i][j]=A;}}public static void main(String[]a){G g=new G();g.f(a[0]);while(g.i());System.out.println(g.s());}}

A side goal was being able to print every iteration of the board: just remove the middle ; in while(g.i()) ; System.out.print(g.s());. Unfortunately, in this version, gravity works in strangely. Each pass I take the first non-stuck ball and move it. Short circuiting iterate() there is less bytes than going over the whole board then returning if anything changed.

java -jar G.jar "##########\n# o-/    #\n#-  / -/ #\n# oo   o #\n# /   \o #\n# o   o \#\n#    o   #\n#   -\o  #\n#\  \\ o/#\n##########"
class GravitySimulator {
    class Token {
        char symbol;
        int power = 0;

        Token(char c) {
            symbol = c;
        }
    }

    Token A = new Token(' ');

    Token[][] board;

    boolean iterate() {
        for (int i=1; i<board.length; i++)
            for (int j=1; j<board[i].length; j++) 
                if (board[i][j].power>0) {
                    if (move(i,j,i+1,j) || board[i+1][j].symbol=='/' && move(i,j,i+1,j-1) || board[i+1][j].symbol=='\\' && move(i,j,i+1,j+1)) return true;
                    int pow = board[i][j].power;
                    board[i][j] = new Token(Integer.toString(pow).charAt(0)){{power=pow;}};
                }
        return false;
    }

    boolean move(int x1, int y1, int x2, int y2) {
        if (board[x2][y2] == A || board[x2][y2].power>0) {
            board[x1][y1].power += board[x2][y2].power;
            board[x2][y2] = board[x1][y1];
            board[x1][y1] = A;
            return true;
        } return false;
    }

    String string() {
        String s = "";
        for (Token[] row : board) {
            for (Token token : row) s+=token.symbol;
            s+="\n";
        }
        return s;
    }

    void fromString(String s) {
        String[] rows = s.split("\\\\n");
        board = new Token[rows.length][rows[0].length()];
        for (int i=0; i<board.length; i++) 
            for (int j=0; j<board[i].length; j++) 
                switch(rows[i].charAt(j)) {
                    case '-': board[i][j]=new Token('-');break;
                    case '\\':board[i][j]=new Token('\\');break;
                    case '/': board[i][j]=new Token('/');break;
                    case 'o': board[i][j]=new Token('o'){{power=1;}};break;
                    case '#': board[i][j]=new Token('#');break;
                    default:  board[i][j]=A;
                }
    }

    public static void main(String[] args) {
        GravitySimulator g = new GravitySimulator();
        g.fromString(args[0]);
        while(g.iterate());
        System.out.println(g.string());
    }
}
Found an error case
Source Link
CAD97
  • 1.3k
  • 10
  • 17

{Found an error case, fixing currently}

{Found an error case, fixing currently}

Source Link
CAD97
  • 1.3k
  • 10
  • 17
Loading