Run cargo fmt

This commit is contained in:
Chandler Swift 2025-02-08 22:22:58 -06:00
parent f0ba450ecf
commit 5f6ab7876b
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F

View file

@ -1,10 +1,10 @@
#![feature(test)]
extern crate test;
use std::str::FromStr;
use std::{env, fmt};
use std::fmt::Display;
use std::process::ExitCode;
use std::str::FromStr;
use std::{env, fmt};
use rand::seq::IteratorRandom;
@ -38,14 +38,26 @@ impl FromStr for Grid {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Grid(s.split('\n').map(|s|s.chars().collect()).collect()))
Ok(Grid(s.split('\n').map(|s| s.chars().collect()).collect()))
}
}
fn generate_grid() -> Grid {
Grid((0..HEIGHT).map(
|_| (0..WIDTH).map(|_| SOURCE_CHARS.chars().choose(&mut rand::rng()).unwrap().clone()).collect()
).collect())
Grid(
(0..HEIGHT)
.map(|_| {
(0..WIDTH)
.map(|_| {
SOURCE_CHARS
.chars()
.choose(&mut rand::rng())
.unwrap()
.clone()
})
.collect()
})
.collect(),
)
}
fn count_matches(grid: &Grid) -> usize {
@ -54,9 +66,18 @@ fn count_matches(grid: &Grid) -> usize {
// horizontal
for row in 0..grid.0.len() {
for col in 0..grid.0[0].len() - len {
if STRING.chars().enumerate().all(|(i,c)| grid.0[row][col+i] == c) {
if STRING
.chars()
.enumerate()
.all(|(i, c)| grid.0[row][col + i] == c)
{
count += 1;
} else if STRING.chars().rev().enumerate().all(|(i,c)| grid.0[row][col+i] == c) {
} else if STRING
.chars()
.rev()
.enumerate()
.all(|(i, c)| grid.0[row][col + i] == c)
{
count += 1;
}
}
@ -64,9 +85,18 @@ fn count_matches(grid: &Grid) -> usize {
// vertical
for row in 0..grid.0.len() - len {
for col in 0..grid.0[0].len() {
if STRING.chars().enumerate().all(|(i,c)| grid.0[row+i][col] == c) {
if STRING
.chars()
.enumerate()
.all(|(i, c)| grid.0[row + i][col] == c)
{
count += 1;
} else if STRING.chars().rev().enumerate().all(|(i,c)| grid.0[row+i][col] == c) {
} else if STRING
.chars()
.rev()
.enumerate()
.all(|(i, c)| grid.0[row + i][col] == c)
{
count += 1;
}
}
@ -74,9 +104,18 @@ fn count_matches(grid: &Grid) -> usize {
// y = x diagonal
for row in len..grid.0.len() {
for col in 0..grid.0[0].len() - len {
if STRING.chars().enumerate().all(|(i,c)| grid.0[row-i][col+i] == c) {
if STRING
.chars()
.enumerate()
.all(|(i, c)| grid.0[row - i][col + i] == c)
{
count += 1;
} else if STRING.chars().rev().enumerate().all(|(i,c)| grid.0[row-i][col+i] == c) {
} else if STRING
.chars()
.rev()
.enumerate()
.all(|(i, c)| grid.0[row - i][col + i] == c)
{
count += 1;
}
}
@ -84,9 +123,18 @@ fn count_matches(grid: &Grid) -> usize {
// y = -x diagonal
for row in 0..grid.0.len() - len {
for col in 0..grid.0[0].len() - len {
if STRING.chars().enumerate().all(|(i,c)| grid.0[row+i][col+i] == c) {
if STRING
.chars()
.enumerate()
.all(|(i, c)| grid.0[row + i][col + i] == c)
{
count += 1;
} else if STRING.chars().rev().enumerate().all(|(i,c)| grid.0[row+i][col+i] == c) {
} else if STRING
.chars()
.rev()
.enumerate()
.all(|(i, c)| grid.0[row + i][col + i] == c)
{
count += 1;
}
}
@ -109,7 +157,10 @@ fn stats(iters: usize) {
}
fn show_help() {
eprintln!("{} <n>: TODO calculate cows in puzzle <n> times and show stats", env::args().nth(0).unwrap())
eprintln!(
"{} <n>: TODO calculate cows in puzzle <n> times and show stats",
env::args().nth(0).unwrap()
)
}
fn main() -> ExitCode {
@ -120,7 +171,7 @@ fn main() -> ExitCode {
Ok(iters) => {
stats(iters);
ExitCode::SUCCESS
},
}
Err(_) => {
eprintln!("Unable to parse iterations");
ExitCode::FAILURE
@ -136,7 +187,7 @@ fn main() -> ExitCode {
"--help" | "-h" => {
show_help();
ExitCode::SUCCESS
},
}
_ => {
show_help();
ExitCode::FAILURE
@ -164,7 +215,7 @@ mod tests {
for (s, n) in v {
assert_eq!(count_matches(&s.parse().unwrap()), n)
}
}
}
#[bench]
fn bench_generate_grid(b: &mut Bencher) {