[Rust] enumerate() attach index to iterator

fn get_input() -> &'static str {
    return "..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#";
}

fn main() {
  let count = get_input()
    .lines()
    .enumerate()
    .flat_map(|(idx, line)| {
        return line.chars().nth(idx * 3 % line.len())
    })
    .filter(|&x| x == '#')
    .count();

  println!("{}", count)
}

 

posted @ 2024-02-20 15:42  Zhentiw  阅读(13)  评论(0编辑  收藏  举报