-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c99747d
commit 91196f1
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
RRRRIICCFF | ||
RRRRIICCCF | ||
VVRRRCCFFF | ||
VVRCCCJFFF | ||
VVVVCJJCFE | ||
VVIVCCJJEE | ||
VVIIICJJEE | ||
MIIIIIJJEE | ||
MIIISIJEEE | ||
MMMISSJEEE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> grid = Grid.fromString(input, (value) => value); | ||
|
||
// Part 1 | ||
Map<String, int> 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<String> 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'; | ||
} | ||
|
||
} |