Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Created December 14, 2017 08:35
Show Gist options
  • Save snarkbait/6b69d25688895d566c832e4bfb18d9d3 to your computer and use it in GitHub Desktop.
Save snarkbait/6b69d25688895d566c832e4bfb18d9d3 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 14
package Advent2017;
import util.*;
import java.math.BigInteger;
import java.util.List;
public class Day14 extends AdventOfCode{
private BigInteger[] hashes;
private boolean[][] disk = new boolean[128][128];
private String key;
private boolean findGroup(int x, int y) {
if (!Direction.rangeCheck(x, y, 128)) return false;
if (disk[y][x]) {
disk[y][x] = false;
for (Direction dir : Direction.values()) {
findGroup(x + dir.getDx(), y + dir.getDy());
}
return true;
}
return false;
}
public Day14(List<String> input) {
super(input);
}
@Override
public Object part1() {
int count = 0;
hashes = new BigInteger[128];
for (int i = 0; i < 128; i++) {
Day10 knotHash = new Day10(input.get(0) + "-" + i);
hashes[i] = new BigInteger(knotHash.part2(), 16);
count += hashes[i].bitCount();
}
return count;
}
@Override
public Object part2() {
// make matrix
int y = 0;
for (BigInteger row : hashes) {
for (int x = 0; x < 128; x++) {
if (row.testBit(x)) {
disk[y][x] = true;
}
}
y++;
}
// find groups
int groups = 0;
for (int i = 0; i < 128; i++) {
for (int j = 0; j < 128; j++) {
if (findGroup(j, i)) groups++;
}
}
return groups;
}
@Override
public void parse() {
key = input.get(0);
}
@Override
public void run() {
super.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment