applied clippy

This commit is contained in:
Ruediger Ludwig 2023-01-29 19:12:22 +01:00
parent 33eb92e9d1
commit 4e08117ed0
16 changed files with 205 additions and 504 deletions

View file

@ -15,25 +15,25 @@ impl DayTrait for Day {
fn part1(&self, lines: &[String]) -> anyhow::Result<ResultType> {
let sum = lines
.iter()
.map(|line| RPS::parse_line(&line))
.map(|line| Rps::parse_line(line))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.map(|(first, second)| second.asses_pair(&first))
.sum();
Ok(ResultType::IntResult(sum))
Ok(ResultType::Integer(sum))
}
fn part2(&self, lines: &[String]) -> anyhow::Result<ResultType> {
let sum = lines
.iter()
.map(|line| Strategy::parse_line(&line))
.map(|line| Strategy::parse_line(line))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.map(|(first, second)| second.fullfill(&first).asses_pair(&first))
.sum();
Ok(ResultType::IntResult(sum))
Ok(ResultType::Integer(sum))
}
}
@ -47,29 +47,29 @@ enum RPSError {
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum RPS {
enum Rps {
Rock,
Paper,
Scissors,
}
impl RPS {
impl Rps {
pub fn parse_line(line: &str) -> Result<(Self, Self), RPSError> {
let mut parts = line.split(" ");
let mut parts = line.split(' ');
let (Some(first), Some(second)) = (parts.next(), parts.next()) else {
Err(RPSError::IllegalLine(line.to_owned()))?
};
let first = RPS::try_from(first)?;
let second = RPS::try_from(second)?;
let first = Rps::try_from(first)?;
let second = Rps::try_from(second)?;
Ok((first, second))
}
pub fn value(&self) -> i64 {
match self {
RPS::Rock => 1,
RPS::Paper => 2,
RPS::Scissors => 3,
Rps::Rock => 1,
Rps::Paper => 2,
Rps::Scissors => 3,
}
}
@ -84,13 +84,13 @@ impl RPS {
}
}
impl PartialOrd for RPS {
impl PartialOrd for Rps {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(RPS::Rock, RPS::Paper) | (RPS::Paper, RPS::Scissors) | (RPS::Scissors, RPS::Rock) => {
(Rps::Rock, Rps::Paper) | (Rps::Paper, Rps::Scissors) | (Rps::Scissors, Rps::Rock) => {
Some(Ordering::Less)
}
(RPS::Rock, RPS::Scissors) | (RPS::Paper, RPS::Rock) | (RPS::Scissors, RPS::Paper) => {
(Rps::Rock, Rps::Scissors) | (Rps::Paper, Rps::Rock) | (Rps::Scissors, Rps::Paper) => {
Some(Ordering::Greater)
}
_ => Some(Ordering::Equal),
@ -98,14 +98,14 @@ impl PartialOrd for RPS {
}
}
impl TryFrom<&str> for RPS {
impl TryFrom<&str> for Rps {
type Error = RPSError;
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
match value {
"A" | "X" => Ok(RPS::Rock),
"B" | "Y" => Ok(RPS::Paper),
"C" | "Z" => Ok(RPS::Scissors),
"A" | "X" => Ok(Rps::Rock),
"B" | "Y" => Ok(Rps::Paper),
"C" | "Z" => Ok(Rps::Scissors),
_ => Err(RPSError::ParseError(value.to_owned())),
}
}
@ -118,23 +118,23 @@ enum Strategy {
}
impl Strategy {
pub fn parse_line(line: &str) -> Result<(RPS, Self), RPSError> {
let mut parts = line.split(" ");
pub fn parse_line(line: &str) -> Result<(Rps, Self), RPSError> {
let mut parts = line.split(' ');
let (Some(first), Some(second)) = (parts.next(), parts.next()) else {
Err(RPSError::IllegalLine(line.to_owned()))?
};
let first = RPS::try_from(first)?;
let first = Rps::try_from(first)?;
let second = Strategy::try_from(second)?;
Ok((first, second))
}
pub fn fullfill(&self, other: &RPS) -> RPS {
pub fn fullfill(&self, other: &Rps) -> Rps {
match (other, self) {
(_, Strategy::Draw) => *other,
(RPS::Rock, Strategy::Win) | (RPS::Scissors, Strategy::Loose) => RPS::Paper,
(RPS::Paper, Strategy::Win) | (RPS::Rock, Strategy::Loose) => RPS::Scissors,
(RPS::Scissors, Strategy::Win) | (RPS::Paper, Strategy::Loose) => RPS::Rock,
(Rps::Rock, Strategy::Win) | (Rps::Scissors, Strategy::Loose) => Rps::Paper,
(Rps::Paper, Strategy::Win) | (Rps::Rock, Strategy::Loose) => Rps::Scissors,
(Rps::Scissors, Strategy::Win) | (Rps::Paper, Strategy::Loose) => Rps::Rock,
}
}
}
@ -161,8 +161,8 @@ mod test {
#[test]
fn test_parse() -> Result<()> {
let input = "A Y";
let expected = (RPS::Rock, RPS::Paper);
let result = RPS::parse_line(input)?;
let expected = (Rps::Rock, Rps::Paper);
let result = Rps::parse_line(input)?;
assert_eq!(result, expected);
Ok(())
@ -170,8 +170,8 @@ mod test {
#[test]
fn test_assess() -> Result<()> {
let first = RPS::Scissors;
let second = RPS::Paper;
let first = Rps::Scissors;
let second = Rps::Paper;
let expected = 9;
let result = first.asses_pair(&second);
assert_eq!(result, expected);
@ -183,7 +183,7 @@ mod test {
fn test_part1() -> Result<()> {
let day = Day {};
let lines = read_lines(day.get_day_number(), "example01.txt")?;
let expected = ResultType::IntResult(15);
let expected = ResultType::Integer(15);
let result = day.part1(&lines)?;
assert_eq!(result, expected);
@ -193,7 +193,7 @@ mod test {
#[test]
fn test_parse_strategy() -> Result<()> {
let input = "A Y";
let expected = (RPS::Rock, Strategy::Draw);
let expected = (Rps::Rock, Strategy::Draw);
let result = Strategy::parse_line(input)?;
assert_eq!(result, expected);
@ -202,9 +202,9 @@ mod test {
#[test]
fn test_assess_stragety() -> Result<()> {
let first = RPS::Scissors;
let first = Rps::Scissors;
let second = Strategy::Win;
let expected = RPS::Rock;
let expected = Rps::Rock;
let result = second.fullfill(&first);
assert_eq!(result, expected);
@ -215,7 +215,7 @@ mod test {
fn test_part2() -> Result<()> {
let day = Day {};
let lines = read_lines(day.get_day_number(), "example01.txt")?;
let expected = ResultType::NoResult;
let expected = ResultType::Nothing;
let result = day.part2(&lines)?;
assert_eq!(result, expected);