day1_part1.carbon 653 B

12345678910111213141516171819202122232425262728
  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 library "day1_common";
  7. import library "sort";
  8. fn Abs(n: i32) -> i32 { return if n < 0 then -n else n; }
  9. fn Run() {
  10. var a: [i32; 1000];
  11. var b: [i32; 1000];
  12. var n: i32 = ReadInputs(&a, &b);
  13. Quicksort(&a, 0, n);
  14. Quicksort(&b, 0, n);
  15. var i: i32 = 0;
  16. var difference: i32 = 0;
  17. while (i < n) {
  18. difference += Abs(a[i] - b[i]);
  19. i += 1;
  20. }
  21. Core.Print(difference);
  22. }