Created
December 10, 2015 06:37
-
-
Save desrtfx/92d3e82bd0f82a1d5387 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class Day10 { | |
public final static int ITERATIONS_PART_1 = 40; | |
public final static int ITERATIONS_PART_2 = 50; | |
public static void main(String[] args) { | |
String input = "1321131112"; | |
System.out.println("Part 1: " + runSequence(input, ITERATIONS_PART_1)); | |
System.out.println("Part 2: " + runSequence(input, ITERATIONS_PART_2)); | |
} | |
public static String processString(String input) { | |
StringBuilder sb = new StringBuilder(); | |
int pos = 1; | |
int count = 1; | |
char ch = input.charAt(0); | |
while (pos < input.length()) { | |
if (ch == input.charAt(pos)) { // same letter as stored | |
count++; | |
pos++; | |
} else { // different letter | |
sb.append(count); | |
sb.append(ch); | |
ch = input.charAt(pos); | |
pos++; | |
count = 1; | |
} | |
} | |
sb.append(count); | |
sb.append(ch); | |
return sb.toString(); | |
} | |
public static long runSequence(String input, int iterations) { | |
String output = input; | |
for (int i = 0; i < iterations; i++) { | |
output = processString(output); | |
} | |
return output.length(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment