refactor(clippy): fix all clippy warnings

This commit is contained in:
Kristofers Solo 2024-03-17 21:33:51 +02:00
parent ae4cd562f1
commit cb7c2c3e79
10 changed files with 2000166 additions and 18 deletions

View File

@ -11,3 +11,11 @@ anyhow = "1"
[dev-dependencies]
[package.metadata.clippy]
warn = [
"clippy::pedantic",
"clippy::nursery",
"clippy::unwrap_used",
"clippy::expect_used",
]

131
data/GenTest.py Normal file
View File

@ -0,0 +1,131 @@
# Typehints within class definition can use that class
from __future__ import annotations
from enum import Enum
import random
# File
OUT_FILE = "customs.in"
# Tokens - P or N -> Do not touch
class Tokens(Enum):
NONE = "idk, some random none value"
P_TYPE = "P"
N_TYPE = "N"
def __init__(self, token: str):
self.token = token
@staticmethod
def pick_one() -> Tokens:
if bool(random.randint(0, 1)):
return Tokens.P_TYPE
else:
return Tokens.N_TYPE
pass
# Ranges -> Do not touch
class Ranges(Enum):
MUITNIEKU_COUNT = (1, 99)
MUITNIEKU_W_TIME = (1, 100000)
OUTLIERS = (0, -1) # Second val should always be modified
IN_PEOPLE_COUNT = (0, 4000000)
def __init__(self, min_v: int, max_v: int):
self.min_v = min_v
self.max_v = max_v
def get_random(self) -> int:
assert self.min_v == -1 or self.max_v == -1
return random.randint(self.min_v, self.max_v)
# value of -1 translates to ingore this limit / use the default one
def get_random_wighted(self, min_v: int, max_v: int) -> int:
if self.min_v == -1 or self.max_v == -1:
return self._pick_random_overide(min_v, max_v)
assert (self.min_v <= min_v <= self.max_v) or min_v == -1
assert (self.min_v <= max_v <= self.max_v) or max_v == -1
if min_v == -1 and max_v == -1:
return self.get_random()
if min_v == -1:
return random.randint(self.min_v, max_v)
if max_v == -1:
return random.randint(min_v, self.max_v)
return random.randint(min_v, max_v)
def _pick_random_overide(self, min_v: int, max_v: int) -> int:
if self.min_v == -1 and self.max_v == -1:
return random.randint(min_v, max_v)
if self.min_v == -1:
assert min_v <= self.max_v
return random.randint(min_v, self.max_v)
if self.max_v == -1:
assert self.min_v <= max_v
return random.randint(self.min_v, max_v)
pass
def main():
ofile = open(OUT_FILE, "wt")
# Firts Line
P_COUNT = Ranges.MUITNIEKU_COUNT.get_random_wighted(50, 60)
N_COUNT = Ranges.MUITNIEKU_COUNT.get_random_wighted(50, 60)
P_DEF_PROC_TIME = Ranges.MUITNIEKU_W_TIME.get_random_wighted(180, 200)
N_DEF_PROC_TIME = Ranges.MUITNIEKU_W_TIME.get_random_wighted(180, 200)
ofile.write(f"{P_COUNT} {N_COUNT} {P_DEF_PROC_TIME} {N_DEF_PROC_TIME}\n")
# Muitnieku Procesing Times
# Do not make the work times too small or your queues will have no work!
MAX_OUTL = P_COUNT + N_COUNT
OUTLIER_COUNT = Ranges.OUTLIERS.get_random_wighted(-1, int(MAX_OUTL / 3))
OUTLIER_COUNT = 4
for i in range(OUTLIER_COUNT):
token = Tokens.pick_one()
w_time = Ranges.MUITNIEKU_W_TIME.get_random_wighted(180, 230)
if token == Tokens.P_TYPE:
muitn = random.randint(1, P_COUNT)
elif token == Tokens.N_TYPE:
muitn = random.randint(1, N_COUNT)
else:
assert False
# My implimentantion can handdle dublicte enteries here, so i dont check for it.
ofile.write(f"T {token.token} {muitn} {w_time} \n")
# And now the ppl
# PPL_COUNT = Ranges.IN_PEOPLE_COUNT.get_random_wighted(2000000, 2000000)
PPL_COUNT = 2_000_000
_MAX_ARIVAL_TIME = 4_000_000
INIT_ARIVAL_TIME = random.randint(100, 200)
cur_time = INIT_ARIVAL_TIME
real_ppl_count = -1
for i in range(PPL_COUNT):
token = Tokens.pick_one()
ofile.write(f"{token.token} {cur_time}\n")
# Random delta time
cur_time += random.randint(1, 2)
if cur_time >= _MAX_ARIVAL_TIME:
real_ppl_count = i
break
pass
# EOF
ofile.write("X")
# Stats
print(f"=== Stats ===")
print(f"Pc: {P_COUNT} Pt: {P_DEF_PROC_TIME}")
print(f"Nc: {N_COUNT} Nc: {N_DEF_PROC_TIME}")
print(f"Outlier count: {OUTLIER_COUNT}")
print(f"Base arival time: {INIT_ARIVAL_TIME:,}")
print(f"Final arival time: {cur_time:,}")
print(f"PPL Count Target: {PPL_COUNT:,}")
print(f"PPL Count Real: {real_ppl_count:,}")
ofile.close()
if __name__ == "__main__":
main()

2000006
data/input/customs.i8 Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,12 +2,18 @@ use std::str::FromStr;
use crate::{error::InputError, utils::Time};
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CitizenshipType {
Citizen,
NonCitizen,
}
impl PartialOrd for CitizenshipType {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CitizenshipType {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self, other) {

View File

@ -1,6 +1,6 @@
use crate::{citizenship::CitizenshipType, officer::Officer};
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Customs {
pub citizens: Vec<Officer>,
pub noncitizens: Vec<Officer>,

View File

@ -4,12 +4,11 @@ use anyhow::Result;
use customs_rs::process::process;
fn main() -> Result<()> {
let filename = "customs.i4";
let filename = "customs.i8";
println!("\nFilename: {}\n", &filename);
let mut file = File::open(format!("data/input/{}", &filename))?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let result = process(&contents);
print!("{result}");
process(&contents);
Ok(())
}

View File

@ -35,7 +35,7 @@ impl Officer {
impl PartialOrd for Officer {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.id.partial_cmp(&other.id)
Some(self.id.cmp(&other.id))
}
}
@ -52,7 +52,7 @@ impl FromStr for Officer {
match (iter.next(), iter.next(), iter.next(), iter.next()) {
(Some("T"), Some(citizenship_type), Some(customs_num), Some(time)) => {
let citizenship = CitizenshipType::from_str(&citizenship_type)?;
let citizenship = CitizenshipType::from_str(citizenship_type)?;
let time = time
.parse::<Time>()
.map_err(|_| InputError::InvalidFormat)?;

View File

@ -35,10 +35,8 @@ impl From<Data> for Output {
for citizen in &value.citizens {
let time = match citizen.type_ {
CitizenshipType::Citizen => process_citizenship_entry(&citizen, &mut citizens),
CitizenshipType::NonCitizen => {
process_citizenship_entry(&citizen, &mut noncitizens)
}
CitizenshipType::Citizen => process_citizenship_entry(citizen, &mut citizens),
CitizenshipType::NonCitizen => process_citizenship_entry(citizen, &mut noncitizens),
};
outputs.push(time);
}
@ -57,9 +55,9 @@ impl Display for Output {
}
}
impl Into<String> for Output {
fn into(self) -> String {
self.to_string()
impl From<Output> for String {
fn from(value: Output) -> Self {
value.to_string()
}
}
@ -71,7 +69,7 @@ fn calculate_departure_time(officer: &mut Officer, arrival_time: Time) -> Time {
}
}
fn process_citizenship_entry(citizen: &Citizenship, customs: &mut Vec<Officer>) -> OutputTime {
fn process_citizenship_entry(citizen: &Citizenship, customs: &mut [Officer]) -> OutputTime {
let arrival_time = citizen.arrival_time;
let officer = customs

View File

@ -31,7 +31,7 @@ impl OutputTime {
impl PartialOrd for OutputTime {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.departure_time.partial_cmp(&other.departure_time)
Some(self.departure_time.cmp(&other.departure_time))
}
}

View File

@ -34,8 +34,8 @@ pub fn parse_input(input: &str) -> Result<Output, ParseError> {
.noncitizens
.reserve_exact(first.noncitizen.count as usize);
while let Some(line) = lines.next() {
if line.contains("X") {
for line in lines {
if line.contains('X') {
break;
};
if let Ok(officer) = Officer::from_str(line) {