Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advent2019;
- import util.AdventOfCode;
- import util.BitUtils;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- public class Day24 extends AdventOfCode {
- int bugs;
- int moreBugs[];
- public Day24(List<String> input) {
- super(input);
- }
- int countAdj(int i) {
- int row = i / 5;
- int col = i % 5;
- int count = 0;
- if (row > 0) if (BitUtils.getBit(bugs, i - 5)) count++;
- if (row < 4) if (BitUtils.getBit(bugs, i + 5)) count++;
- if (col > 0) if (BitUtils.getBit(bugs, i - 1)) count++;
- if (col < 4) if (BitUtils.getBit(bugs, i + 1)) count++;
- return count;
- }
- int countAdjPart2(int i, int lvl) {
- int row = i / 5;
- int col = i % 5;
- int count = 0;
- if (row > 0) {
- if (BitUtils.getBit(moreBugs[lvl], i - 5)) count++;
- } else {
- if (BitUtils.getBit(moreBugs[lvl - 1], 7)) count++;
- }
- if (row < 4) {
- if (BitUtils.getBit(moreBugs[lvl], i + 5)) count++;
- } else {
- if (BitUtils.getBit(moreBugs[lvl - 1], 17)) count++;
- }
- if (col > 0) {
- if (BitUtils.getBit(moreBugs[lvl], i - 1)) count++;
- } else {
- if (BitUtils.getBit(moreBugs[lvl - 1], 11)) count++;
- }
- if (col < 4) {
- if (BitUtils.getBit(moreBugs[lvl], i + 1)) count++;
- } else {
- if (BitUtils.getBit(moreBugs[lvl - 1], 13)) count++;
- }
- if (i == 7) {
- for (int j = 0; j < 5; j++) {
- if (BitUtils.getBit(moreBugs[lvl + 1], j)) count++;
- }
- }
- if (i == 11) {
- for (int j = 0; j < 25; j+= 5) {
- if (BitUtils.getBit(moreBugs[lvl + 1], j)) count++;
- }
- }
- if (i == 13) {
- for (int j = 4; j < 25; j+= 5) {
- if (BitUtils.getBit(moreBugs[lvl + 1], j)) count++;
- }
- }
- if (i == 17) {
- for (int j = 20; j < 25; j++) {
- if (BitUtils.getBit(moreBugs[lvl + 1], j)) count++;
- }
- }
- return count;
- }
- @Override
- public Object part1() {
- Set<Integer> seen = new HashSet<>();
- while (true) {
- int nextgen = 0;
- for (int i = 0; i < 25; i++) {
- int count = countAdj(i);
- if (BitUtils.getBit(bugs, i)) {
- if (count == 1) nextgen = BitUtils.setBit(nextgen, i);
- } else {
- if (count > 0 && count < 3) nextgen = BitUtils.setBit(nextgen, i);
- }
- }
- bugs = nextgen;
- if (!seen.add(bugs)) return bugs;
- }
- }
- @Override
- public Object part2() {
- parse();
- moreBugs = new int[205];
- int iterations = 200;
- moreBugs[102] = bugs;
- for (int i = 0; i < iterations; i++) {
- int[] nextgen = new int[205];
- for (int j = 1; j < 204; j++) {
- for (int k = 0; k < 25; k++) {
- if (k != 12) {
- int count = countAdjPart2(k, j);
- if (BitUtils.getBit(moreBugs[j], k)) {
- if (count == 1) nextgen[j] = BitUtils.setBit(nextgen[j], k);
- } else {
- if (count > 0 && count < 3) nextgen[j] = BitUtils.setBit(nextgen[j], k);
- }
- }
- }
- }
- moreBugs = nextgen;
- }
- return Arrays.stream(moreBugs)
- .map(BitUtils::countBits)
- .sum();
- }
- @Override
- public void parse() {
- bugs = 0;
- for (int i = 0; i < input.size(); i++) {
- for (int j = 0; j < input.get(i).length(); j++) {
- if (input.get(i).charAt(j) == '#') {
- bugs |= 1 << (i * 5) + j;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement