type Dir = Vector2<i32>;
pub fn read_map(reader: &mut dyn BufRead) -> (Map, Pos) {
- let mut map = DMatrix::default();
let mut guard_position = Pos::default();
+ let mut data: Vec<bool> = Vec::new();
+ let mut nb_rows = 0;
+
for (i, l) in reader.lines().enumerate() {
- if map.nrows() < i + 1 {
- map = map.insert_row(i, false);
- }
+ nb_rows += 1;
for (j, c) in l.unwrap().chars().enumerate() {
- if map.ncols() < j + 1 {
- map = map.insert_column(j, false);
- }
- if c == '#' {
- map[(i, j)] = true;
- } else if c == '^' {
+ data.push(c == '#');
+ if c == '^' {
guard_position = Pos::new(i as i32, j as i32);
}
}
}
- (map, guard_position)
+ (
+ Map::from_row_slice(nb_rows, data.len() / nb_rows, &data),
+ guard_position,
+ )
}
const ROT_MAT: Matrix2<i32> = Matrix2::new(0, 1, -1, 0);
const EMPTY: char = ' ';
pub fn read(reader: &mut dyn BufRead) -> Antennae {
- let mut antennae = Antennae::default();
+ let mut data: Vec<char> = Vec::new();
+ let mut nb_rows = 0;
- for (i, l) in reader.lines().enumerate() {
- if antennae.nrows() < i + 1 {
- antennae = antennae.insert_row(i, EMPTY);
- }
- for (j, c) in l.unwrap().chars().enumerate() {
- if antennae.ncols() < j + 1 {
- antennae = antennae.insert_column(j, EMPTY);
- }
- if c != '.' {
- antennae[(i, j)] = c;
- }
+ for l in reader.lines() {
+ nb_rows += 1;
+ for c in l.unwrap().chars() {
+ data.push(if c == '.' { EMPTY } else { c });
}
}
- antennae
+ Antennae::from_row_slice(nb_rows, data.len() / nb_rows, &data)
}
pub enum AntinodeMode {
TwoPerPair,