Advertisement
Guest User

Untitled

a guest
Dec 29th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. package Advent2019;
  2.  
  3. import util.AdventOfCode;
  4. import util.BitUtils;
  5.  
  6. import java.util.Arrays;
  7. import java.util.HashSet;
  8. import java.util.List;
  9. import java.util.Set;
  10.  
  11. public class Day24 extends AdventOfCode {
  12.  
  13.     int bugs;
  14.     int moreBugs[];
  15.  
  16.     public Day24(List<String> input) {
  17.         super(input);
  18.     }
  19.  
  20.     int countAdj(int i) {
  21.         int row = i / 5;
  22.         int col = i % 5;
  23.         int count = 0;
  24.         if (row > 0) if (BitUtils.getBit(bugs, i - 5)) count++;
  25.         if (row < 4) if (BitUtils.getBit(bugs, i + 5)) count++;
  26.         if (col > 0) if (BitUtils.getBit(bugs, i - 1)) count++;
  27.         if (col < 4) if (BitUtils.getBit(bugs, i + 1)) count++;
  28.         return count;
  29.     }
  30.  
  31.     int countAdjPart2(int i, int lvl) {
  32.         int row = i / 5;
  33.         int col = i % 5;
  34.         int count = 0;
  35.         if (row > 0)  {
  36.             if (BitUtils.getBit(moreBugs[lvl], i - 5)) count++;
  37.         } else {
  38.             if (BitUtils.getBit(moreBugs[lvl - 1], 7)) count++;
  39.         }
  40.         if (row < 4) {
  41.             if (BitUtils.getBit(moreBugs[lvl], i + 5)) count++;
  42.         } else {
  43.             if (BitUtils.getBit(moreBugs[lvl - 1], 17)) count++;
  44.         }
  45.         if (col > 0)  {
  46.             if (BitUtils.getBit(moreBugs[lvl], i - 1)) count++;
  47.         } else {
  48.             if (BitUtils.getBit(moreBugs[lvl - 1], 11)) count++;
  49.         }
  50.         if (col < 4) {
  51.             if (BitUtils.getBit(moreBugs[lvl], i + 1)) count++;
  52.         } else {
  53.             if (BitUtils.getBit(moreBugs[lvl - 1], 13)) count++;
  54.         }
  55.         if (i == 7) {
  56.             for (int j = 0; j < 5; j++) {
  57.                 if (BitUtils.getBit(moreBugs[lvl + 1], j)) count++;
  58.             }
  59.         }
  60.         if (i == 11) {
  61.             for (int j = 0; j < 25; j+= 5) {
  62.                 if (BitUtils.getBit(moreBugs[lvl + 1], j)) count++;
  63.             }
  64.         }
  65.         if (i == 13) {
  66.             for (int j = 4; j < 25; j+= 5) {
  67.                 if (BitUtils.getBit(moreBugs[lvl + 1], j)) count++;
  68.             }
  69.         }
  70.         if (i == 17) {
  71.             for (int j = 20; j < 25; j++) {
  72.                 if (BitUtils.getBit(moreBugs[lvl + 1], j)) count++;
  73.             }
  74.         }
  75.         return count;
  76.     }
  77.     @Override
  78.     public Object part1() {
  79.         Set<Integer> seen = new HashSet<>();
  80.         while (true) {
  81.             int nextgen = 0;
  82.             for (int i = 0; i < 25; i++) {
  83.                 int count = countAdj(i);
  84.                 if (BitUtils.getBit(bugs, i)) {
  85.                     if (count == 1) nextgen = BitUtils.setBit(nextgen, i);
  86.                 } else {
  87.                     if (count > 0 && count < 3) nextgen = BitUtils.setBit(nextgen, i);
  88.                 }
  89.             }
  90.             bugs = nextgen;
  91.             if (!seen.add(bugs)) return bugs;
  92.         }
  93.     }
  94.  
  95.     @Override
  96.     public Object part2() {
  97.         parse();
  98.         moreBugs = new int[205];
  99.         int iterations = 200;
  100.         moreBugs[102] = bugs;
  101.         for (int i = 0; i < iterations; i++) {
  102.             int[] nextgen = new int[205];
  103.             for (int j = 1; j < 204; j++) {
  104.                 for (int k = 0; k < 25; k++) {
  105.                     if (k != 12) {
  106.                         int count = countAdjPart2(k, j);
  107.                         if (BitUtils.getBit(moreBugs[j], k)) {
  108.                             if (count == 1) nextgen[j] = BitUtils.setBit(nextgen[j], k);
  109.                         } else {
  110.                             if (count > 0 && count < 3) nextgen[j] = BitUtils.setBit(nextgen[j], k);
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.             moreBugs = nextgen;
  116.         }
  117.  
  118.         return Arrays.stream(moreBugs)
  119.                 .map(BitUtils::countBits)
  120.                 .sum();
  121.     }
  122.  
  123.  
  124.     @Override
  125.     public void parse() {
  126.         bugs = 0;
  127.         for (int i = 0; i < input.size(); i++) {
  128.             for (int j = 0; j < input.get(i).length(); j++) {
  129.                 if  (input.get(i).charAt(j) == '#') {
  130.                     bugs |= 1 << (i * 5) + j;
  131.                 }
  132.             }
  133.         }
  134.  
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement