Skip to content

Instantly share code, notes, and snippets.

@paulhoadley
Last active January 11, 2016 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulhoadley/47f913dac07a621d1ca0 to your computer and use it in GitHub Desktop.
Save paulhoadley/47f913dac07a621d1ca0 to your computer and use it in GitHub Desktop.
package net.logicsquad.advent;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class Day18 {
public static void main(String[] args) throws IOException {
G g = new G(Files.readAllLines(Paths.get("etc/day18.input"), StandardCharsets.UTF_8));
for (int i = 0; i < 100; i++) g.t();
System.out.println("Day18.main: grid.lightsOn() = " + g.o());
}
public static class G {
private boolean[][] l = new boolean[100][100];
public G(List<String> p) {
for (int i = 0; i < 100; i++) for (int j = 0; j < 100; j++) if (p.get(i).charAt(j) == '#') l[i][j] = true;
}
private int n(int i, int j) {
int c = 0;
if (i - 1 > -1 && j - 1 > -1 && l[i - 1][j - 1]) c++;
if (j - 1 > -1 && l[i][j - 1]) c++;
if (i + 1 < 100 && j - 1 > -1 && l[i + 1][j - 1]) c++;
if (i - 1 > -1 && l[i - 1][j]) c++;
if (i + 1 < 100 && l[i + 1][j]) c++;
if (i - 1 > -1 && j + 1 < 100 && l[i - 1][j + 1]) c++;
if (j + 1 < 100 && l[i][j + 1]) c++;
if (i + 1 < 100 && j + 1 < 100 && l[i + 1][j + 1]) c++;
return c;
}
public void t() {
boolean[][] x = new boolean[100][100];
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++) {
int a = n(i, j);
if (l[i][j]) {
if (a == 2 || a == 3)
x[i][j] = true;
} else {
if (a == 3)
x[i][j] = true;
}
}
l = x;
return;
}
public int o() {
int c = 0;
for (int i = 0; i < 100; i++) for (int j = 0; j < 100; j++) if (l[i][j]) c++;
return c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment