@@ -46,10 +46,14 @@ public long solvePart1() {
46
46
}
47
47
48
48
public enum Op {
49
- SUM , MULT ;
49
+ SUM , MULT , CONCAT ;
50
50
51
51
public String print () {
52
- return this == SUM ? "+" : "*" ;
52
+ return switch (this ) {
53
+ case SUM -> "+" ;
54
+ case MULT -> "*" ;
55
+ case CONCAT -> "||" ;
56
+ };
53
57
}
54
58
}
55
59
@@ -102,7 +106,87 @@ private long countOfCorrectOperators(long result, List<Long> longs, List<Op> ope
102
106
}
103
107
104
108
public long solvePart2 () {
109
+ long sum = 0 ;
110
+ for (int i = 0 ; i < results .size (); i ++) {
111
+ Long result = results .get (i );
105
112
106
- return 0 ;
113
+ LOGGER .debug ("Checking {}" , result );
114
+ long count = countOfCorrectOperatorsWithConcat (result , components .get (i ), List .of (), newArrayList ());
115
+ LOGGER .debug ("count for {}: {}" , result , count );
116
+ if (count > 0 ) {
117
+ sum += result ;
118
+ }
119
+ }
120
+ return sum ;
107
121
}
122
+
123
+
124
+ private long countOfCorrectOperatorsWithConcat (long result , List <Long > longs , List <Op > operators , List <Long > components ) {
125
+ if (longs .size () == 1 ) {
126
+ Long lastEl = Iterables .getOnlyElement (longs );
127
+ if (result == lastEl ) {
128
+
129
+ String fullOperation = "(" .repeat (operators .size ()) + lastEl ;
130
+
131
+ ArrayList <Op > mutableOps = newArrayList (operators );
132
+ ArrayList <Long > mutableComponents = newArrayList (components );
133
+ while (!mutableOps .isEmpty ()) {
134
+ fullOperation += mutableOps .removeLast ().print () + "" + mutableComponents .removeLast () + ")" ;
135
+ }
136
+
137
+ LOGGER .debug ("Operation: {}" , fullOperation );
138
+ LOGGER .info ("Found correct operators" );
139
+ return 1 ;
140
+ }
141
+
142
+ return 0 ;
143
+
144
+ }
145
+
146
+ if (result <= 0 || longs .isEmpty ()) {
147
+ return 0 ;
148
+ }
149
+
150
+
151
+ ArrayList <Op > opsForSum = newArrayList (operators );
152
+ opsForSum .add (Op .SUM );
153
+
154
+
155
+
156
+ ArrayList <Long > next = newArrayList (longs );
157
+ Long component = next .removeLast ();
158
+
159
+ ArrayList <Long > nextComponents = newArrayList (components );
160
+ nextComponents .add (component );
161
+
162
+ long sumConcat = 0 ;
163
+ String stringResult = "" + result ;
164
+ String stringComponent = "" + component ;
165
+
166
+ LOGGER .info ("Result: {} - longs: {}" , result , longs );
167
+ LOGGER .info ("Result: {} - stringComponent: {}" , stringResult , stringComponent );
168
+
169
+ if (stringResult .length () > stringComponent .length () && stringResult .endsWith (stringComponent )) {
170
+ LOGGER .info ("Trailing matches: Result: {} - stringComponent: {}" , stringResult , stringComponent );
171
+
172
+ ArrayList <Op > opsForConcat = newArrayList (operators );
173
+ opsForConcat .add (Op .CONCAT );
174
+
175
+ long expectedResult = Long .parseLong (stringResult .substring (0 , stringResult .length () - stringComponent .length ()));
176
+ LOGGER .info ("expectedResult: {}" , expectedResult );
177
+ sumConcat += countOfCorrectOperatorsWithConcat (
178
+ expectedResult ,
179
+ next , opsForConcat , nextComponents );
180
+ }
181
+
182
+ long sumMult = 0 ;
183
+ if (result % component == 0 ) {
184
+ ArrayList <Op > opsForMult = newArrayList (operators );
185
+ opsForMult .add (MULT );
186
+ sumMult += countOfCorrectOperatorsWithConcat (result / component , next , opsForMult , nextComponents );
187
+ }
188
+ return sumMult + sumConcat
189
+ + countOfCorrectOperatorsWithConcat (result - component , next , opsForSum , nextComponents );
190
+ }
191
+
108
192
}
0 commit comments