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
static
s
- 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}