day12_part1.carbon 759 B

1234567891011121314151617181920212223242526272829
  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/12
  5. import Core library "io";
  6. import Core library "range";
  7. import library "day12_common";
  8. import library "io_utils";
  9. fn Run() {
  10. var map: Map = Map.Read();
  11. var regions: DisjointSetForest = MakeRegions(map);
  12. var total: i32 = 0;
  13. for (i: i32 in Core.Range(140 * 140)) {
  14. if (regions.Lookup(i) == i) {
  15. let area: i32 = regions.Weight(i);
  16. let internal_edges: i32 = regions.Unions(i);
  17. let perimeter: i32 = area * 4 - internal_edges * 2;
  18. total += area * perimeter;
  19. }
  20. }
  21. Core.Print(total);
  22. }