Created
December 15, 2017 05:48
-
-
Save snarkbait/d6ea07113cd3d51f42464e7bc6e3af44 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Advent2017; | |
import util.AdventOfCode; | |
import java.util.List; | |
public class Day15 extends AdventOfCode { | |
Generator A; | |
Generator B; | |
class Generator { | |
int factor; | |
long last; | |
public Generator(int seed, int factor) { last = seed; this.factor = factor; } | |
public int next() { | |
last = (last * factor) % Integer.MAX_VALUE; | |
return (int) last; | |
} | |
int next4() { | |
int result = next(); | |
while (last % 4 != 0) { | |
result = next(); | |
} | |
return result; | |
} | |
int next8() { | |
int result = next(); | |
while (last % 8 != 0) { | |
result = next(); | |
} | |
return result; | |
} | |
} | |
public Day15(List<String> input) { | |
super(input); | |
} | |
@Override | |
public Object part1() { | |
int matches = 0; | |
for (int i = 0; i < 40000000; i++) { | |
if ((A.next() & 0xFFFF) == (B.next() & 0xFFFF)) matches++; | |
} | |
return matches; | |
} | |
@Override | |
public Object part2() { | |
parse(); | |
int matches = 0; | |
for (int i = 0; i < 5000000; i++) { | |
if ((A.next4() & 0xFFFF) == (B.next8() & 0xFFFF)) { | |
matches++; | |
} | |
} | |
return matches; | |
} | |
@Override | |
public void parse() { | |
A = new Generator(512, 16807); | |
B = new Generator(191, 48271); | |
} | |
@Override | |
public void run() { | |
super.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment