-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay14.java
148 lines (122 loc) · 3.45 KB
/
Day14.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package aoc18;
import java.util.Stack;
import myutils18.DoublyLinkedList;
import myutils18.DoublyLinkedList.Node;
public class Day14 {
private int recipeCount;
private DoublyLinkedList recipes;
public Day14(int recipeCount) {
this.recipeCount = recipeCount;
recipes = new DoublyLinkedList();
}
// part 1
public void run() {
recipes.addLast(3);
recipes.addLast(7);
Node firstElf = recipes.getHead();
Node secondElf = recipes.getHead().next;
while (recipes.size() < recipeCount + 11) {
int newRecipe = firstElf.data + secondElf.data;
addRecipes(recipes, newRecipe);
// move elves to new recipe positions
int limitFirstElf = firstElf.data + 1;
for (int i = 0; i < limitFirstElf; i++) {
firstElf = firstElf.next;
if (firstElf == null)
firstElf = recipes.getHead();
}
int limitSecondElf = secondElf.data + 1;
for (int i = 0; i < limitSecondElf; i++) {
secondElf = secondElf.next;
if (secondElf == null)
secondElf = recipes.getHead();
}
}
String resultRecipes = get10Recipes();
System.out.println(resultRecipes);
}
private String get10Recipes() {
StringBuffer resultRecipes = new StringBuffer();
Node tmpNode = recipes.getHead();
for (int i = 0; i < recipeCount; i++) {
tmpNode = tmpNode.next;
}
for (int i = 0; i < 10; i++) {
resultRecipes.append(tmpNode.data);
tmpNode = tmpNode.next;
}
return resultRecipes.toString();
}
private void addRecipes(DoublyLinkedList recipes, int newRecipe) {
if (newRecipe == 0) {
recipes.addLast(0);
return;
}
Stack<Integer> recipeDigits = new Stack<Integer>();
while (newRecipe > 0) {
recipeDigits.add(newRecipe % 10);
newRecipe /= 10;
}
while (!recipeDigits.isEmpty()) {
recipes.addLast(recipeDigits.pop());
}
}
@SuppressWarnings("unused")
private void printList() {
for (Node tmp = recipes.getHead(); tmp != null; tmp = tmp.next) {
System.out.print(tmp.data + " ");
}
}
// part 2
public void run2() {
recipes.addLast(3);
recipes.addLast(7);
Node firstElf = recipes.getHead();
Node secondElf = recipes.getHead().next;
int resultIndex = -1;
int count = 0;
;
while (true) {
int newRecipe = firstElf.data + secondElf.data;
addRecipes(recipes, newRecipe);
// move elves to new recipe positions
int limitFirstElf = firstElf.data + 1;
for (int i = 0; i < limitFirstElf; i++) {
firstElf = firstElf.next;
if (firstElf == null)
firstElf = recipes.getHead();
}
int limitSecondElf = secondElf.data + 1;
for (int i = 0; i < limitSecondElf; i++) {
secondElf = secondElf.next;
if (secondElf == null)
secondElf = recipes.getHead();
}
// checking is expensive so only do it after adding 1.000.000 new
// recipes
if (count % 1000000 == 0) {
resultIndex = indexOfScoreSequence();
}
if (resultIndex > 0)
break;
count++;
}
System.out.println(resultIndex);
}
private int indexOfScoreSequence() {
StringBuffer recipesAsString = new StringBuffer();
for (Node tmpNode = recipes.getHead(); tmpNode != null; tmpNode = tmpNode.next) {
recipesAsString.append(tmpNode.data);
}
// System.out.println(recipesAsString.length());
return recipesAsString.indexOf(Integer.toString(recipeCount));
}
public static void main(String[] args) {
int input = 598701;
// int sampleInput = 9;
Day14 test = new Day14(input);
// test.run();
test.run2();
// test.printList();
}
}