mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Avoid allocating the final buffer twice
This commit is contained in:
@@ -107,22 +107,17 @@ fn print_dots(dots: &[Coords]) -> String {
|
|||||||
(xc.max(xn as usize + 1), yc.max(yn as usize + 1))
|
(xc.max(xn as usize + 1), yc.max(yn as usize + 1))
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut buffer = vec![b' '; width * height];
|
let mut buffer = vec![b' '; (width + 1) * height - 1];
|
||||||
|
|
||||||
for &(x, y) in dots {
|
for &(x, y) in dots {
|
||||||
buffer[x as usize + width * y as usize] = b'#';
|
buffer[x as usize + (width + 1) * y as usize] = b'#';
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut result = String::with_capacity((width + 1) * height);
|
for line in buffer.chunks_exact_mut(width + 1) {
|
||||||
|
line[width] = b'\n';
|
||||||
for line in buffer.chunks_exact(width) {
|
|
||||||
result += std::str::from_utf8(line).unwrap();
|
|
||||||
result.push('\n');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result.pop();
|
String::from_utf8(buffer).unwrap()
|
||||||
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part1(input: &mut dyn Read) -> String {
|
pub fn part1(input: &mut dyn Read) -> String {
|
||||||
|
|||||||
Reference in New Issue
Block a user