diff --git a/aoc2015/src/day02.rs b/aoc2015/src/day02.rs index bb5a7d6..79aa3ec 100644 --- a/aoc2015/src/day02.rs +++ b/aoc2015/src/day02.rs @@ -8,10 +8,7 @@ const INPUT: &str = include_str!("../input/day02.txt"); pub fn run() -> Result { let mut res = String::with_capacity(128); - let presents: Vec = INPUT - .lines() - .map(|line| line.parse()) - .collect::>()?; + let presents: Vec = INPUT.lines().map(str::parse).collect::>()?; writeln!(res, "part 1: {}", part1(&presents))?; writeln!(res, "part 2: {}", part2(&presents))?; @@ -104,7 +101,7 @@ mod tests { fn part1_real() { let presents: Vec = INPUT .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>() .unwrap(); @@ -121,7 +118,7 @@ mod tests { fn part2_real() { let presents: Vec = INPUT .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>() .unwrap(); diff --git a/aoc2015/src/day05.rs b/aoc2015/src/day05.rs index e696295..942a5e5 100644 --- a/aoc2015/src/day05.rs +++ b/aoc2015/src/day05.rs @@ -16,7 +16,7 @@ pub fn run() -> Result { fn part1(input: &str) -> usize { input .lines() - .map(|line| line.trim_end()) + .map(str::trim_end) .filter(|line| { let mut vowel_count = 0; for c in line.chars() { @@ -51,7 +51,7 @@ fn part1(input: &str) -> usize { fn part2(input: &str) -> usize { input .lines() - .map(|line| line.trim_end()) + .map(str::trim_end) .filter(|line| { for i in 0..(line.chars().count() - 3) { let seq = &line[i..(i + 2)]; diff --git a/aoc2018/src/day04.rs b/aoc2018/src/day04.rs index b8c837b..879bf67 100644 --- a/aoc2018/src/day04.rs +++ b/aoc2018/src/day04.rs @@ -61,8 +61,8 @@ impl FromStr for Date { type Err = anyhow::Error; fn from_str(s: &str) -> Result { - let lbracket = s.find('[').context("`[` not found")?; - let s = &s[(lbracket + 1)..]; + let l_bracket = s.find('[').context("`[` not found")?; + let s = &s[(l_bracket + 1)..]; let dash = s.find('-').context("`-` not found")?; let year = s[..dash].parse()?; @@ -79,9 +79,9 @@ impl FromStr for Date { let hour = s[..colon].parse()?; let s = &s[(colon + 1)..]; - let rbracket = s.find(']').context("`]` not found")?; + let r_bracket = s.find(']').context("`]` not found")?; - let minute = s[..rbracket].parse()?; + let minute = s[..r_bracket].parse()?; Ok(Date { year, diff --git a/aoc2019/src/day01.rs b/aoc2019/src/day01.rs index cd63724..5f7b0a1 100644 --- a/aoc2019/src/day01.rs +++ b/aoc2019/src/day01.rs @@ -20,7 +20,7 @@ fn fuel_needed(module_weight: u64) -> u64 { fn part1(input: &str) -> Result { input .lines() - .map(|line| line.parse::()) + .map(str::parse) .map(|w| w.map(fuel_needed).map_err(anyhow::Error::new)) .sum() } @@ -40,7 +40,7 @@ fn cumulated_fuel_needed(module_weight: u64) -> u64 { fn part2(input: &str) -> Result { input .lines() - .map(|line| line.parse::()) + .map(str::parse) .map(|w| w.map(cumulated_fuel_needed).map_err(anyhow::Error::new)) .sum() } diff --git a/aoc2019/src/day03.rs b/aoc2019/src/day03.rs index 471d897..d77b719 100644 --- a/aoc2019/src/day03.rs +++ b/aoc2019/src/day03.rs @@ -33,9 +33,8 @@ fn part1(first_wire: &Wire, second_wire: &Wire) -> Result { }) .filter_map(|(f, s)| match f.intersection(s) { Some(Point { x: 0, y: 0 }) | None => None, - Some(p) => Some(p), + Some(p) => Some(manhattan_distance(&p, &Point { x: 0, y: 0 })), }) - .map(|inter| manhattan_distance(&inter, &Point { x: 0, y: 0 })) .min() .context("wire was empty") } @@ -45,10 +44,10 @@ fn part2(first_wire: &Wire, second_wire: &Wire) -> Result { let mut first_length = 0; - for seg1 in first_wire.0.iter() { + for seg1 in &first_wire.0 { let mut second_length = 0; - for seg2 in second_wire.0.iter() { + for seg2 in &second_wire.0 { if let Some(inter) = seg1.intersection(&seg2) { if inter.x == 0 && inter.y == 0 { continue; diff --git a/aoc2019/src/day06.rs b/aoc2019/src/day06.rs index 2980f09..3c7a134 100644 --- a/aoc2019/src/day06.rs +++ b/aoc2019/src/day06.rs @@ -37,7 +37,7 @@ fn count_orbits( fn part1(input: &str) -> Result { let orbits = input .lines() - .map(|line| line.trim_end()) + .map(str::trim_end) .map(|line| { let paren = line .find(')') @@ -56,7 +56,7 @@ fn part1(input: &str) -> Result { fn part2(input: &str) -> Result { let orbits = input .lines() - .map(|line| line.trim_end()) + .map(str::trim_end) .map(|line| { let paren = line .find(')') diff --git a/aoc2019/src/day07.rs b/aoc2019/src/day07.rs index 66f7ea3..893b4b9 100644 --- a/aoc2019/src/day07.rs +++ b/aoc2019/src/day07.rs @@ -48,7 +48,7 @@ fn part1(input: &str) -> Result { let mut res = 0; for combination in combinations { let mut output = 0; - for phase in combination.iter() { + for phase in &combination { let mut intcode = Intcode::with_memory(memory.clone()); intcode.add_input(*phase); @@ -95,7 +95,7 @@ fn part2(input: &str) -> Result { intcode.run_and_wait()?; - for out in intcode.output.iter() { + for out in &intcode.output { next.add_input(*out); } intcode.output.clear(); @@ -116,7 +116,7 @@ fn part2(input: &str) -> Result { None => bail!("last amplifier halted without output"), }; } else { - for out in last.output.iter() { + for out in &last.output { first.add_input(*out); } last.output.clear(); diff --git a/aoc2019/src/day08.rs b/aoc2019/src/day08.rs index 412f104..1d6d6d2 100644 --- a/aoc2019/src/day08.rs +++ b/aoc2019/src/day08.rs @@ -96,7 +96,7 @@ impl FromStr for Image { let mut result = vec![vec![2; IMG_WIDTH]; IMG_HEIGHT]; // overlap layers - for layer in layers.iter() { + for layer in &layers { for (src_row, dst_row) in layer.pixels.iter().zip(result.iter_mut()) { for (src_pixel, dst_pixel) in src_row.iter().zip(dst_row.iter_mut()) { if let 2 = *dst_pixel { diff --git a/aoc2019/src/day10.rs b/aoc2019/src/day10.rs index 71f563d..89d63d8 100644 --- a/aoc2019/src/day10.rs +++ b/aoc2019/src/day10.rs @@ -48,10 +48,10 @@ fn part1(input: &str) -> Result { } let mut best = None; - for a in asteroids.iter() { + for a in &asteroids { let mut set = HashSet::new(); - for b in asteroids.iter() { + for b in &asteroids { if a == b { continue; } diff --git a/aoc2019/src/day12.rs b/aoc2019/src/day12.rs index 10737b4..fa3aa55 100644 --- a/aoc2019/src/day12.rs +++ b/aoc2019/src/day12.rs @@ -40,7 +40,7 @@ fn generate_pairs(n: usize) -> Vec<(usize, usize)> { } fn parse_planets(input: &str) -> Result> { - input.lines().map(|l| l.parse()).collect() + input.lines().map(str::parse).collect() } fn part1(mut planets: Vec, steps: usize) -> Result { @@ -91,12 +91,12 @@ fn part1(mut planets: Vec, steps: usize) -> Result { } // update position - for planet in planets.iter_mut() { + for planet in &mut planets { planet.update_pos(); } } - Ok(planets.iter().map(|p| p.total_energy()).sum()) + Ok(planets.iter().map(Planet::total_energy).sum()) } fn gcd(a: usize, b: usize) -> usize { @@ -165,7 +165,7 @@ fn part2(mut planets: Vec) -> Result { } // update position - for planet in planets.iter_mut() { + for planet in &mut planets { planet.update_pos(); } diff --git a/aoc2019/src/day14.rs b/aoc2019/src/day14.rs index 398953a..4462914 100644 --- a/aoc2019/src/day14.rs +++ b/aoc2019/src/day14.rs @@ -79,7 +79,7 @@ fn get_ore_cost( let needed = quantity - in_stock; let num_reactions = (needed + recipe.produced - 1) / recipe.produced; - for elem in recipe.elems.iter() { + for elem in &recipe.elems { total += get_ore_cost( elem.name.clone(), elem.amount * num_reactions, diff --git a/aoc2019/src/intcode/mod.rs b/aoc2019/src/intcode/mod.rs index 68d8e51..a93ba8b 100644 --- a/aoc2019/src/intcode/mod.rs +++ b/aoc2019/src/intcode/mod.rs @@ -203,10 +203,10 @@ impl Intcode { bail!("dst must be a valid address: {}", dst); } - if val != 0 { - self.ip = dst as usize; - } else { + if val == 0 { self.ip += 3; + } else { + self.ip = dst as usize; } } Opcode::JumpFalse(test, dst) => { diff --git a/aoc2020/src/day02.rs b/aoc2020/src/day02.rs index 2491d75..8f70669 100644 --- a/aoc2020/src/day02.rs +++ b/aoc2020/src/day02.rs @@ -92,7 +92,7 @@ pub fn run() -> Result { fn part1(input: &str) -> Result { let policies = input .lines() - .map(|line| line.parse::()) + .map(str::parse::) .collect::>>()?; Ok(policies @@ -104,7 +104,7 @@ fn part1(input: &str) -> Result { fn part2(input: &str) -> Result { let policies = input .lines() - .map(|line| line.parse::()) + .map(str::parse::) .collect::>>()?; Ok(policies diff --git a/aoc2020/src/day04.rs b/aoc2020/src/day04.rs index 6c44372..f79a908 100644 --- a/aoc2020/src/day04.rs +++ b/aoc2020/src/day04.rs @@ -48,8 +48,8 @@ fn part2(input: &str) -> Result { Ok(passports .into_iter() - .filter_map(|p| p.complete()) - .filter(|p| p.is_valid()) + .filter_map(Passport::complete) + .filter(CompletePassport::is_valid) .count()) } diff --git a/aoc2020/src/day05.rs b/aoc2020/src/day05.rs index 198f084..599f304 100644 --- a/aoc2020/src/day05.rs +++ b/aoc2020/src/day05.rs @@ -17,12 +17,12 @@ pub fn run() -> Result { fn part1(input: &str) -> Result { let seats = input .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>>()?; seats .iter() - .map(|seat| seat.id()) + .map(Seat::id) .max() .context("0 seats processed") } @@ -30,7 +30,7 @@ fn part1(input: &str) -> Result { fn part2(input: &str) -> Result { let mut seats = input .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>>()?; // Seats will be sorted by lexicographical order of fields thanks to `derive(PartialOrd, Ord)`, @@ -39,7 +39,7 @@ fn part2(input: &str) -> Result { seats.sort_unstable(); let mut prev_id = None; - for id in seats.iter().map(|s| s.id()) { + for id in seats.iter().map(Seat::id) { match prev_id { Some(pid) if pid == (id - 1) => prev_id = Some(id), Some(pid) => return Ok(pid + 1), diff --git a/aoc2020/src/day07.rs b/aoc2020/src/day07.rs index f925192..b846d51 100644 --- a/aoc2020/src/day07.rs +++ b/aoc2020/src/day07.rs @@ -18,7 +18,7 @@ pub fn run() -> Result { fn part1(input: &str) -> Result { let bag_rules = input .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>>() .unwrap(); @@ -42,7 +42,7 @@ fn part1(input: &str) -> Result { fn part2(input: &str) -> Result { let bag_rules = input .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>>() .unwrap(); @@ -168,7 +168,7 @@ mod tests { fn part1_provided_parse() { let bag_rules = PROVIDED1 .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>>() .unwrap(); diff --git a/aoc2020/src/day08.rs b/aoc2020/src/day08.rs index 6e057b9..f24ee3e 100644 --- a/aoc2020/src/day08.rs +++ b/aoc2020/src/day08.rs @@ -17,7 +17,7 @@ pub fn run() -> Result { fn part1(input: &str) -> Result { let instructions = input .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>>()?; let mut interpreter = Interpreter::new(instructions); @@ -31,7 +31,7 @@ fn part1(input: &str) -> Result { fn part2(input: &str) -> Result { let instructions = input .lines() - .map(|line| line.parse()) + .map(str::parse) .collect::>>()?; for idx in 0..instructions.len() { diff --git a/aoc2020/src/day12.rs b/aoc2020/src/day12.rs index 5470292..b88f137 100644 --- a/aoc2020/src/day12.rs +++ b/aoc2020/src/day12.rs @@ -14,10 +14,7 @@ pub fn run() -> Result { } fn part1(input: &str) -> Result { - let actions: Vec = input - .lines() - .map(|line| line.parse()) - .collect::>()?; + let actions: Vec = input.lines().map(str::parse).collect::>()?; let mut ship = Ship::new(); @@ -29,10 +26,7 @@ fn part1(input: &str) -> Result { } fn part2(input: &str) -> Result { - let actions: Vec = input - .lines() - .map(|line| line.parse()) - .collect::>()?; + let actions: Vec = input.lines().map(str::parse).collect::>()?; let mut ship = Ship::new(); @@ -212,7 +206,7 @@ impl Ship { } ActionKind::Forward => { - for mv in self.waypoint.as_moves(action.arg).iter() { + for mv in &self.waypoint.as_moves(action.arg) { self.process(mv); } } diff --git a/aoc2020/src/day14.rs b/aoc2020/src/day14.rs index a2ef9b4..aaed831 100644 --- a/aoc2020/src/day14.rs +++ b/aoc2020/src/day14.rs @@ -294,7 +294,7 @@ impl std::str::FromStr for Program { type Err = anyhow::Error; fn from_str(s: &str) -> Result { - let instructions = s.lines().map(|line| line.parse()).collect::>()?; + let instructions = s.lines().map(str::parse).collect::>()?; Ok(Program { instructions, diff --git a/aoc2020/src/day16.rs b/aoc2020/src/day16.rs index 9d0dcdb..69f30d2 100644 --- a/aoc2020/src/day16.rs +++ b/aoc2020/src/day16.rs @@ -120,13 +120,13 @@ fn parse_input(input: &str) -> Result<(HashMap<&str, Field>, Ticket, Vec let my_ticket = my_ticket_part .lines() .skip(1) - .map(|line| line.parse()) + .map(str::parse) .next() .context("no second line for ticket")??; let tickets = tickets_part .lines() .skip(1) - .map(|line| line.parse()) + .map(str::parse) .collect::>() .context("couldn't parse tickets")?; @@ -184,16 +184,19 @@ impl std::str::FromStr for Field { fn from_str(s: &str) -> Result { let mut ranges = s.split(" or "); - let mut range1 = ranges.next().context("no first range found")?.split('-'); - let range1_start = range1.next().context("no bound for range")?.parse()?; - let range1_end = range1.next().context("no bound for range")?.parse()?; + let mut first_range = ranges.next().context("no first range found")?.split('-'); + let first_range_start = first_range.next().context("no bound for range")?.parse()?; + let first_range_end = first_range.next().context("no bound for range")?.parse()?; - let mut range2 = ranges.next().context("no second range found")?.split('-'); - let range2_start = range2.next().context("no bound for range")?.parse()?; - let range2_end = range2.next().context("no bound for range")?.parse()?; + let mut second_range = ranges.next().context("no second range found")?.split('-'); + let second_range_start = second_range.next().context("no bound for range")?.parse()?; + let second_range_end = second_range.next().context("no bound for range")?.parse()?; Ok(Field { - ranges: (range1_start..=range1_end, range2_start..=range2_end), + ranges: ( + first_range_start..=first_range_end, + second_range_start..=second_range_end, + ), }) } }