day11_common.carbon 800 B

12345678910111213141516171819202122232425262728293031
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // https://adventofcode.com/2024/day/11
  5. library "day11_common";
  6. import Core library "io";
  7. import library "io_utils";
  8. fn Next(n: i64) -> (i64, i64) {
  9. if (n == 0) { return (1, -1); }
  10. var pow10: i64 = 10;
  11. var pow100: i64 = 1;
  12. while (n / pow100 >= 100) {
  13. pow100 = pow100 * 100;
  14. pow10 = pow10 * 10;
  15. }
  16. if (n / pow100 >= 10) {
  17. return (n / pow10, n % pow10);
  18. }
  19. return (n * 2024, -1);
  20. }
  21. fn Count(n: i64, depth: i32) -> i32 {
  22. if (n == -1) { return 0; }
  23. if (depth == 0) { return 1; }
  24. let next: (i64, i64) = Next(n);
  25. return Count(next.0, depth - 1) + Count(next.1, depth - 1);
  26. }