day5_part2.carbon 818 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/5
  5. import Core library "io";
  6. import library "day5_common";
  7. import library "io_utils";
  8. fn Run() {
  9. var rules: Rules = Rules.Read();
  10. SkipNewline();
  11. var total: i32 = 0;
  12. while (true) {
  13. var page_list: PageList = PageList.Read();
  14. if (page_list.num_pages == 0) {
  15. break;
  16. }
  17. if (page_list.FollowsRules(rules)) {
  18. continue;
  19. }
  20. var new_page_list: PageList = PageList.Empty();
  21. while (page_list.num_pages != 0) {
  22. new_page_list.Add(page_list.ExtractPossibleFirstPage(rules));
  23. }
  24. total += new_page_list.MiddlePage();
  25. }
  26. Core.Print(total);
  27. }