|
| 1 | +package dev.jacobandersen.codechallenges.challenge.adventofcode.year2024.day12; |
| 2 | + |
| 3 | +import dev.jacobandersen.codechallenges.challenge.adventofcode.Day; |
| 4 | +import dev.jacobandersen.codechallenges.util.Direction; |
| 5 | +import dev.jacobandersen.codechallenges.util.Grid; |
| 6 | +import dev.jacobandersen.codechallenges.util.Pair; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | +import java.util.List; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +public class Day12 extends Day { |
| 13 | + public Day12() { |
| 14 | + super(2024, 12, "Garden Groups"); |
| 15 | + } |
| 16 | + |
| 17 | + private boolean isInRegion(List<Region> regions, int y, int x) { |
| 18 | + return regions.stream().anyMatch(rg -> rg.plots.stream().anyMatch(plot -> plot.y == y && plot.x == x)); |
| 19 | + } |
| 20 | + |
| 21 | + private void exploreFrom(Grid<String> grid, Plot plot, Region region) { |
| 22 | + if (region.plots.contains(plot)) return; // don't backtrack |
| 23 | + |
| 24 | + region.plots.add(plot); |
| 25 | + |
| 26 | + Grid<String> local = grid.copy(); |
| 27 | + for (Direction direction : Direction.cardinalValues()) { |
| 28 | + final String peek = local.peek(direction); |
| 29 | + if (peek != null && peek.equals(plot.crop)) { |
| 30 | + local.move(direction); |
| 31 | + exploreFrom(local, new Plot(plot.crop, local.getCurrentRow(), local.getCurrentCol()), region); |
| 32 | + local.move(direction.getOpposite()); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private List<Region> explore(Grid<String> grid) { |
| 38 | + List<Region> regions = new ArrayList<>(); |
| 39 | + |
| 40 | + for (int y = 0; y < grid.getRows(); y++) { |
| 41 | + for (int x = 0; x < grid.getCols(); x++) { |
| 42 | + grid.move(y, x); |
| 43 | + if (isInRegion(regions, y, x)) continue; |
| 44 | + |
| 45 | + Region region = new Region(new ArrayList<>()); |
| 46 | + exploreFrom(grid, new Plot(grid.get(), y, x), region); |
| 47 | + regions.add(region); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return regions; |
| 52 | + } |
| 53 | + |
| 54 | + private Grid<String> loadGrid() { |
| 55 | + return Grid.create(getInputLinesStreamNoBlanks(), str -> Arrays.stream(str.split("")).toList()); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String partOne() { |
| 60 | + return String.valueOf( |
| 61 | + explore(loadGrid()).stream().mapToInt(Region::fencePrice).sum() |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String partTwo() { |
| 67 | + Grid<String> grid = loadGrid(); |
| 68 | + List<Region> regions = explore(grid); |
| 69 | + return String.valueOf( |
| 70 | + regions.stream().mapToInt(Region::discountedFencePrice).sum() |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + record Plot(String crop, int y, int x) { |
| 75 | + } |
| 76 | + |
| 77 | + record Region(List<Plot> plots) { |
| 78 | + public int area() { |
| 79 | + return plots.size(); |
| 80 | + } |
| 81 | + |
| 82 | + public int perimeter() { |
| 83 | + final Set<Pair<Integer, Integer>> points = plots.stream().map(plot -> Pair.of(plot.y, plot.x)).collect(Collectors.toSet()); |
| 84 | + return points.stream().mapToInt(pt -> { |
| 85 | + int perimeter = 4; |
| 86 | + for (final Direction direction : Direction.cardinalValues()) { |
| 87 | + final Pair<Integer, Integer> rel = direction.relativeCoordinate(); |
| 88 | + final Pair<Integer, Integer> neighbor = Pair.of(pt.first() + rel.first(), pt.second() + rel.second()); |
| 89 | + if (points.contains(neighbor)) perimeter--; |
| 90 | + } |
| 91 | + return perimeter; |
| 92 | + }).sum(); |
| 93 | + } |
| 94 | + |
| 95 | + public int fencePrice() { |
| 96 | + return area() * perimeter(); |
| 97 | + } |
| 98 | + |
| 99 | + public int sides() { |
| 100 | + return 0; |
| 101 | + } |
| 102 | + |
| 103 | + public int discountedFencePrice() { |
| 104 | + return area() * sides(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments