day3_part2.carbon 870 B

123456789101112131415161718192021222324252627282930313233
  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/3
  5. import Core library "io";
  6. import library "day3_common";
  7. import library "io_utils";
  8. fn Run() {
  9. var total: i32 = 0;
  10. var enabled: bool = true;
  11. while (PeekChar() != Core.EOF()) {
  12. if (PeekChar() == 0x6D) {
  13. // TODO: Use `if let` when available.
  14. let result: (bool, i32, i32) = ReadMul();
  15. if (result.0 and enabled) {
  16. total += result.1 * result.2;
  17. }
  18. } else if (PeekChar() == 0x64) {
  19. // TODO: Use `if let` when available.
  20. let result: (bool, bool) = ReadDoOrDont();
  21. if (result.0) {
  22. enabled = result.1;
  23. }
  24. } else {
  25. ReadChar();
  26. }
  27. }
  28. Core.Print(total);
  29. }