Make Halo a part of the Part structure
This commit is contained in:
parent
659a9ce482
commit
234f53b8b5
107
2023/src/day3.rs
107
2023/src/day3.rs
|
@ -39,6 +39,32 @@ impl Part {
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn halo(&self, max_row: usize, max_col: usize) -> Vec<Addr> {
|
||||||
|
let mut halo: Vec<(i32, i32)> = vec![];
|
||||||
|
|
||||||
|
for r in self.start..self.end + 1 {
|
||||||
|
halo.push((self.row as i32 - 1, r as i32));
|
||||||
|
halo.push((self.row as i32 + 1, r as i32));
|
||||||
|
}
|
||||||
|
|
||||||
|
halo.push((self.row as i32 - 1, self.start as i32 - 1));
|
||||||
|
halo.push((self.row as i32, self.start as i32 - 1));
|
||||||
|
halo.push((self.row as i32 + 1, self.start as i32 - 1));
|
||||||
|
halo.push((self.row as i32 - 1, self.end as i32 + 1));
|
||||||
|
halo.push((self.row as i32, self.end as i32 + 1));
|
||||||
|
halo.push((self.row as i32 + 1, self.end as i32 + 1));
|
||||||
|
|
||||||
|
halo.into_iter()
|
||||||
|
.filter(|(row, col)| {
|
||||||
|
*row >= 0 && *row <= max_row as i32 && *col >= 0 && *col <= max_col as i32
|
||||||
|
})
|
||||||
|
.map(|(row, col)| Addr {
|
||||||
|
row: row as usize,
|
||||||
|
col: col as usize,
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
@ -79,16 +105,13 @@ fn find_gears(schematic: String) -> Vec<Gear> {
|
||||||
let splats = find_splats(sc)
|
let splats = find_splats(sc)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|addr| {
|
.map(|addr| {
|
||||||
halo(
|
Part {
|
||||||
Part {
|
value: 0,
|
||||||
value: 0,
|
row: addr.row,
|
||||||
row: addr.row,
|
start: addr.col,
|
||||||
start: addr.col,
|
end: addr.col,
|
||||||
end: addr.col,
|
}
|
||||||
},
|
.halo(max_row - 1, max_col - 1)
|
||||||
max_row - 1,
|
|
||||||
max_col - 1,
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<Vec<Addr>>>();
|
.collect::<Vec<Vec<Addr>>>();
|
||||||
|
|
||||||
|
@ -115,7 +138,7 @@ fn part_within_halo(halo1: Vec<Addr>, part: Part) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_part(schematic: &Vec<String>, part: Part) -> bool {
|
fn is_part(schematic: &Vec<String>, part: Part) -> bool {
|
||||||
halo(part.clone(), schematic.len() - 1, schematic[0].len() - 1)
|
part.halo(schematic.len() - 1, schematic[0].len() - 1)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.any(|addr| {
|
.any(|addr| {
|
||||||
let cell = schematic[addr.row].chars().nth(addr.col).unwrap();
|
let cell = schematic[addr.row].chars().nth(addr.col).unwrap();
|
||||||
|
@ -123,32 +146,6 @@ fn is_part(schematic: &Vec<String>, part: Part) -> bool {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn halo(addr: Part, max_row: usize, max_col: usize) -> Vec<Addr> {
|
|
||||||
let mut halo: Vec<(i32, i32)> = vec![];
|
|
||||||
|
|
||||||
for r in addr.start..addr.end + 1 {
|
|
||||||
halo.push((addr.row as i32 - 1, r as i32));
|
|
||||||
halo.push((addr.row as i32 + 1, r as i32));
|
|
||||||
}
|
|
||||||
|
|
||||||
halo.push((addr.row as i32 - 1, addr.start as i32 - 1));
|
|
||||||
halo.push((addr.row as i32, addr.start as i32 - 1));
|
|
||||||
halo.push((addr.row as i32 + 1, addr.start as i32 - 1));
|
|
||||||
halo.push((addr.row as i32 - 1, addr.end as i32 + 1));
|
|
||||||
halo.push((addr.row as i32, addr.end as i32 + 1));
|
|
||||||
halo.push((addr.row as i32 + 1, addr.end as i32 + 1));
|
|
||||||
|
|
||||||
halo.into_iter()
|
|
||||||
.filter(|(row, col)| {
|
|
||||||
*row >= 0 && *row <= max_row as i32 && *col >= 0 && *col <= max_col as i32
|
|
||||||
})
|
|
||||||
.map(|(row, col)| Addr {
|
|
||||||
row: row as usize,
|
|
||||||
col: col as usize,
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_numbers(schematic: String) -> Vec<Part> {
|
fn find_numbers(schematic: String) -> Vec<Part> {
|
||||||
let mut result = vec![];
|
let mut result = vec![];
|
||||||
for (idx, line) in schematic.lines().enumerate() {
|
for (idx, line) in schematic.lines().enumerate() {
|
||||||
|
@ -355,7 +352,7 @@ mod test {
|
||||||
end: 2,
|
end: 2,
|
||||||
};
|
};
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
halo(part, schematic.len() - 1, schematic[0].len() - 1),
|
part.halo(schematic.len() - 1, schematic[0].len() - 1),
|
||||||
vec![
|
vec![
|
||||||
Addr { row: 1, col: 0 },
|
Addr { row: 1, col: 0 },
|
||||||
Addr { row: 1, col: 1 },
|
Addr { row: 1, col: 1 },
|
||||||
|
@ -366,16 +363,13 @@ mod test {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
halo(
|
Part {
|
||||||
Part {
|
value: 0,
|
||||||
value: 0,
|
row: 1,
|
||||||
row: 1,
|
start: 1,
|
||||||
start: 1,
|
end: 1
|
||||||
end: 1
|
}
|
||||||
},
|
.halo(schematic.len() - 1, schematic[0].len() - 1),
|
||||||
schematic.len() - 1,
|
|
||||||
schematic[0].len() - 1
|
|
||||||
),
|
|
||||||
vec![
|
vec![
|
||||||
Addr { row: 0, col: 1 },
|
Addr { row: 0, col: 1 },
|
||||||
Addr { row: 2, col: 1 },
|
Addr { row: 2, col: 1 },
|
||||||
|
@ -388,16 +382,13 @@ mod test {
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
halo(
|
Part {
|
||||||
Part {
|
value: 0,
|
||||||
value: 0,
|
row: 6,
|
||||||
row: 6,
|
start: 2,
|
||||||
start: 2,
|
end: 4,
|
||||||
end: 4,
|
}
|
||||||
},
|
.halo(schematic.len() - 1, schematic[0].len() - 1),
|
||||||
schematic.len() - 1,
|
|
||||||
schematic[0].len() - 1
|
|
||||||
),
|
|
||||||
vec![
|
vec![
|
||||||
Addr { row: 5, col: 2 },
|
Addr { row: 5, col: 2 },
|
||||||
Addr { row: 7, col: 2 },
|
Addr { row: 7, col: 2 },
|
||||||
|
|
Loading…
Reference in New Issue