diff --git a/src/bin/06.rs b/src/bin/06.rs index 68c9c1c..3e06637 100644 --- a/src/bin/06.rs +++ b/src/bin/06.rs @@ -5,11 +5,11 @@ advent_of_code::solution!(6); const DIRS: [(isize, isize); 4] = [(-1, 0), (0, 1), (1, 0), (0, -1)]; -fn in_bounds(map: &Vec>, pos: (isize, isize)) -> bool { +fn in_bounds(map: &[Vec], pos: (isize, isize)) -> bool { pos.0 >= 0 && pos.1 >= 0 && pos.0 < map.len() as isize && pos.1 < map[0].len() as isize } -fn check_map(map: &Vec>, start: (isize, isize)) -> (bool, Vec<(isize, isize)>) { +fn check_map(map: &[Vec], start: (isize, isize)) -> (bool, Vec<(isize, isize)>) { let mut visited: HashSet<(isize, isize)> = HashSet::new(); let mut visited_dir: HashSet<((isize, isize), usize)> = HashSet::new(); let mut dir = 0; @@ -22,7 +22,7 @@ fn check_map(map: &Vec>, start: (isize, isize)) -> (bool, Vec<(isize, visited_dir.insert((current, dir)); let next = (current.0 + DIRS[dir].0, current.1 + DIRS[dir].1); if !in_bounds(map, next) { - return (false, visited.iter().map(|x| *x).collect()); + return (false, visited.iter().copied().collect()); } if map[next.0 as usize][next.1 as usize] == '#' { dir = (dir + 1) % 4; @@ -35,9 +35,9 @@ fn check_map(map: &Vec>, start: (isize, isize)) -> (bool, Vec<(isize, fn parse_input(input: &str) -> (Vec>, (isize, isize)) { let map: Vec> = input.lines().map(|line| line.chars().collect()).collect(); let mut start: (isize, isize) = (0, 0); - for i in 0..map.len() { - for j in 0..map[i].len() { - if map[i][j] == '^' { + for (i, row) in map.iter().enumerate() { + for (j, cell) in row.iter().enumerate() { + if *cell == '^' { start = (i as isize, j as isize); } } diff --git a/src/bin/07.rs b/src/bin/07.rs index 9520270..5d52f80 100644 --- a/src/bin/07.rs +++ b/src/bin/07.rs @@ -32,7 +32,7 @@ fn solve(input: &str, skip: usize) -> Option { .multi_cartesian_product() .any(|c| { let mut c = c.into_iter(); - numbers.iter().map(|x| *x).reduce(|acc, n| { + numbers.iter().copied().reduce(|acc, n| { let op = c.next().unwrap(); match op { Operation::Add => acc + n, diff --git a/src/bin/08.rs b/src/bin/08.rs index 963ab0f..feaf4e1 100644 --- a/src/bin/08.rs +++ b/src/bin/08.rs @@ -2,17 +2,17 @@ use std::collections::HashSet; advent_of_code::solution!(8); -fn in_bounds(map: &Vec>, pos: (i32, i32)) -> bool { +fn in_bounds(map: &[Vec], pos: (i32, i32)) -> bool { pos.0 >= 0 && pos.1 >= 0 && pos.0 < map.len() as i32 && pos.1 < map[0].len() as i32 } pub fn solve + Clone>(input: &str, range: R) -> Option { let map: Vec> = input.lines().map(|line| line.chars().collect()).collect(); let mut antennas: Vec<((usize, usize), char)> = Vec::new(); - for i in 0..map.len() { - for j in 0..map[i].len() { - if map[i][j] != '.' { - antennas.push(((i, j), map[i][j])); + for (i, row) in map.iter().enumerate() { + for (j, cell) in row.iter().enumerate() { + if *cell != '.' { + antennas.push(((i, j), *cell)); } } }