Skip to content

Instantly share code, notes, and snippets.

@tomwhoiscontrary
Created May 15, 2025 18:33
Show Gist options
  • Save tomwhoiscontrary/9c3828af83166ddf832f27608d3826a3 to your computer and use it in GitHub Desktop.
Save tomwhoiscontrary/9c3828af83166ddf832f27608d3826a3 to your computer and use it in GitHub Desktop.
import java.util.Objects;
public class PlaceSanctuaries {
public static void main(String[] args) {
// 0 means this tile cannot be either a sanctuary or a forest (outside borders, water, mountains, cities, already built on, etc)
// n means this can be either city n's sanctuary, or a forest
Grid grid = new Grid(new int[][]{
{0, 0, 1, 0, 2, 2, 2, 0},
{0, 0, 1, 0, 0, 2, 2, 2},
{1, 0, 0, 0, 2, 0, 0, 0},
{0, 0, 1, 1, 2, 2, 0, 2},
{3, 3, 3, 3, 4, 0, 4, 0},
{0, 0, 0, 0, 4, 0, 4, 4},
{3, 0, 3, 3, 0, 0, 4, 0},
{0, 0, 0, 0, 4, 0, 0, 0}});
int count = 0;
int bestScore = 0;
int bestX1 = 0;
int bestY1 = 0;
int bestX2 = 0;
int bestY2 = 0;
int bestX3 = 0;
int bestY3 = 0;
int bestX4 = 0;
int bestY4 = 0;
for (int x1 = 0; x1 < grid.width(); x1++) {
for (int y1 = 0; y1 < grid.width(); y1++) {
if (grid.get(x1, y1) != 1) continue;
grid.set(x1, y1, 0);
for (int x2 = 0; x2 < grid.width(); x2++) {
for (int y2 = 0; y2 < grid.width(); y2++) {
if (grid.get(x2, y2) != 2) continue;
grid.set(x2, y2, 0);
for (int x3 = 0; x3 < grid.width(); x3++) {
for (int y3 = 0; y3 < grid.width(); y3++) {
if (grid.get(x3, y3) != 3) continue;
grid.set(x3, y3, 0);
for (int x4 = 0; x4 < grid.width(); x4++) {
for (int y4 = 0; y4 < grid.width(); y4++) {
if (grid.get(x4, y4) != 4) continue;
grid.set(x4, y4, 0);
count++;
int score = grid.evaluate(x1, y1) + grid.evaluate(x2, y2) + grid.evaluate(x3, y3) + grid.evaluate(x4, y4);
if (score > bestScore) {
bestScore = score;
bestX1 = x1;
bestY1 = y1;
bestX2 = x2;
bestY2 = y2;
bestX3 = x3;
bestY3 = y3;
bestX4 = x4;
bestY4 = y4;
}
grid.set(x4, y4, 4);
}
}
grid.set(x3, y3, 3);
}
}
grid.set(x2, y2, 2);
}
}
grid.set(x1, y1, 1);
}
}
System.out.println("count = " + count);
System.out.println("best score = " + bestScore);
grid.set(bestX1, bestY1, 0);
grid.set(bestX2, bestY2, 0);
grid.set(bestX3, bestY3, 0);
grid.set(bestX4, bestY4, 0);
int score1 = grid.evaluate(bestX1, bestY1);
int score2 = grid.evaluate(bestX2, bestY2);
int score3 = grid.evaluate(bestX3, bestY3);
int score4 = grid.evaluate(bestX4, bestY4);
System.out.println("best 1 = " + bestX1 + "," + bestY1 + " = " + score1);
System.out.println("best 2 = " + bestX2 + "," + bestY2 + " = " + score2);
System.out.println("best 3 = " + bestX3 + "," + bestY3 + " = " + score3);
System.out.println("best 4 = " + bestX4 + "," + bestY4 + " = " + score4);
System.out.println("total = " + (score1 + score2 + score3 + score4));
}
record Grid(int width, int height, int[] tiles) {
public Grid(int[][] tiles) {
this(tiles[0].length, tiles.length, flatten(tiles));
}
private static int[] flatten(int[][] tiles) {
int[] flat = new int[tiles.length * tiles[0].length];
for (int y = 0; y < tiles.length; y++) {
System.arraycopy(tiles[y], 0, flat, y * tiles[0].length, tiles[0].length);
}
return flat;
}
int get(int x, int y) {
return tiles[index(x, y)];
}
void set(int x, int y, int value) {
tiles[index(x, y)] = value;
}
int index(int x, int y) {
return Objects.checkIndex(y, height) * width + Objects.checkIndex(x, width);
}
int evaluate(int x, int y) {
int sum = 0;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue;
int i;
try {
i = get(x + dx, y + dy);
} catch (IndexOutOfBoundsException e) { // lol
i = 0;
}
if (i != 0) sum += 1;
}
}
return sum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment