diff --git a/2022/benches/days.rs b/2022/benches/days.rs index 68dd88d..2c85012 100644 --- a/2022/benches/days.rs +++ b/2022/benches/days.rs @@ -11,7 +11,7 @@ use criterion::Criterion; const DAYS_IMPLEMENTED: u8 = 25; fn read_input(day: u8) -> std::io::Result> { - let input_path = format!("inputs/{:02}.txt", day); + let input_path = format!("inputs/{day:02}.txt"); let mut buffer = Vec::new(); File::open(input_path)?.read_to_end(&mut buffer)?; diff --git a/2022/src/common.rs b/2022/src/common.rs index d52e041..baa9b63 100644 --- a/2022/src/common.rs +++ b/2022/src/common.rs @@ -182,8 +182,7 @@ impl IndexSet { self.0 .get(entry) - .map(|&entry| (entry & (1 << pos) != 0)) - .unwrap_or(false) + .map_or(false, |&entry| (entry & (1 << pos) != 0)) } } diff --git a/2022/src/day07.rs b/2022/src/day07.rs index 4642d27..f484e0c 100644 --- a/2022/src/day07.rs +++ b/2022/src/day07.rs @@ -84,14 +84,14 @@ fn parse_program(input: &[u8]) -> IResult<&[u8], (u32, Vec)> { pub fn part1(input: &[u8]) -> Result { let (_, sizes) = parse_input(input, parse_program)?; - let searched_size: u32 = sizes.into_iter().filter(|&size| size <= 100000).sum(); + let searched_size: u32 = sizes.into_iter().filter(|&size| size <= 100_000).sum(); Ok(searched_size.to_string()) } pub fn part2(input: &[u8]) -> Result { - const TARGET: u32 = 30000000; - const TOTAL: u32 = 70000000; + const TARGET: u32 = 30_000_000; + const TOTAL: u32 = 70_000_000; let (used, sizes) = parse_input(input, parse_program)?; diff --git a/2022/src/day11.rs b/2022/src/day11.rs index 2a1fdb3..4736fe7 100644 --- a/2022/src/day11.rs +++ b/2022/src/day11.rs @@ -53,7 +53,7 @@ impl Monkey { // Miraculously get less worried new_val /= 3; - drains[(new_val % self.test_mod == 0) as usize].push(new_val); + drains[usize::from(new_val % self.test_mod == 0)].push(new_val); } } @@ -65,7 +65,7 @@ impl Monkey { // Modular arithmetic is a good way to get less worried new_val = new_val % mod_base; - drains[(new_val % self.test_mod == 0) as usize].push(new_val); + drains[usize::from(new_val % self.test_mod == 0)].push(new_val); } } } diff --git a/2022/src/day15.rs b/2022/src/day15.rs index 3149342..6e1476d 100644 --- a/2022/src/day15.rs +++ b/2022/src/day15.rs @@ -108,7 +108,7 @@ fn part1_generic(input: &[u8]) -> Result { } pub fn part1(input: &[u8]) -> Result { - part1_generic::<2000000>(input) + part1_generic::<2_000_000>(input) } fn part2_generic(input: &[u8]) -> Result { @@ -153,11 +153,11 @@ fn part2_generic(input: &[u8]) -> Result { let Vec2([x, y]) = find_unseen::(&beacons)?; - Ok((i64::from(x) * 4000000 + i64::from(y)).to_string()) + Ok((i64::from(x) * 4_000_000 + i64::from(y)).to_string()) } pub fn part2(input: &[u8]) -> Result { - part2_generic::<4000000>(input) + part2_generic::<4_000_000>(input) } #[cfg(test)] diff --git a/2022/src/day16.rs b/2022/src/day16.rs index d8a3691..2871d89 100644 --- a/2022/src/day16.rs +++ b/2022/src/day16.rs @@ -61,7 +61,7 @@ impl From> for SimpleNetwork { .map(|(_, flow, connected)| { let connected = connected.into_iter().map(|name| mapping[&name]).collect(); - SimpleValve { flow, connected } + SimpleValve { connected, flow } }) .collect(), start: mapping[&b"AA"[..]], diff --git a/2022/src/day17.rs b/2022/src/day17.rs index a8270e0..bd27597 100644 --- a/2022/src/day17.rs +++ b/2022/src/day17.rs @@ -154,7 +154,7 @@ pub fn part2(input: &[u8]) -> Result { let mut tortoise_time = 0; let mut last_gust = 0; - const TARGET: usize = 1000000000000; + const TARGET: usize = 1_000_000_000_000; for (it, (shape_id, &shape)) in shapes.by_ref().enumerate() { let mut x = 2usize; diff --git a/2022/src/day19.rs b/2022/src/day19.rs index 755c6af..3716251 100644 --- a/2022/src/day19.rs +++ b/2022/src/day19.rs @@ -105,17 +105,13 @@ impl BluePrint { machines, }) = todo.pop() { - let ideal_from_now = ideal(time_left as u32); + let ideal_from_now = ideal(u32::from(time_left)); // Need to check again because we might've gotten a better result in the meantime. if u32::from(best - got) >= ideal_from_now { continue; } - if todo.len() > 1_000_000 { - panic!( - "Safety: got a todo list of len {}, best: {best}", - todo.len() - ); - } + assert!(todo.len() <= 1_000_000, "Safety: got a todo list of len {}, best: {best}", + todo.len()); for (element, &costs) in self.costs.iter().enumerate() { let Some(min_to_build) = self.until_buildable(costs, resources, machines) else { break }; @@ -161,7 +157,7 @@ impl BluePrint { let mut new_machines = machines; new_machines[element] += 1; - let new_missed = ideal_from_now - ideal(time_after as u32); + let new_missed = ideal_from_now - ideal(u32::from(time_after)); todo.push(State { missed: new_missed, got, @@ -249,7 +245,7 @@ pub fn part1(input: &[u8]) -> Result { Ok(blueprints .into_iter() - .map(|bp| bp.max_geodes(24) as u32 * bp.id) + .map(|bp| u32::from(bp.max_geodes(24)) * bp.id) .sum::() .to_string()) } @@ -260,7 +256,7 @@ pub fn part2(input: &[u8]) -> Result { let result: u32 = blueprints .iter() .take(3) - .map(|bp| bp.max_geodes(32) as u32) + .map(|bp| u32::from(bp.max_geodes(32))) .product(); Ok(result.to_string()) diff --git a/2022/src/day20.rs b/2022/src/day20.rs index 96ada7c..91ca443 100644 --- a/2022/src/day20.rs +++ b/2022/src/day20.rs @@ -106,7 +106,7 @@ fn shuffle(encrypted: &[i64], times: usize) -> Result { } pub fn part2(input: &[u8]) -> Result { - const ENCRYPTION_KEY: i64 = 811589153; + const ENCRYPTION_KEY: i64 = 811_589_153; let mut encrypted = parse_input(input, parse_encrypted)?; diff --git a/2022/src/day22.rs b/2022/src/day22.rs index 9be8d06..98519ac 100644 --- a/2022/src/day22.rs +++ b/2022/src/day22.rs @@ -81,13 +81,12 @@ pub fn part1(input: &[u8]) -> Result { Step::Forward(amount) => match dir { Direction::Up => { for _ in 0..amount { - if y == 0 || map[y - 1].get(x).map(|&b| b == b' ').unwrap_or(true) { + if y == 0 || map[y - 1].get(x).map_or(true, |&b| b == b' ') { let new_y = map .iter() .rposition(|line| { line.get(x) - .map(|&b| b == b'.' || b == b'#') - .unwrap_or(false) + .map_or(false, |&b| b == b'.' || b == b'#') }) .unwrap(); if map[new_y][x] == b'#' { @@ -105,14 +104,13 @@ pub fn part1(input: &[u8]) -> Result { Direction::Down => { for _ in 0..amount { if y + 1 >= map.len() - || map[y + 1].get(x).map(|&b| b == b' ').unwrap_or(true) + || map[y + 1].get(x).map_or(true, |&b| b == b' ') { let new_y = map .iter() .position(|line| { line.get(x) - .map(|&b| b == b'.' || b == b'#') - .unwrap_or(false) + .map_or(false, |&b| b == b'.' || b == b'#') }) .unwrap();