Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- /**
- * @author /u/Philboyd_Studge on 12/17/2015.
- */
- public class Advent18 {
- static boolean[][] grid = new boolean[100][100];
- static boolean part2 = false;
- public static int countNeighbors(int x, int y) {
- int count = 0;
- for (int i = x > 0 ? -1 : 0; i < (x < 99 ? 2 : 1); i++) {
- for (int j = y > 0 ? -1 : 0; j < (y < 99 ? 2 : 1); j++) {
- if (!(i == 0 && j == 0) && grid[x + i][y + j]) count++;
- }
- }
- return count;
- }
- public static int countLightsOn() {
- int count = 0;
- for (boolean[] each : grid) {
- for (boolean every : each) {
- if (every) count++;
- }
- }
- return count;
- }
- public static void tick() {
- boolean[][] newGrid = new boolean[100][100];
- for (int i = 0; i < 100; i++) {
- for (int j = 0; j < 100; j++) {
- int neighbors = countNeighbors(i, j);
- if (grid[i][j] && (neighbors < 2 || neighbors > 3)) {
- newGrid[i][j] = false;
- } else {
- if (neighbors == 3) {
- newGrid[i][j] = true;
- } else {
- newGrid[i][j] = grid[i][j];
- }
- }
- }
- }
- if (part2) {
- newGrid[0][0] = true;
- newGrid[0][99] = true;
- newGrid[99][99] = true;
- newGrid[99][0] = true;
- }
- grid = newGrid;
- }
- public static void main(String[] args) {
- //boolean[][] grid = new boolean[100][100];
- List<String> input = FileIO.getFileAsList("advent18.txt");
- for (int i = 0; i < 100; i++) {
- String line = input.get(i);
- for (int j = 0; j < 100; j++) {
- grid[i][j] = line.charAt(j)=='#';
- }
- }
- // hack
- if (part2) grid[99][0] = true;
- for (int i = 0; i < 100; i++) tick();
- System.out.println(countLightsOn());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment