Skip to content

Instantly share code, notes, and snippets.

@Drullkus
Last active January 6, 2020 21:01
Show Gist options
  • Save Drullkus/6082500acaa51a2e4f0589be06dc52f0 to your computer and use it in GitHub Desktop.
Save Drullkus/6082500acaa51a2e4f0589be06dc52f0 to your computer and use it in GitHub Desktop.
import java.util.Random;
public class Connectome {
private final int width, height;
private final byte[] mapping;
public Connectome(int width, int height) {
this.width = width;
this.height = height;
this.mapping = new byte[width * height + 1 >> 1];
System.out.println(String.format("Size %s %s with length %s", this.width, this.height, this.mapping.length));
generate();
}
public void generate() {
new Random().nextBytes(mapping);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
for (Direction direction : Direction.values()) {
int xD = x + direction.X_OFFSET;
int yD = y + direction.Y_OFFSET;
if (xD >= 0
&& xD < width
&& yD >= 0
&& yD < height) {
if (direction.testFor(getData(x, y))) {
// If there is connection in a direction, connect the adjacent piece
orData(xD, yD, direction.OPPOSITE);
} else {
// Otherwise completely sever it
andData(x, y, direction.INVERTED);
andData(xD, yD, direction.INVERTED_OPPOSITE);
}
}
}
}
}
}
private void orData(int x, int y, byte data) {
data &= 0b1111;
data <<= ((x + y) & 1) << 2;
mapping[getSingleCoordFromDouble(x, y)] |= data;
}
private void andData(int x, int y, byte data) {
data &= 0b1111;
data <<= ((x + y) & 1) << 2;
data |= 0b1111 << (((x ^ 1) + y & 1) << 2);
mapping[getSingleCoordFromDouble(x, y)] &= data;
}
private byte getData(int x, int y) {
byte b = mapping[getSingleCoordFromDouble(x, y)];
b >>= ((x + y) & 1) << 2;
b &= 0b1111;
return b;
}
private int getSingleCoordFromDouble(int x, int y) {
return x + y * width >> 1;
}
public String visualize() {
StringBuilder maze = new StringBuilder();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
maze.append(Main.getStringFromFacings(getData(x, y)));
}
maze.append('\n');
}
return maze.toString();
}
public enum Direction {
EAST ((byte) 0b0001, 1, 0),
SOUTH((byte) 0b0010, 0, 1),
WEST ((byte) 0b0100, -1, 0),
NORTH((byte) 0b1000, 0, -1);
public final byte BYTE;
public final byte OPPOSITE;
public final byte INVERTED;
public final byte INVERTED_OPPOSITE;
public final int X_OFFSET;
public final int Y_OFFSET;
Direction(byte bite, int xOffset, int yOffset) {
this.BYTE = bite;
this.OPPOSITE = (byte) (bite >>> 2 | bite << 2);
this.INVERTED = (byte) ~this.BYTE;
this.INVERTED_OPPOSITE = (byte) ~this.OPPOSITE;
this.X_OFFSET = xOffset;
this.Y_OFFSET = yOffset;
}
protected boolean testFor(byte directions) {
return (this.BYTE & directions) == this.BYTE;
}
}
}
public class Main {
public static void main(String... args) {
for (int height = 3; height < 16; height+=4) {
for (int width = 3; width < 16; width+=4) {
System.out.println((new Connectome(width, height)).visualize());
}
}
}
// These util methods are placed here because otherwise it gets lonely in a third class :(
static String getPaddedBitString(byte b) {
return String.format("%1$4s", Integer.toBinaryString(b & 0xF)).replace(' ', '0');
}
static String getStringFromFacings(byte directions) {
switch (directions & 0b1111) {
case 0b0010:
return " ╷";
case 0b0001:
return " ╶";
case 0b1000:
return " ╵";
case 0b0100:
return "─╴";
case 0b1001:
return " └";
case 0b1100:
return "─┘";
case 0b0110:
return "─┐";
case 0b0011:
return " ┌";
case 0b1101:
return "─┴";
case 0b1110:
return "─┤";
case 0b0111:
return "─┬";
case 0b1011:
return " ├";
case 0b1010:
return " │";
case 0b0101:
return "──";
case 0b1111:
return "─┼";
default:
return " •";
}
}
}
Size 3 3 with length 5
─┤ ╷ ╶
─┘ ├──
┌─┤ •
Size 7 3 with length 11
─┐ ├───┬─┴─┬─╴
─┘ └─┬─┘ ╷ ╵ ┌
─────┴─╴ │ • ╵
Size 11 3 with length 17
└─┐ ├─┐ ┌─┤ │ ├───╴ ╶
• └─┘ │ │ │ │ ├─┐ • ┌
• ╷ ┌─┤ └─┼─┤ │ └─┬─┼
Size 15 3 with length 23
─┘ • │ └─┤ ╶─┤ ╵ ┌─┴─┤ ├─╴ ╵ ╷
• ╶─┤ ┌─┴─╴ └─┐ │ ╷ └─┘ ╷ • │
╷ • ╵ ├─┬─┬─╴ └─┤ ├───┬─┤ ╶─┤
Size 3 7 with length 11
───╴ ╶
• • •
─┬───╴
─┘ ╷ ╶
╶─┘ •
╶─┬─┐
╷ └─┼
Size 7 7 with length 25
─┼─────┤ │ ├──
─┘ ┌─┬─┼─┼─┘ ╷
─╴ └─┤ ├─┘ • │
╶─┬─┘ │ • ╶─┴
─┐ ├─┐ ├───┐ •
─┘ ╵ │ └─╴ ├─┐
─┐ • │ • ╶─┴─┘
Size 11 7 with length 39
└─┤ │ ╷ • └─╴ ╷ ╷ ╶─┘
• │ └─┤ ┌───╴ ╵ └───╴
─┬─┘ • │ ├───╴ • ╶─╴ •
├─╴ • ╵ ╵ ┌─┐ • ╶───┐
└─┬─┬─┐ ┌─┼─┤ ┌─╴ ╶─┤
• ├─┼─┴─┼─┘ └─┤ ┌─╴ └
───┼─┘ ┌─┴─╴ • │ └───┬
Size 15 7 with length 53
╵ ┌───┼─┬─┬─╴ • ╷ ╶─┼─┬───╴ ┌
• │ • └─┴─┴───╴ ╵ ╷ └─┼─┬─╴ ├
─┬─┼─╴ ╷ ╶─┬─┬───┐ │ ╶─┤ └─┐ └
─┴─┼─┐ ├─╴ │ │ ╶─┼─┼───┼─┬─┘ •
─╴ └─┼─┴─┐ ├─┼─┐ ╵ ╵ • ╵ └─┐ ╶
─┬─╴ ├───┴─┘ ╵ └───┐ ┌───╴ │ •
─┼─┬─┼─┐ ┌─╴ ┌─┐ ╶─┼─┤ • • ╵ •
Size 3 11 with length 17
├─┐ ├
│ ├─┼
│ └─┤
│ ╷ └
─┼─┤ ╷
─┴─┴─┤
╶───┘
╷ ╶──
╵ ┌──
• └──
──────
Size 7 11 with length 39
╵ ┌─┘ ├─┴─┴─╴
╶─┤ ┌─┴───┐ •
╷ ├─┼─┐ • ├──
─┤ │ │ ├─┬─┼─┐
└─┼─┤ └─┴─┴─┤
╷ │ ╵ ╶─╴ ╶─┤
├─┼─┬─┐ ┌─╴ │
─┘ └─┼─┤ │ ╶─┼
╶─┬─┼─┘ ├───┤
╷ │ ├───┤ ┌─┼
─┤ ╵ │ ╶─┘ ├─┼
Size 11 11 with length 61
╵ │ ├─┬─┬─┼─┘ ┌─┘ └─┤
───┤ ├─┼─┼─┘ ╶─┴─┐ ╷ ╵
┌─┼─┘ ├─┴─┬─────┴─┤ ┌
│ │ ╶─┤ ┌─┼─╴ ┌─┬─┴─┘
╵ ├─┐ ├─┴─┴─┐ └─┘ ╶──
─╴ │ │ │ ┌───┼─┐ ╶────
╷ │ ╵ ╵ ╵ ╷ ╵ └─────╴
│ ├─┬─┐ ╷ │ • • • ╶──
│ ╵ ├─┼─┴─┤ ╷ ╶───╴ ╶
─┴───┤ │ ╶─┘ │ ╷ ╷ ╶─╴
╶─┐ │ ╵ • • │ ├─┘ ┌──
Size 15 11 with length 83
┌─┬─┴─┬─┐ └─┤ ╶─╴ ╶─┘ └─┐ ╷ ┌
─┤ │ ┌─┤ └─╴ ├─╴ ┌─╴ ╷ ╶─┤ ╵ └
─┤ └─┼─┼─┬─┬─┤ ╶─┼─┬─┘ ╷ ╵ ╶─╴
─┤ ╶─┤ └─┘ ╵ │ ┌─┘ ├─┬─┤ ╶─┬─┐
└─┐ │ • • ╶─┤ │ ╶─┴─┴─┴─┐ └─┼
╷ └─┤ ┌─┐ ┌─┤ │ ╷ • • ╶─┘ ╶─┤
─┘ ┌─┼─┘ └─┴─┘ ├─┴─╴ ┌─┐ ╷ ┌─┘
─┬─┤ ├─╴ ╷ ╶─┐ ├─┬───┼─┤ ╵ │ ┌
├─┤ └─┐ ╵ • └─┤ │ ╶─┘ ├─╴ └─┼
─┘ │ ╶─┼─┐ ┌───┼─┤ • • └───╴ │
• ╵ ╶─┤ ╵ ╵ • └─┼─┐ ╷ • ╶─┐ └
Size 3 15 with length 23
┌─┴─┬
─┤ ┌─┴
─┘ └─┬
┌─┐ └
├─┼─┬
│ ╵ ├
╵ ╷ ├
─┬─┴─┴
─┘ ┌─┐
• ╵ ├
─┬─╴ └
│ ┌─┬
╵ │ └
┌─┤ ╶
─┴─┼─┐
Size 7 15 with length 53
─┘ └─╴ • ┌─┴─╴
┌───┬─┬─┤ ╶─╴
─┤ ╶─┤ │ ├───╴
└─┬─┴─┤ ├─┬─┬
┌─┴─┐ ├─┼─┴─┤
└─┐ └─┴─┘ ╷ ├
┌─┼─┬───┐ ├─┘
─┴─┴─┘ ┌─┘ │ ╶
┌─╴ ╶─┼─╴ ╵ ┌
└─╴ ╶─┴───╴ └
─┐ ┌─┐ ╷ ╷ ╶─┐
─┘ └─┴─┤ ├─┐ ├
╶─┬─┐ │ ├─┼─┴
───┴─┘ ├─┴─┤ ╶
─┬─╴ ╶─┼─╴ ╵ ╷
Size 11 15 with length 83
─┘ • ├─╴ ┌─┘ ╷ ╷ ╵ │ ╶
─╴ • └─╴ ╵ ┌─┼─┴─┬─┘ ╷
╷ ┌─┐ ╷ ╷ ├─┘ • └─╴ ╵
╵ ╵ │ ╵ │ │ ╶─╴ ╶─┬──
╶─┬─┤ ╶─┼─┤ ╶─┐ ╷ ├──
┌─┘ ╵ ╷ │ ╵ ┌─┼─┴─┴─╴
─┘ ╶─┐ │ └─┐ ╵ │ ╶───╴
─┐ • ├─┤ ┌─┤ • ├─╴ • ╶
│ ┌─┼─┤ │ │ • │ • • •
│ ╵ ╵ │ │ ╵ ╷ ├─╴ ╷ ┌
─┘ ╶─┐ ├─┤ ┌─┤ └─╴ └─┘
─┐ • ╵ ├─┤ ├─┼─╴ • ┌─┐
├─────┴─┴─┼─┘ ┌─┬─┘ ╵
─┘ ┌─┐ ╶─┬─┤ • └─┘ ╶─┐
╷ │ └───┴─┤ ╷ • • ╶─┤
Size 15 15 with length 113
┌───┬───┤ ╷ └─┘ ├─┐ ┌─┼─┴─┘ ╶
├─╴ ╵ ╶─┤ ╵ • ╷ ╵ │ ├─┼─╴ • •
│ • • ╶─┴─┬───┘ ┌─┤ └─┼─┬─┐ ┌
─┼─╴ ╷ ╶───┤ ╶─┐ ├─┼─╴ │ ╵ ├─┼
─┘ • └─┬───┤ ╶─┤ └─┘ ╶─┼───┼─┘
╷ • ╶─┼───┤ ┌─┴───┬─┬─┘ ┌─┴─╴
─┴─╴ ┌─┤ • ╵ │ ╷ ╶─┘ ├─╴ ╵ ╶─┬
╷ • │ │ ╶───┼─┤ ╷ ╶─┴───╴ • │
├───┤ ╵ ╶───┘ ├─┴─┬─┬─┐ • ╶─┤
│ • │ ┌─┬─╴ ╶─┴─┬─┼─┤ │ ╷ ╷ └
─┼─┬─┼─┼─┘ ╷ ┌─┬─┴─┘ ├─┤ │ │ ╷
├─┼─┼─┴───┤ └─┼─╴ ┌─┼─┤ ╵ ├─┼
─┼─┘ │ ┌─╴ │ ╷ ├─╴ ├─┼─┴─╴ │ │
─┴─┐ │ └─┬─┴─┤ │ ┌─┴─┼─┐ • └─┴
╷ ╵ └─┬─┘ ┌─┘ └─┤ ╶─┼─┤ ┌─╴ ┌
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment