From 09840f8fba1b2f96ba5d411c9348a190cd41fec9 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 12 Jan 2026 16:15:03 +0100 Subject: [PATCH] Update dependencies + code cleaning. --- Cargo.toml | 4 ++-- src/day01.rs | 1 - src/day02.rs | 20 +++++++++----------- src/day05.rs | 12 +++++------- src/day06.rs | 20 +++++++++++--------- src/day07.rs | 20 +++++++------------- src/day08.rs | 8 ++------ src/day09.rs | 4 ++-- src/day10.rs | 7 +++---- src/day11.rs | 2 +- src/day13.rs | 44 ++++++++++++++++++++------------------------ src/day14.rs | 2 +- 12 files changed, 63 insertions(+), 81 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5d2d9af..d436b78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,9 @@ edition = "2021" [dependencies] advent_of_code_common = { path = "advent_of_code_common" } -itertools = "0.13" +itertools = "0.14" regex = "1" -nalgebra = "0.33" +nalgebra = "0.34" num = "0.4" rustc-hash = "2.1" diff --git a/src/day01.rs b/src/day01.rs index dcc1522..bd356bf 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -8,7 +8,6 @@ pub fn read_location_ids_as_sorted(reader: &mut dyn BufRead) -> (Vec, Vec = l .unwrap() .split_whitespace() - .into_iter() .map(|n| n.parse::().unwrap()) .collect(); diff --git a/src/day02.rs b/src/day02.rs index 25ef44c..f022fee 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -32,7 +32,7 @@ pub fn nb_of_safe_reports_with_tolerance(reports: &Reports) -> i32 { let mut diff: Vec = levels.iter().tuple_windows().map(|(a, b)| b - a).collect(); if diff.iter().map(|v| v.signum()).sum::() < 0 { for v in diff.iter_mut() { - *v = *v * -1; + *v *= -1; } } @@ -53,17 +53,15 @@ pub fn nb_of_safe_reports_with_tolerance(reports: &Reports) -> i32 { if diff[p - 1] > 3 { diff[p - 1] += diff[p]; } + } else if diff[p - 1] > diff[p + 1] { + diff[p - 1] += diff[p]; + if diff[p - 1] == 0 { + return nb; + } } else { - if diff[p - 1] > diff[p + 1] { - diff[p - 1] += diff[p]; - if diff[p - 1] == 0 { - return nb; - } - } else { - diff[p + 1] += diff[p]; - if diff[p + 1] == 0 { - return nb; - } + diff[p + 1] += diff[p]; + if diff[p + 1] == 0 { + return nb; } } } diff --git a/src/day05.rs b/src/day05.rs index 9693975..e3ff33c 100644 --- a/src/day05.rs +++ b/src/day05.rs @@ -35,17 +35,17 @@ pub fn read_ordering_and_updates(reader: &mut dyn BufRead) -> (PageOrdering, Upd let mut lines_iter = lines.iter(); let mut page_ordering = PageOrdering::new(); - while let Some(l) = lines_iter.next() { + for l in lines_iter.by_ref() { if l.is_empty() { break; } - let pages = utils::split_to_vec(&l, "|"); + let pages = utils::split_to_vec(l, "|"); page_ordering.add_page_order(pages[0], pages[1]); } let mut updates: Updates = vec![]; - while let Some(l) = lines_iter.next() { - updates.push(utils::split_to_vec(&l, ",")); + for l in lines_iter.by_ref() { + updates.push(utils::split_to_vec(l, ",")); } (page_ordering, updates) @@ -82,9 +82,7 @@ pub fn sum_of_mid_page_from_corrected_updates( { updates_corrected.swap(i, i + 1); modified = true; - if i > 0 { - i -= 1; - } + i = i.saturating_sub(1); } else { i += 1; } diff --git a/src/day06.rs b/src/day06.rs index 6c198c8..9363f1c 100644 --- a/src/day06.rs +++ b/src/day06.rs @@ -107,13 +107,12 @@ pub fn nb_possible_obstruction_position(map: &Map, initial_pos: Pos) -> usize { if inside(map, obstacle_pos) && !map[(obstacle_pos.x as usize, obstacle_pos.y as usize)] && map_visited[(obstacle_pos.x as usize, obstacle_pos.y as usize)].is_none() - { - if does_it_loop( + && does_it_loop( PathIterator::new(map, pos, dir, Some(obstacle_pos)), &map_visited, - ) { - nb_loops += 1; - } + ) + { + nb_loops += 1; } } nb_loops @@ -123,13 +122,16 @@ fn does_it_loop(path: PathIterator, map_visited: &DMatrix>) -> bool let mut map_visited = map_visited.clone(); for (pos, dir) in path { let visited = map_visited[(pos.x as usize, pos.y as usize)]; - if visited.is_none() { + + if let Some(visited) = visited { + if visited == dir { + return true; + } + } else { map_visited[(pos.x as usize, pos.y as usize)] = Some(dir); - } else if visited.unwrap() == dir { - return true; } } - return false; + false } #[cfg(test)] diff --git a/src/day07.rs b/src/day07.rs index 8d03c75..e7723e7 100644 --- a/src/day07.rs +++ b/src/day07.rs @@ -13,7 +13,7 @@ pub fn read(reader: &mut dyn BufRead) -> Vec { .lines() .map(|l| { let l = l.unwrap(); - let mut result_and_rest = l.split(": ").into_iter(); + let mut result_and_rest = l.split(": "); Equation { result: result_and_rest.next().unwrap().parse().unwrap(), operands: utils::split_to_vec(result_and_rest.next().unwrap(), " "), @@ -29,10 +29,8 @@ pub fn sum_valid_equations(equations: &[Equation]) -> i64 { return result == operands[0]; } let last = operands[l - 1]; - if result % last == 0 { - if is_valid(result / last, &operands[0..l - 1]) { - return true; - } + if result % last == 0 && is_valid(result / last, &operands[0..l - 1]) { + return true; } let sub = result - last; if sub >= 0 { @@ -59,10 +57,8 @@ pub fn sum_valid_equations_with_concat(equations: &[Equation]) -> i64 { return result == operands[0]; } let last = operands[l - 1]; - if result % last == 0 { - if is_valid(result / last, &operands[0..l - 1]) { - return true; - } + if result % last == 0 && is_valid(result / last, &operands[0..l - 1]) { + return true; } let sub = result - last; if sub >= 0 { @@ -71,10 +67,8 @@ pub fn sum_valid_equations_with_concat(equations: &[Equation]) -> i64 { } let p = 10i64.pow(utils::nb_of_digits_i64(last)); - if sub % p == 0 { - if is_valid(sub / p, &operands[0..l - 1]) { - return true; - } + if sub % p == 0 && is_valid(sub / p, &operands[0..l - 1]) { + return true; } } false diff --git a/src/day08.rs b/src/day08.rs index 8b70448..c8641d1 100644 --- a/src/day08.rs +++ b/src/day08.rs @@ -77,10 +77,7 @@ pub fn nb_antinodes(antennae: &Antennae, mode: AntinodeMode) -> i32 { for i in 0..nr { let current = antennae[(i, j)]; if current != EMPTY { - antennae_positions - .entry(current) - .or_insert(Vec::new()) - .push((i, j)); + antennae_positions.entry(current).or_default().push((i, j)); } } } @@ -88,8 +85,7 @@ pub fn nb_antinodes(antennae: &Antennae, mode: AntinodeMode) -> i32 { for (x, y) in positions .iter() .combinations(2) - .map(|positions| antinode_positions(positions, &mode, (nr, nc))) - .flatten() + .flat_map(|positions| antinode_positions(positions, &mode, (nr, nc))) { if !antinodes[(x as usize, y as usize)] { antinodes[(x as usize, y as usize)] = true; diff --git a/src/day09.rs b/src/day09.rs index 7bb59f8..c0d271d 100644 --- a/src/day09.rs +++ b/src/day09.rs @@ -29,8 +29,8 @@ pub fn read(reader: &mut dyn BufRead) -> Vec { pub fn defrag_v1(mut memory: Vec) -> Vec { fn next_empty_space_pos(from: usize, memory: &[u32]) -> usize { - for i in from..memory.len() { - if memory[i] == EMPTY { + for (i, v) in memory.iter().enumerate().skip(from) { + if *v == EMPTY { return i; } } diff --git a/src/day10.rs b/src/day10.rs index b28ec58..ea4f074 100644 --- a/src/day10.rs +++ b/src/day10.rs @@ -60,9 +60,9 @@ pub fn score(map: &Map, start_positions: &[Position], multiple_paths: bool) -> u } start_positions - .into_iter() + .iter() .map(|pos| { - let n = get_nb_summits( + get_nb_summits( *pos, map, &mut if multiple_paths { @@ -70,8 +70,7 @@ pub fn score(map: &Map, start_positions: &[Position], multiple_paths: bool) -> u } else { Some(DMatrix::::repeat(map.nrows(), map.ncols(), false)) }, - ); - n + ) }) .sum() } diff --git a/src/day11.rs b/src/day11.rs index 58a059d..48c7df9 100644 --- a/src/day11.rs +++ b/src/day11.rs @@ -23,7 +23,7 @@ pub fn blink(mut stones: Stones, i: u32) -> Stones { add_or_set(&mut stones, 1, n); } else { let nb_digits = utils::nb_of_digits_u64(stone); - if nb_digits % 2 == 0 { + if nb_digits.is_multiple_of(2) { let base = 10u64.pow(nb_digits / 2); add_or_set(&mut stones, stone / base, n); add_or_set(&mut stones, stone % base, n); diff --git a/src/day13.rs b/src/day13.rs index e84087d..3a34ded 100644 --- a/src/day13.rs +++ b/src/day13.rs @@ -12,30 +12,26 @@ pub fn read(reader: &mut dyn BufRead) -> Equations { let mut equations = Vec::new(); let mut iter = reader.lines(); - loop { - if let Some(Ok(line1)) = iter.next() { - let a_cap = button_a.captures(&line1).unwrap(); - let a_factors = Vector2::new( - a_cap[1].parse::().unwrap(), - a_cap[2].parse::().unwrap(), - ); - let line2 = iter.next().unwrap().unwrap(); - let b_cap = button_b.captures(&line2).unwrap(); - let b_factors = Vector2::new( - b_cap[1].parse::().unwrap(), - b_cap[2].parse::().unwrap(), - ); - let line3 = iter.next().unwrap().unwrap(); - let prize_cap = prize.captures(&line3).unwrap(); - let prize_xy = Vector2::new( - prize_cap[1].parse::().unwrap(), - prize_cap[2].parse::().unwrap(), - ); - let _ = iter.next(); - equations.push((a_factors, b_factors, prize_xy)); - } else { - break; - } + while let Some(Ok(line1)) = iter.next() { + let a_cap = button_a.captures(&line1).unwrap(); + let a_factors = Vector2::new( + a_cap[1].parse::().unwrap(), + a_cap[2].parse::().unwrap(), + ); + let line2 = iter.next().unwrap().unwrap(); + let b_cap = button_b.captures(&line2).unwrap(); + let b_factors = Vector2::new( + b_cap[1].parse::().unwrap(), + b_cap[2].parse::().unwrap(), + ); + let line3 = iter.next().unwrap().unwrap(); + let prize_cap = prize.captures(&line3).unwrap(); + let prize_xy = Vector2::new( + prize_cap[1].parse::().unwrap(), + prize_cap[2].parse::().unwrap(), + ); + let _ = iter.next(); + equations.push((a_factors, b_factors, prize_xy)); } equations } diff --git a/src/day14.rs b/src/day14.rs index 9f3296b..433cdb8 100644 --- a/src/day14.rs +++ b/src/day14.rs @@ -33,7 +33,7 @@ fn robot_positions<'a>( area_size: (i64, i64), seconds: i64, ) -> impl Iterator + use<'a> { - robots.into_iter().map(move |(pos, vel)| { + robots.iter().map(move |(pos, vel)| { let pos = pos + vel * seconds; ( pos[0].rem_euclid(area_size.0), -- 2.51.0