advent_of_code_2024/day_12.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
//! This is my solution for [Advent of Code - Day 12: _Garden Groups_](https://adventofcode.com/2024/day/12)
//!
//! [`parse_input`] turns the input file it a [`Garden`] as a `Vec<Vec<char>>`.
//!
//! [`Garden::find_regions`] splits the Garden into [`Region`]s. [`Garden::total_fencing_cost`] solves part 1 using
//! the data collected when finding the regions. [`Garden::total_fencing_cost_with_discount`] solves part 2, using
//! [`Region::count_edges`] to find the unique edges in a region by counting corners in the perimeter.
use itertools::Itertools;
use std::collections::HashSet;
use std::{fs, usize};
/// The entry point for running the solutions with the 'real' puzzle input.
///
/// - The puzzle input is expected to be at `<project_root>/res/day-12-input`
/// - It is expected this will be called by [`super::main()`] when the user elects to run day 12.
pub fn run() {
let contents = fs::read_to_string("res/day-12-input.txt").expect("Failed to read file");
let garden = parse_input(&contents);
println!("The total fencing cost is {}", garden.total_fencing_cost());
println!(
"The total discounted fencing cost is {}",
garden.total_fencing_cost_with_discount()
);
}
/// Coordinates of a plot within a [`Garden`]
type Plot = (usize, usize);
/// Implement deltas as a struct to allow some convenient consts and functions to be defined
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
struct Delta(isize, isize);
impl Delta {
/// Move upwards
const UP: Delta = Delta(-1, 0);
/// Move rightwards
const RIGHT: Delta = Delta(0, 1);
/// Move downwards
const DOWN: Delta = Delta(1, 0);
/// Move leftwards
const LEFT: Delta = Delta(0, -1);
/// Combine two deltas
fn add(&self, other: &Self) -> Self {
Delta(self.0 + other.0, self.1 + other.1)
}
/// Get the coordinates of the plot after applying this delta to the provided plot. This will be None if either
/// axis becomes negative
fn apply_to(&self, (r, c): &Plot) -> Option<Plot> {
r.checked_add_signed(self.0)
.zip(c.checked_add_signed(self.1))
}
}
/// Use to track which side of the current plot has the edge being followed when walking the perimeter
#[derive(Eq, PartialEq, Debug, Copy, Clone, Hash)]
enum Side {
TOP,
RIGHT,
BOTTOM,
LEFT,
}
impl Side {
/// Given a facing parallel to the current edge, headed clockwise, the plot forwards and Side::left will be filled if
/// the edge turns round a concave corner.
fn convex_delta(&self) -> Delta {
match self {
Side::TOP => Delta::UP.add(&Delta::RIGHT),
Side::RIGHT => Delta::RIGHT.add(&Delta::DOWN),
Side::BOTTOM => Delta::DOWN.add(&Delta::LEFT),
Side::LEFT => Delta::LEFT.add(&Delta::UP),
}
}
/// Given a cell which potentially has an edge on this side, what is the delta to cross that edge, from inside
/// the shape to outside
fn cross_outwards_delta(&self) -> Delta {
match self {
Side::TOP => Delta::UP,
Side::RIGHT => Delta::RIGHT,
Side::BOTTOM => Delta::DOWN,
Side::LEFT => Delta::LEFT,
}
}
/// The facing parallel to this side, that walks the inside of that edge clockwise.
fn straight_ahead_delta(&self) -> Delta {
self.turn_clockwise().cross_outwards_delta()
}
/// The side clockwise of this one
fn turn_clockwise(&self) -> Side {
match self {
Side::TOP => Side::RIGHT,
Side::RIGHT => Side::BOTTOM,
Side::BOTTOM => Side::LEFT,
Side::LEFT => Side::TOP,
}
}
}
/// A region that is a set of orthogonally adjacent plots in a [`Garden`] with the same crop. It stores the plots and
/// number of units in the perimeter for use by [`Garden::total_fencing_cost`].
#[derive(Eq, PartialEq, Debug)]
struct Region {
crop: char,
plots: HashSet<Plot>,
perimeter: HashSet<(Plot, Side)>,
}
impl Region {
/// Initialise an empty region
fn new(crop: char) -> Region {
Region {
crop,
plots: HashSet::new(),
perimeter: HashSet::new(),
}
}
/// Helper for checking if plot is in the grid. Takes an `Option` to match [`Delta::apply_to`]
fn contains(&self, plot: &Option<Plot>) -> bool {
if let Some(coord) = plot {
self.plots.iter().contains(coord)
} else {
false
}
}
/// Given an edge on one side of a plot. Calculate if following that edge clockwise is a corner
///
/// ```text
/// +---+
/// | A |
/// +---+ - +
/// | B | C |
/// +---+---+
/// ```
///
/// There are only three cases:
/// - Straight - no corner: The examples above are
/// - Following the Side::right of the shape from `A` to `C`.
/// - Following the Side::bottom of the shape from `C` to `B`.
/// - Convex corner, e.g. Side::top B going to A
/// - Concave corner which follow the Side::left and Side::top of A, Side::bottom and Side::left of B, and the Side::right of C.
///
/// If the block straight-ahead is not set it's a concave corner. If it is set and the one ahead and to the Side::left
/// is also set it's concave.
fn check_for_corner(&self, plot: &Plot, side: &Side) -> bool {
let next_convex = side.convex_delta().apply_to(&plot);
let next_straight = side.straight_ahead_delta().apply_to(&plot);
!self.contains(&next_straight) || self.contains(&next_convex)
}
/// For all the edges in the perimeter, check if they are followed by a corner
fn count_edges(&self) -> usize {
self.perimeter
.iter()
.filter(|(plot, side)| self.check_for_corner(plot, &side))
.count()
}
}
/// A grid of plots containing regions of different crops
#[derive(Eq, PartialEq, Debug)]
struct Garden {
plots: Vec<Vec<char>>,
}
impl Garden {
/// Get the contents of a given plot, None if the coordinates are outside the garden
fn get(&self, (r, c): Plot) -> Option<char> {
self.plots.get(r).and_then(|row| row.get(c).copied())
}
/// Do a modified bucket fill to determine the plots that make up the region that includes the starting plot. When
/// an edge is encountered store the side of the plot it is on for later corner detection.
fn walk_region(&self, start: Plot) -> Region {
fn walk_region_iter(garden: &Garden, plot: Plot, region: &mut Region) {
let crop = garden.get(plot).unwrap();
if !region.plots.insert(plot) {
// already visited
return;
}
for side in [Side::TOP, Side::RIGHT, Side::BOTTOM, Side::LEFT] {
match side
.cross_outwards_delta()
.apply_to(&plot)
.and_then(|next_plot| Some(next_plot).zip(garden.get(next_plot)))
{
Some((next_plot, next_crop)) if next_crop == crop => {
walk_region_iter(garden, next_plot, region)
}
_ => {
region.perimeter.insert((plot, side));
}
}
}
}
let mut region = Region::new(self.get(start).unwrap());
walk_region_iter(self, start, &mut region);
region
}
/// Iterate over each plots' coordinates in the garden
fn iter_plots<'a>(&'a self) -> impl Iterator<Item = Plot> + 'a {
self.plots
.iter()
.enumerate()
.flat_map(|(r, row)| row.iter().enumerate().map(move |(c, _)| (r, c)))
}
/// Return all the distinct crop regions in the garden
fn find_regions(&self) -> Vec<Region> {
let mut visited: HashSet<Plot> = HashSet::new();
let mut regions = Vec::new();
for (r, c) in self.iter_plots() {
if !visited.contains(&(r, c)) {
let region = self.walk_region((r, c));
visited.extend(®ion.plots);
regions.push(region);
}
}
regions
}
/// The total cost to fence all the regions in the garden
fn total_fencing_cost(&self) -> usize {
self.find_regions()
.iter()
.map(|region| region.plots.len() * region.perimeter.len())
.sum()
}
/// The total cost to fence all the regions in the garden after applying the "bulk discount"
fn total_fencing_cost_with_discount(&self) -> usize {
self.find_regions()
.iter()
.map(|region| region.plots.len() * region.count_edges())
.sum()
}
}
/// Parse a text grid into a [`Garden`]
fn parse_input(input: &String) -> Garden {
Garden {
plots: input.lines().map(|line| line.chars().collect()).collect(),
}
}
#[cfg(test)]
mod tests {
use crate::day_12::*;
use crate::helpers::test::assert_contains_in_any_order;
fn example_garden() -> Garden {
Garden {
plots: vec![
vec!['A', 'A', 'A', 'A'],
vec!['B', 'B', 'C', 'D'],
vec!['B', 'B', 'C', 'C'],
vec!['E', 'E', 'E', 'C'],
],
}
}
//noinspection SpellCheckingInspection
fn enclave_example() -> Garden {
parse_input(
&"OOOOO
OXOXO
OOOOO
OXOXO
OOOOO"
.to_string(),
)
}
//noinspection SpellCheckingInspection
fn larger_example() -> Garden {
parse_input(
&"RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE
"
.to_string(),
)
}
//noinspection SpellCheckingInspection
#[test]
fn can_parse_input() {
let input = "AAAA
BBCD
BBCC
EEEC"
.to_string();
assert_eq!(parse_input(&input), example_garden())
}
fn region_a() -> Region {
Region {
crop: 'A',
plots: vec![(0, 0), (0, 1), (0, 2), (0, 3)].into_iter().collect(),
perimeter: vec![
((0, 0), Side::TOP),
((0, 0), Side::BOTTOM),
((0, 0), Side::LEFT),
((0, 1), Side::TOP),
((0, 1), Side::BOTTOM),
((0, 2), Side::TOP),
((0, 2), Side::BOTTOM),
((0, 3), Side::TOP),
((0, 3), Side::RIGHT),
((0, 3), Side::BOTTOM),
]
.into_iter()
.collect(),
}
}
fn region_b() -> Region {
Region {
crop: 'B',
plots: vec![(1, 0), (1, 1), (2, 0), (2, 1)].into_iter().collect(),
perimeter: vec![
((1, 0), Side::TOP),
((1, 0), Side::LEFT),
((1, 1), Side::TOP),
((1, 1), Side::RIGHT),
((2, 0), Side::BOTTOM),
((2, 0), Side::LEFT),
((2, 1), Side::RIGHT),
((2, 1), Side::BOTTOM),
]
.into_iter()
.collect(),
}
}
fn region_c() -> Region {
Region {
crop: 'C',
plots: vec![(1, 2), (2, 2), (2, 3), (3, 3)].into_iter().collect(),
perimeter: vec![
((1, 2), Side::RIGHT),
((1, 2), Side::LEFT),
((2, 2), Side::BOTTOM),
((2, 3), Side::RIGHT),
((3, 3), Side::BOTTOM),
((2, 3), Side::TOP),
((1, 2), Side::TOP),
((2, 2), Side::LEFT),
((3, 3), Side::RIGHT),
((3, 3), Side::LEFT),
]
.into_iter()
.collect(),
}
}
fn region_d() -> Region {
Region {
crop: 'D',
plots: vec![(1, 3)].into_iter().collect(),
perimeter: vec![
((1, 3), Side::TOP),
((1, 3), Side::BOTTOM),
((1, 3), Side::LEFT),
((1, 3), Side::RIGHT),
]
.into_iter()
.collect(),
}
}
fn region_e() -> Region {
Region {
crop: 'E',
plots: vec![(3, 0), (3, 1), (3, 2)].into_iter().collect(),
perimeter: vec![
((3, 1), Side::TOP),
((3, 2), Side::RIGHT),
((3, 2), Side::BOTTOM),
((3, 0), Side::TOP),
((3, 2), Side::TOP),
((3, 1), Side::BOTTOM),
((3, 0), Side::LEFT),
((3, 0), Side::BOTTOM),
]
.into_iter()
.collect(),
}
}
#[test]
fn can_find_region() {
let garden = example_garden();
assert_eq!(garden.walk_region((0, 0)), region_a());
assert_eq!(garden.walk_region((1, 0)), region_b());
assert_eq!(garden.walk_region((1, 2)), region_c());
assert_eq!(garden.walk_region((1, 3)), region_d());
assert_eq!(garden.walk_region((3, 0)), region_e());
assert_contains_in_any_order(
garden.find_regions(),
vec![region_a(), region_b(), region_c(), region_d(), region_e()],
);
}
#[test]
fn can_calculate_costs() {
assert_eq!(example_garden().total_fencing_cost(), 140);
assert_eq!(enclave_example().total_fencing_cost(), 772);
assert_eq!(larger_example().total_fencing_cost(), 1930);
}
#[test]
fn can_count_edges() {
let basic = Region {
crop: 'A',
plots: vec![(0, 0)].into_iter().collect(),
perimeter: vec![
((0, 0), Side::TOP),
((0, 0), Side::RIGHT),
((0, 0), Side::BOTTOM),
((0, 0), Side::LEFT),
]
.into_iter()
.collect(),
};
assert_eq!(basic.count_edges(), 4);
assert_eq!(region_a().count_edges(), 4);
let regions = enclave_example().find_regions();
let with_holes = regions.iter().find(|r| r.crop == 'O').unwrap();
assert_eq!(with_holes.count_edges(), 20);
}
//noinspection SpellCheckingInspection
#[test]
fn can_calculate_costs_with_discount() {
assert_eq!(example_garden().total_fencing_cost_with_discount(), 80);
assert_eq!(enclave_example().total_fencing_cost_with_discount(), 436);
assert_eq!(larger_example().total_fencing_cost_with_discount(), 1206);
let example_e = parse_input(
&"EEEEE
EXXXX
EEEEE
EXXXX
EEEEE
"
.to_string(),
);
assert_eq!(example_e.total_fencing_cost_with_discount(), 236);
let example_diagnonal = parse_input(
&"AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA
"
.to_string(),
);
assert_eq!(example_diagnonal.total_fencing_cost_with_discount(), 368);
}
}