day1_part1.carbon 690 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. import Core library "io";
  6. import Core library "range";
  7. import library "day1_common";
  8. import library "sort";
  9. fn Abs(n: i32) -> i32 { return if n < 0 then -n else n; }
  10. fn Run() {
  11. var a: array(i32, 1000);
  12. var b: array(i32, 1000);
  13. let n: i32 = ReadInputs(ref a, ref b);
  14. Quicksort(ref a, 0, n);
  15. Quicksort(ref b, 0, n);
  16. var difference: i32 = 0;
  17. for (i: i32 in Core.Range(n)) {
  18. difference += Abs(a[i] - b[i]);
  19. }
  20. Core.Print(difference);
  21. }