Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippy with pedantic option #7

Merged
merged 2 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions 2015/day7/day7.rs
Expand Up @@ -42,9 +42,7 @@ fn run(
reg: &str,
level: u32,
) -> u16 {
if level > 70 {
panic!("too deep");
}
assert!(!(level > 70), "too deep");

if cache.contains_key(reg) {
return cache[reg];
Expand Down
6 changes: 3 additions & 3 deletions 2021/day1/day1.rs
Expand Up @@ -6,7 +6,7 @@ use std::io::{self, BufRead};
use std::path::Path;

fn main() {
let mut prev_num = 999999999u32;
let mut prev_num = 999_999_999_u32;
let mut result = 0;

let mut data = vec![];
Expand All @@ -26,13 +26,13 @@ fn main() {
prev_num = num;

// pour le step 2
data.push(num)
data.push(num);
}
}
println!("{}", result);

// step 2
prev_num = 999999999u32;
prev_num = 999_999_999_u32;
result = 0;

for i in 0..data.len() - 2 {
Expand Down
4 changes: 2 additions & 2 deletions 2021/day10/day10.rs
Expand Up @@ -13,7 +13,7 @@ fn main() {
let mut part2 = vec![];

for line in data {
let (corrupted, completed) = check(line);
let (corrupted, completed) = check(&line);

part1 += corrupted;
if completed != 0 {
Expand All @@ -28,7 +28,7 @@ fn main() {
println!("{:?}", part2[part2.len() / 2]);
}

fn check(line: String) -> (u64, u64) {
fn check(line: &str) -> (u64, u64) {
let mut stack = vec![];

for c in line.chars() {
Expand Down
33 changes: 16 additions & 17 deletions 2021/day11/day11.rs
Expand Up @@ -21,18 +21,18 @@ fn main() {

// read the grid
let n = data.len();
let mut grid = vec![vec![0i8; n]; n];
let mut grid = vec![vec![0u8; n]; n];
for (y, line) in data.iter().enumerate() {
for (x, c) in line.chars().enumerate() {
grid[y][x] = c.to_digit(10).unwrap() as i8;
grid[y][x] = c.to_string().parse().unwrap();
}
}

let mut flashes = 0;

for turn in 1..1000 {
// First, the energy level of each octopus increases by 1.
for line in grid.iter_mut() {
for line in &mut grid {
for val in line {
*val += 1;
}
Expand All @@ -55,25 +55,24 @@ fn main() {
if dy == 0 && dx == 0 {
continue;
}
let nx = x as isize + dx;
let ny = y as isize + dy;
if 0 <= nx && nx < n as isize && 0 <= ny && ny < n as isize {
let v = grid[ny as usize][nx as usize];
let nx = isize::try_from(x).unwrap() + dx;
let ny = isize::try_from(y).unwrap() + dy;
if 0 <= nx
&& nx < n.try_into().unwrap()
&& 0 <= ny
&& ny < n.try_into().unwrap()
{
let nx = usize::try_from(nx).unwrap();
let ny = usize::try_from(ny).unwrap();
let v = grid[ny][nx];
if v != 0 && v <= 9 {
grid[ny as usize][nx as usize] = v + 1;
grid[ny][nx] = v + 1;
}
}
}
}
}

// for y in 0..n {
// for x in 0..n {
// print!("{}",grid[y][x]);
// }
// println!();
// }

if all_flashing(&grid) {
println!("part2: {}", turn);
break;
Expand All @@ -85,7 +84,7 @@ fn main() {
}
}

fn all_flashing(grid: &[Vec<i8>]) -> bool {
fn all_flashing(grid: &[Vec<u8>]) -> bool {
for line in grid {
for val in line {
if *val != 0 {
Expand All @@ -96,7 +95,7 @@ fn all_flashing(grid: &[Vec<i8>]) -> bool {
true
}

fn find_flash(grid: &[Vec<i8>]) -> (usize, usize) {
fn find_flash(grid: &[Vec<u8>]) -> (usize, usize) {
for (y, line) in grid.iter().enumerate() {
for (x, val) in line.iter().enumerate() {
if *val == 10 {
Expand Down
4 changes: 2 additions & 2 deletions 2021/day13/day13.rs
Expand Up @@ -66,10 +66,10 @@ fn main() {

if !part1 {
part1 = true;
let mut sum = 0;
let mut sum = 0i32;
for row in &grid {
for cell in row {
sum += *cell as u32;
sum += i32::from(*cell);
}
}
println!("part1: {}", sum);
Expand Down
5 changes: 3 additions & 2 deletions 2021/day15/day15.rs
Expand Up @@ -27,7 +27,7 @@ fn main() {
let mut grid = vec![vec![0i32; n]; n];
for (y, line) in data.iter().enumerate() {
for (x, c) in line.chars().enumerate() {
grid[y][x] = c.to_digit(10).unwrap() as i32;
grid[y][x] = c.to_string().parse().unwrap();
}
}

Expand All @@ -43,7 +43,8 @@ fn main() {

for yy in 0..5 {
for xx in 0..5 {
grid5[y + n * yy][x + n * xx] = (v - 1 + (xx + yy) as i32) % 9 + 1;
grid5[y + n * yy][x + n * xx] =
(v - 1 + i32::try_from(xx + yy).unwrap()) % 9 + 1;
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions 2021/day17/day17.rs
Expand Up @@ -8,29 +8,29 @@ use text_io::scan;

/// main function
fn main() {
let target_x1: i32;
let target_x2: i32;
let target_y1: i32;
let target_y2: i32;
let abs_min: i32;
let abs_max: i32;
let ord_min: i32;
let ord_max: i32;

let args: Vec<String> = env::args().collect();
println!("args: {:?}", args);
match args.len() {
5 => {
target_x1 = args[1].parse::<i32>().unwrap();
target_x2 = args[2].parse::<i32>().unwrap();
target_y1 = args[3].parse::<i32>().unwrap();
target_y2 = args[4].parse::<i32>().unwrap();
abs_min = args[1].parse::<i32>().unwrap();
abs_max = args[2].parse::<i32>().unwrap();
ord_min = args[3].parse::<i32>().unwrap();
ord_max = args[4].parse::<i32>().unwrap();
}
2 => {
let data = fs::read_to_string(args[1].to_string()).unwrap();
scan!(data.bytes() => "target area: x={}..{}, y={}..{}",
target_x1, target_x2, target_y1, target_y2);
abs_min, abs_max, ord_min, ord_max);
}
1 => {
let data = fs::read_to_string("input.txt").unwrap();
scan!(data.bytes() => "target area: x={}..{}, y={}..{}",
target_x1, target_x2, target_y1, target_y2);
abs_min, abs_max, ord_min, ord_max);
}
_ => {
panic!("Invalid number of arguments");
Expand All @@ -39,7 +39,7 @@ fn main() {

println!(
"target area: x={}..{}, y={}..{}",
target_x1, target_x2, target_y1, target_y2
abs_min, abs_max, ord_min, ord_max
);

let mut part1 = 0;
Expand Down Expand Up @@ -73,7 +73,7 @@ fn main() {

vy -= 1; // the probe's y velocity decreases by 1.

if target_x1 <= x && x <= target_x2 && target_y1 <= y && y <= target_y2 {
if abs_min <= x && x <= abs_max && ord_min <= y && y <= ord_max {
hit = true;
}
}
Expand Down
22 changes: 10 additions & 12 deletions 2021/day19/day19.rs
Expand Up @@ -23,7 +23,7 @@ struct Point {
z: i32,
}

/// implement fmt::Debug for Point
/// implement `fmt::Debug` for Point
impl fmt::Debug for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("")
Expand All @@ -44,11 +44,11 @@ fn main() {
let scanners = load_scanners(data);

let now = Instant::now();
solve(scanners);
println!("elapsed: {} s", (now.elapsed().as_millis() as f64) / 1000.);
solve(&scanners);
println!("elapsed: {} s", now.elapsed().as_secs_f64());
}

fn solve(scanners: Vec<Vec<Point>>) {
fn solve(scanners: &[Vec<Point>]) {
// the list of different beacons
let mut beacons: HashSet<Point> = HashSet::new();
for p in &scanners[0] {
Expand All @@ -67,8 +67,8 @@ fn solve(scanners: Vec<Vec<Point>>) {
}

// compute all rotated coordinates of beacons
let mut scanners_rotated: Vec<Vec<Vec<Point>>> = Vec::new();
for scanner in &scanners {
let mut scanner_rotated_list: Vec<Vec<Vec<Point>>> = Vec::new();
for scanner in scanners {
let mut scanner_rotated: Vec<Vec<Point>> = Vec::new();

for rotation in 0..24 {
Expand All @@ -78,7 +78,7 @@ fn solve(scanners: Vec<Vec<Point>>) {
.collect();
scanner_rotated.push(r);
}
scanners_rotated.push(scanner_rotated);
scanner_rotated_list.push(scanner_rotated);
}

while !pending.is_empty() {
Expand All @@ -96,7 +96,7 @@ fn solve(scanners: Vec<Vec<Point>>) {
}

for rotation in 0..24 {
let b_scan = scanners_rotated[*scanner_id][rotation].clone();
let b_scan = scanner_rotated_list[*scanner_id][rotation].clone();

let mut match_points: HashMap<Point, i32> = HashMap::new();

Expand Down Expand Up @@ -143,9 +143,7 @@ fn solve(scanners: Vec<Vec<Point>>) {
}
}

if found == usize::MAX {
panic!("no beacon found");
}
assert!(!(found == usize::MAX), "no beacon found");

pending.remove(&found);
}
Expand Down Expand Up @@ -232,5 +230,5 @@ fn rotate(point: &Point, rotation: usize) -> Point {
/// load data from file
fn load_data(path: std::path::PathBuf) -> Vec<String> {
let data = fs::read_to_string(path).unwrap();
data.lines().map(|x| x.to_string()).collect()
data.lines().map(std::string::ToString::to_string).collect()
}
30 changes: 15 additions & 15 deletions 2021/day2/day2.rs
Expand Up @@ -11,51 +11,51 @@ fn main() {
}

fn part1() {
let mut hpos = 0;
let mut vpos = 0;
let mut pos_h = 0;
let mut pos_v = 0;

if let Ok(lines) = read_lines("input.txt") {
for line in lines.flatten() {
if let Some((direction, _step)) = line.rsplit_once(' ') {
let step = _step.parse::<i32>().unwrap();
if let Some((direction, step_str)) = line.rsplit_once(' ') {
let step = step_str.parse::<i32>().unwrap();

if direction == "forward" {
hpos += step;
pos_h += step;
} else if direction == "down" {
vpos += step;
pos_v += step;
} else if direction == "up" {
vpos -= step;
pos_v -= step;
}
}
}
}

println!("{}", hpos * vpos);
println!("{}", pos_h * pos_v);
}

fn part2() {
let mut hpos = 0;
let mut vpos = 0;
let mut pos_h = 0;
let mut pos_v = 0;
let mut aim = 0;

if let Ok(lines) = read_lines("input.txt") {
for line in lines.flatten() {
if let Some((direction, _step)) = line.rsplit_once(' ') {
let step = _step.parse::<i32>().unwrap();
if let Some((direction, step_str)) = line.rsplit_once(' ') {
let step = step_str.parse::<i32>().unwrap();

if direction == "down" {
aim += step;
} else if direction == "up" {
aim -= step;
} else if direction == "forward" {
hpos += step;
vpos += aim * step;
pos_h += step;
pos_v += aim * step;
}
}
}
}

println!("{}", hpos * vpos);
println!("{}", pos_h * pos_v);
}

// The output is wrapped in a Result to allow matching on errors
Expand Down