Guest User

Untitled

a guest
Dec 18th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import java.util.List;
  2.  
  3. /**
  4.  * @author /u/Philboyd_Studge on 12/17/2015.
  5.  */
  6. public class Advent18 {
  7.  
  8.     static boolean[][] grid = new boolean[100][100];
  9.  
  10.     static boolean part2 = false;
  11.  
  12.  
  13.     public static int countNeighbors(int x, int y) {
  14.         int count = 0;
  15.         for (int i = x > 0 ? -1 : 0; i < (x < 99 ? 2 : 1); i++) {
  16.             for (int j = y > 0 ? -1 : 0; j < (y < 99 ? 2 : 1); j++) {
  17.                 if (!(i == 0 && j == 0) && grid[x + i][y + j]) count++;
  18.             }
  19.         }
  20.         return count;
  21.     }
  22.  
  23.     public static int countLightsOn() {
  24.         int count = 0;
  25.         for (boolean[] each : grid) {
  26.             for (boolean every : each) {
  27.                 if (every) count++;
  28.             }
  29.  
  30.         }
  31.         return count;
  32.     }
  33.  
  34.     public static void tick() {
  35.         boolean[][] newGrid = new boolean[100][100];
  36.         for (int i = 0; i < 100; i++) {
  37.             for (int j = 0; j < 100; j++) {
  38.                 int neighbors = countNeighbors(i, j);
  39.                 if (grid[i][j] && (neighbors < 2 || neighbors > 3)) {
  40.                     newGrid[i][j] = false;
  41.                 } else {
  42.                     if (neighbors == 3) {
  43.                         newGrid[i][j] = true;
  44.                     } else {
  45.                         newGrid[i][j] = grid[i][j];
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.         if (part2) {
  51.             newGrid[0][0] = true;
  52.             newGrid[0][99] = true;
  53.             newGrid[99][99] = true;
  54.             newGrid[99][0] = true;
  55.         }
  56.         grid = newGrid;
  57.     }
  58.     public static void main(String[] args) {
  59.         //boolean[][] grid = new boolean[100][100];
  60.  
  61.         List<String> input = FileIO.getFileAsList("advent18.txt");
  62.  
  63.         for (int i = 0; i < 100; i++) {
  64.             String line = input.get(i);
  65.             for (int j = 0; j < 100; j++) {
  66.                 grid[i][j] = line.charAt(j)=='#';
  67.             }
  68.         }
  69.  
  70.         // hack
  71.         if (part2) grid[99][0] = true;
  72.  
  73.         for (int i = 0; i < 100; i++) tick();
  74.         System.out.println(countLightsOn());
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment