Skip to content

Commit 5a14360

Browse files
committed
AoC 2024 Day 12 - rust - refactor
1 parent 2d758a6 commit 5a14360

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

src/main/rust/AoC2024_12/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
[dependencies]
77
aoc = { path = "../aoc" }
88
itertools = "0.11"
9+
lazy_static = "1.4"

src/main/rust/AoC2024_12/src/main.rs

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,23 @@ use aoc::graph::BFS;
55
use aoc::grid::Cell;
66
use aoc::Puzzle;
77
use itertools::Itertools;
8+
use lazy_static::lazy_static;
89
use std::collections::{HashMap, HashSet};
910

11+
lazy_static! {
12+
static ref CORNER_DIRS: [[Direction; 3]; 4] = [
13+
[Direction::LeftAndUp, Direction::Left, Direction::Up],
14+
[Direction::RightAndUp, Direction::Right, Direction::Up],
15+
[Direction::RightAndDown, Direction::Right, Direction::Down],
16+
[Direction::LeftAndDown, Direction::Left, Direction::Down],
17+
];
18+
static ref MATCHES: [[bool; 3]; 3] = [
19+
[false, false, false],
20+
[true, false, false],
21+
[false, true, true],
22+
];
23+
}
24+
1025
#[derive(Clone, Debug)]
1126
struct Regions {
1227
plots_by_plant: HashMap<char, HashSet<Cell>>,
@@ -84,13 +99,41 @@ impl<'a> Iterator for RegionIterator<'a> {
8499
}
85100
}
86101

102+
enum Pricing {
103+
Perimeter,
104+
NumberOfSides,
105+
}
106+
107+
impl Pricing {
108+
fn calculate(&self, plot: &Cell, region: &HashSet<Cell>) -> usize {
109+
match self {
110+
Pricing::Perimeter => {
111+
4 - plot
112+
.capital_neighbours()
113+
.iter()
114+
.filter(|n| region.contains(n))
115+
.count()
116+
}
117+
Pricing::NumberOfSides => CORNER_DIRS
118+
.iter()
119+
.filter(|d| {
120+
let test = (0..3)
121+
.map(|i| {
122+
plot.try_at(d[i])
123+
.is_some_and(|n| region.contains(&n))
124+
})
125+
.collect::<Vec<bool>>();
126+
MATCHES.iter().any(|m| *m == *test)
127+
})
128+
.count(),
129+
}
130+
}
131+
}
132+
87133
struct AoC2024_12;
88134

89135
impl AoC2024_12 {
90-
fn solve<F>(&self, input: &[String], count: F) -> usize
91-
where
92-
F: Fn(&Cell, &HashSet<Cell>) -> usize,
93-
{
136+
fn solve(&self, input: &[String], pricing: Pricing) -> usize {
94137
let regions: Regions = (0..input.len())
95138
.cartesian_product(0..input[0].len())
96139
.map(|(r, c)| (input[r].chars().nth(c).unwrap(), Cell::at(r, c)))
@@ -99,7 +142,7 @@ impl AoC2024_12 {
99142
.iter()
100143
.map(|r| {
101144
r.iter()
102-
.map(|plot| count(plot, &r) * r.len())
145+
.map(|plot| pricing.calculate(plot, &r) * r.len())
103146
.sum::<usize>()
104147
})
105148
.sum()
@@ -118,43 +161,11 @@ impl aoc::Puzzle for AoC2024_12 {
118161
}
119162

120163
fn part_1(&self, input: &Self::Input) -> Self::Output1 {
121-
let count_edges = |plot: &Cell, region: &HashSet<Cell>| {
122-
4 - plot
123-
.capital_neighbours()
124-
.iter()
125-
.filter(|n| region.contains(n))
126-
.count()
127-
};
128-
self.solve(input, count_edges)
164+
self.solve(input, Pricing::Perimeter)
129165
}
130166

131167
fn part_2(&self, input: &Self::Input) -> Self::Output2 {
132-
let corner_dirs = [
133-
[Direction::LeftAndUp, Direction::Left, Direction::Up],
134-
[Direction::RightAndUp, Direction::Right, Direction::Up],
135-
[Direction::RightAndDown, Direction::Right, Direction::Down],
136-
[Direction::LeftAndDown, Direction::Left, Direction::Down],
137-
];
138-
let matches = [
139-
[false, false, false],
140-
[true, false, false],
141-
[false, true, true],
142-
];
143-
let count_corners = |plot: &Cell, region: &HashSet<Cell>| {
144-
corner_dirs
145-
.iter()
146-
.filter(|d| {
147-
let test = (0..3)
148-
.map(|i| match plot.try_at(d[i]) {
149-
Some(n) => region.contains(&n),
150-
None => false,
151-
})
152-
.collect::<Vec<bool>>();
153-
matches.iter().any(|m| *m == *test)
154-
})
155-
.count()
156-
};
157-
self.solve(input, count_corners)
168+
self.solve(input, Pricing::NumberOfSides)
158169
}
159170

160171
fn samples(&self) {

src/main/rust/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy