day1_common.carbon 758 B

123456789101112131415161718192021222324252627
  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/1
  5. library "day1_common";
  6. import Core library "range";
  7. import library "io_utils";
  8. // Read a sequence of lines each containing a pair of numbers into two arrays.
  9. // Returns the number of lines read.
  10. fn ReadInputs[N:! Core.IntLiteral()](ref a: array(i32, N), ref b: array(i32, N)) -> i32 {
  11. for (i: i32 in Core.Range(N)) {
  12. var av: i32;
  13. var bv: i32;
  14. if (ReadInt(ref av) and SkipSpaces() and ReadInt(ref bv) and SkipNewline()) {
  15. a[i] = av;
  16. b[i] = bv;
  17. } else {
  18. return i;
  19. }
  20. }
  21. return N;
  22. }