Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2016 19:58
Show Gist options
  • Save anonymous/d00e04363b21d2ad73c6f6178a2fcd15 to your computer and use it in GitHub Desktop.
Save anonymous/d00e04363b21d2ad73c6f6178a2fcd15 to your computer and use it in GitHub Desktop.
Advent of Code 2016 Day 18 /u/Philboyd_Studge
package snarkbait.reddit;
import java.math.BigInteger;
import java.util.Arrays;
/**
* @author /u/Philboyd_Studge on 12/18/2016.
*/
public class Advent2016_18 {
static final int ROWS = 40; // part = 400000
public static void main(String[] args) {
String input = "(input here)";
//String input = ".^^.^.^^^^";
final int len = input.length();
BigInteger[] output = new BigInteger[ROWS];
input = input.replace('.', '0');
input = input.replace('^', '1');
System.out.println(input);
output[0] = new BigInteger(input, 2);
for (int i = 0; i < ROWS - 1; i++) {
output[i + 1] = BigInteger.ZERO;
for (int j = len - 1; j >= 0; j--) {
boolean left = output[i].testBit(j + 1);
boolean right = j != 0 && output[i].testBit(j - 1);
if (left ^ right) output[i + 1] = output[i + 1].setBit(j);
}
}
int count = Arrays.asList(output).stream()
.mapToInt(BigInteger::bitCount)
.map(x -> len - x)
.sum();
System.out.println(count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment