mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Simplify read_char_grid
This commit is contained in:
@@ -35,27 +35,11 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_char_grid(input: &mut dyn Read) -> Vec<Vec<u8>> {
|
pub fn read_char_grid(input: &mut dyn Read) -> Vec<Vec<u8>> {
|
||||||
let mut reader = BufReader::new(input);
|
BufReader::new(input)
|
||||||
let mut buffer = Vec::new();
|
.lines()
|
||||||
|
// filter_map avoids an expensive unwrap and we know our input is valid ascii
|
||||||
let mut grid = Vec::new();
|
.filter_map(|s| s.ok().map(String::into_bytes))
|
||||||
|
.collect()
|
||||||
while let Ok(read) = reader.read_until(b'\n', &mut buffer) {
|
|
||||||
if read == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
let line: &[u8] = if let Some(&b'\n') = buffer.last() {
|
|
||||||
&buffer[..(buffer.len() - 1)]
|
|
||||||
} else {
|
|
||||||
&buffer[..]
|
|
||||||
};
|
|
||||||
|
|
||||||
grid.push(line.to_owned());
|
|
||||||
buffer.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
grid
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An interface to count elements in particular categories.
|
/// An interface to count elements in particular categories.
|
||||||
|
|||||||
Reference in New Issue
Block a user