summaryrefslogtreecommitdiff
path: root/2024_rust/src/bin/day12.rs
diff options
context:
space:
mode:
authorGuillermo Ramos2024-12-14 17:27:23 +0100
committerGuillermo Ramos2024-12-14 17:37:08 +0100
commit7f09510a3169c13a4f1181ff24f978d42fcf58f5 (patch)
tree1561a93941be0ed5f9c3ef9583db8abd4505ab4b /2024_rust/src/bin/day12.rs
parent9e3f932e990eb2922b535fdf758791a5ee94774f (diff)
downloadAoC-7f09510a3169c13a4f1181ff24f978d42fcf58f5.tar.gz
2024.14
Diffstat (limited to '2024_rust/src/bin/day12.rs')
-rw-r--r--2024_rust/src/bin/day12.rs49
1 files changed, 26 insertions, 23 deletions
diff --git a/2024_rust/src/bin/day12.rs b/2024_rust/src/bin/day12.rs
index 21868aa..4408980 100644
--- a/2024_rust/src/bin/day12.rs
+++ b/2024_rust/src/bin/day12.rs
@@ -1,9 +1,7 @@
use aoc2024::matrix;
use std::collections::HashSet;
-static PLUS_DELTAS: [(isize, isize); 4] = [
- (-1, 0), (0, -1), (0, 1), (1, 0)
-];
+static PLUS_DELTAS: [(isize, isize); 4] = [(-1, 0), (0, -1), (0, 1), (1, 0)];
type Price = u32;
type Matrix = matrix::Matrix<char>;
@@ -23,7 +21,7 @@ fn regions_price(m: &Matrix, version: u8) -> Price {
for i in 0..m.limit.0 {
for j in 0..m.limit.1 {
let pos = (i, j);
- if ! visited.contains(&pos) {
+ if !visited.contains(&pos) {
let (price, pss) = region(m, pos, version);
result += price;
for p in pss {
@@ -78,7 +76,10 @@ struct Lines {
impl Lines {
fn new() -> Self {
- Lines { hs: vec![], vs: vec![] }
+ Lines {
+ hs: vec![],
+ vs: vec![],
+ }
}
fn from_pos(pos: matrix::Pos, in_region: &HashSet<matrix::Pos>) -> Self {
@@ -89,25 +90,21 @@ impl Lines {
// println!(" Checking p {p:?}");
let p_u = (p.0 as usize, p.1 as usize);
// println!(" Checking p_u {p_u:?}");
- if ! in_region.contains(&p_u) {
+ if !in_region.contains(&p_u) {
if p.0 == pos.0 as isize {
// println!(" PUSHING V ({} vs {}!", p.1, pos.1);
- vs.push(
- if p.1 < pos.1 as isize {
- (true, pos)
- } else {
- (false, (pos.0, pos.1+1))
- }
- )
+ vs.push(if p.1 < pos.1 as isize {
+ (true, pos)
+ } else {
+ (false, (pos.0, pos.1 + 1))
+ })
} else {
// println!(" PUSHING H ({} vs {}!", p.0, pos.0);
- hs.push(
- if p.0 < pos.0 as isize {
- (true, pos)
- } else {
- (false, (pos.0+1, pos.1))
- }
- )
+ hs.push(if p.0 < pos.0 as isize {
+ (true, pos)
+ } else {
+ (false, (pos.0 + 1, pos.1))
+ })
}
}
}
@@ -125,8 +122,15 @@ impl Lines {
self.hs[..].sort();
self.vs[..].sort_by(|(d1, (x1, y1)), (d2, (x2, y2))| (d1, y1, x1).cmp(&(d2, y2, x2)));
// println!("Sorted self: {self:?}");
- self.hs.chunk_by(|(d1, (x1, y1)), (d2, (x2, y2))| d1 == d2 && x1 == x2 && *y1 == y2-1).collect::<Vec<_>>().len() as u32 +
- self.vs.chunk_by(|(d1, (x1, y1)), (d2, (x2, y2))| d1 == d2 && y1 == y2 && *x1 == x2-1).collect::<Vec<_>>().len() as u32
+ self.hs
+ .chunk_by(|(d1, (x1, y1)), (d2, (x2, y2))| d1 == d2 && x1 == x2 && *y1 == y2 - 1)
+ .collect::<Vec<_>>()
+ .len() as u32
+ + self
+ .vs
+ .chunk_by(|(d1, (x1, y1)), (d2, (x2, y2))| d1 == d2 && y1 == y2 && *x1 == x2 - 1)
+ .collect::<Vec<_>>()
+ .len() as u32
}
}
@@ -134,7 +138,6 @@ impl Lines {
// xs.chunk_by(|().len()
// }
-
fn region(m: &Matrix, pos: matrix::Pos, version: u8) -> (Price, HashSet<matrix::Pos>) {
match version {
1 => region_v1(m, pos),