diff --git a/lib/day07.dart b/lib/day07.dart index 053fda0..3fb9ec1 100644 --- a/lib/day07.dart +++ b/lib/day07.dart @@ -12,9 +12,9 @@ int solveA(Iterable input) { for (final line in input) { final [testValueString, rest] = line.split(': '); final testValue = int.parse(testValueString); - final numbers = [...rest.split(' ').reversed.map(int.parse)]; + final numbers = [...rest.split(' ').map(int.parse)]; - if (tryOperators1(numbers, testValue, 0).contains(testValue)) { + if (tryOperators(numbers, testValue, partB: false)) { totalCalibrationResult += testValue; } } @@ -44,9 +44,9 @@ Future solveB(List input) async { for (final line in lines) { final [testValueString, rest] = line.split(': '); final testValue = int.parse(testValueString); - final numbers = [...rest.split(' ').reversed.map(int.parse)]; + final numbers = [...rest.split(' ').map(int.parse)]; - if (tryOperators2(numbers, testValue, 0).contains(testValue)) { + if (tryOperators(numbers, testValue, partB: true)) { totalCalibrationResult += testValue; } } @@ -57,15 +57,39 @@ Future solveB(List input) async { .sum; } -Iterable tryOperators2(List numbers, int target, int pos) sync* { - if (pos == numbers.length - 1) { - yield numbers.last; - } else { - yield* tryOperators2(numbers, target, pos + 1) - .map((value) => value + numbers[pos]); - yield* tryOperators2(numbers, target, pos + 1) - .map((value) => value * numbers[pos]); - yield* tryOperators2(numbers, target, pos + 1) - .map((value) => int.parse('$value${numbers[pos]}')); +bool tryOperators( + List numbers, + int target, { + int result = 0, + int pos = 0, + required bool partB, +}) { + if (result > target) { + return false; + } + if (pos == numbers.length) { + return target == result; } + return tryOperators( + numbers, + target, + result: numbers[pos] + result, + pos: pos + 1, + partB: partB, + ) || + tryOperators( + numbers, + target, + result: numbers[pos] * result, + pos: pos + 1, + partB: partB, + ) || + (partB && + tryOperators( + numbers, + target, + result: int.parse('$result${numbers[pos]}'), + pos: pos + 1, + partB: partB, + )); }