-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymerFactory.java
More file actions
78 lines (70 loc) · 3.04 KB
/
PolymerFactory.java
File metadata and controls
78 lines (70 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Solution to Day 14: Extended Polymerization.
*
* @author bluebillxp
*/
public class PolymerFactory {
public static void main(String[] args) {
List<String> input = AdventHelper.readInput("input-day14-extended-polymerization.txt");
// List<String> input = AdventHelper.readInput("input-day14-test.txt");
System.out.println("Day 14: Extended Polymerization: " + input.size() + " lines loaded.");
System.out.println("Day 14: Extended Polymerization --- Part One ---");
System.out.println("10 steps - What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?");
final int answerOne = solutionPartOne(input);
System.out.println("Answer: " + answerOne);
System.out.println("\nDay 14: Extended Polymerization --- Part Two ---");
System.out.println("40 steps - What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?");
final int answerTwo = solutionPartTwo(input);
System.out.println("Answer: " + answerTwo);
}
private static int solutionPartOne(List<String> input) {
Map<String, Integer> pairMap = new HashMap<>();
for (int i = 2; i < input.size(); i++) {
String[] pair = input.get(i).split(" -> ");
pairMap.put(pair[0].trim(), pair[1].trim().charAt(0) - 'A');
}
char[] template = input.get(0).toCharArray();
for (int step = 0; step < 10; step++) {
char[] newTemplate = new char[template.length * 2 -1];
int index = 0;
for (int c = 0; c < template.length - 1; c++) {
Integer insertion = pairMap.get(String.format("%c%c", template[c], template[c+1]));
if (insertion != null) {
newTemplate[index] = template[c];
newTemplate[index + 1] = (char) ('A' + insertion.intValue());
index += 2;
} else {
newTemplate[index] = template[c];
index += 1;
}
}
newTemplate[index] = template[template.length - 1];
template = newTemplate;
}
int[] elementMap = new int[26];
for (char ch : new String(template).toCharArray()) {
if (ch - 'A' < 0) {
break;
}
elementMap[ch - 'A']++;
}
int maxElement = 0;
int minElement = 0;
for (int i = 0; i < elementMap.length; i++) {
if (elementMap[i] > elementMap[maxElement]) {
maxElement = i;
}
if ((elementMap[i] != 0 && elementMap[i] < elementMap[minElement])
|| elementMap[minElement] == 0) {
minElement = i;
}
}
return elementMap[maxElement] - elementMap[minElement];
}
private static int solutionPartTwo(List<String> input) {
return 0;
}
}