Day 7
1 and 2
On reflection, it was a pretty fun little problem to solve. There wasn't much of a difference between the two parts. I ran into some bugs with my recursion termination conditions, but I got them in the end.
Part 1. A quick look at the data showed that the input length was short enough to perform an O(2^n^) search with some early exits. I coded it as a dfs.
Part 2. Adding concatenation just changes the base from 2 to 3, which, while strictly slower, wasn't much slower for this input.
code
void d7(bool sub) => print(getLines()
.map((l) => l.split(RegExp(r':? ')).map(int.parse).toList())
.fold<int>(
0, (p, ops) => test(ops, ops[0], ops[1], 2, sub) ? ops[0] + p : p));
bool test(List<int> l, int cal, int cur, int i, bool sub) =>
cur == cal && i == l.length ||
(i < l.length && cur <= cal) &&
(test(l, cal, cur + l[i], i + 1, sub) ||
test(l, cal, cur * l[i], i + 1, sub) ||
(sub && test(l, cal, cur.concat(l[i]), i + 1, sub)));
re: branch cutting
IDK if this is what your issue was, but one thing I ran into was that if you do something likeif (current_total >= target) prune(),
this can be problematic because if the tail end of the data is 0s and 1s, you exit too early. Basically I would prune strictly when the current total > target.