From 91196f14c9e5a9f49dc3b70cb049aae16a1e2d9f Mon Sep 17 00:00:00 2001 From: Sander in 't Hout Date: Fri, 13 Dec 2024 14:17:39 +0100 Subject: [PATCH] Add AoC 2024 day 12 part 1 solver --- assets/advent_of_code/2024_12_sample.txt | 10 ++++ .../advent_of_code/2024/_all_solvers.dart | 1 + .../advent_of_code/2024/day_12_solver.dart | 50 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 assets/advent_of_code/2024_12_sample.txt create mode 100644 lib/solvers/advent_of_code/2024/day_12_solver.dart diff --git a/assets/advent_of_code/2024_12_sample.txt b/assets/advent_of_code/2024_12_sample.txt new file mode 100644 index 0000000..0b328f1 --- /dev/null +++ b/assets/advent_of_code/2024_12_sample.txt @@ -0,0 +1,10 @@ +RRRRIICCFF +RRRRIICCCF +VVRRRCCFFF +VVRCCCJFFF +VVVVCJJCFE +VVIVCCJJEE +VVIIICJJEE +MIIIIIJJEE +MIIISIJEEE +MMMISSJEEE \ No newline at end of file diff --git a/lib/solvers/advent_of_code/2024/_all_solvers.dart b/lib/solvers/advent_of_code/2024/_all_solvers.dart index 31e8465..0b9230f 100644 --- a/lib/solvers/advent_of_code/2024/_all_solvers.dart +++ b/lib/solvers/advent_of_code/2024/_all_solvers.dart @@ -5,3 +5,4 @@ export 'day_04_solver.dart'; export 'day_05_solver.dart'; export 'day_06_solver.dart'; export 'day_11_solver.dart'; +export 'day_12_solver.dart'; diff --git a/lib/solvers/advent_of_code/2024/day_12_solver.dart b/lib/solvers/advent_of_code/2024/day_12_solver.dart new file mode 100644 index 0000000..64e9872 --- /dev/null +++ b/lib/solvers/advent_of_code/2024/day_12_solver.dart @@ -0,0 +1,50 @@ +import 'package:h3x_devtools/solvers/advent_of_code/2024/aoc_2024_solver.dart'; + +class Day12Solver extends AdventOfCode2024Solver { + + @override + final int dayNumber = 12; + + @override + String getSolution(String input) { + Grid grid = Grid.fromString(input, (value) => value); + + // Part 1 + Map areaSizes = {}, perimeterLengths = {}; + int currentAreaNameChar = 97; + for (int x = 0; x < grid.width; x++) { + for (int y = 0; y < grid.height; y++) { + Coordinates coordinates = Coordinates(x, y, grid); + Cell cell = grid[coordinates]; + + // Area size + if (!areaSizes.containsKey(cell.obj)) { + // Because some area names can be used for multiple areas, make the area names unique. + grid.floodFill4Way(coordinates, cell.obj, String.fromCharCode(currentAreaNameChar)); + currentAreaNameChar++; + + areaSizes[cell.obj] = 1; + perimeterLengths[cell.obj] = 0; + } else { + areaSizes[cell.obj] = areaSizes[cell.obj]! + 1; + } + + // Perimeter length + for (CardinalDirection direction in CardinalDirection.values) { + if (!coordinates.canGoCDirection(direction)) { + perimeterLengths[cell.obj] = perimeterLengths[cell.obj]! + 1; + } else { + Coordinates neighbor = coordinates.goToCDirection(direction); + if (grid[neighbor].obj != cell.obj) { + perimeterLengths[cell.obj] = perimeterLengths[cell.obj]! + 1; + } + } + } + } + } + int price1 = areaSizes.keys.map((key) => areaSizes[key]! * perimeterLengths[key]!).sum; + + return 'Part 1 price: $price1'; + } + +}