1
0

Compare commits

...

3 Commits

Author SHA1 Message Date
Florent DENEF
07b0bf7558 Using the solver for the part 1 form the second problem 2022-12-02 16:43:29 +01:00
Florent DENEF
5aa28c2b07 The thing is working (forced to use a variable for the conversion with
into()).
2022-12-02 16:43:02 +01:00
Florent DENEF
a490b2a72f Better panic message. 2022-12-02 16:42:46 +01:00
2 changed files with 35 additions and 10 deletions

View File

@ -1,10 +1,10 @@
use advent_of_code::prob1; use advent_of_code::{prob1, prob2};
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
let file_path = args let file_path = args
.get(1) .get(1)
.expect("Please provide an argument on command-line."); .expect("Please provide an argument on command-line.");
let max = prob1::solve_part2(file_path); let max = prob2::solve_part1(file_path);
println!("{max}"); println!("{max}");
} }

View File

@ -40,7 +40,10 @@ impl From<char> for Play {
'X' => Play::Rock, 'X' => Play::Rock,
'Y' => Play::Paper, 'Y' => Play::Paper,
'Z' => Play::Scissors, 'Z' => Play::Scissors,
_ => panic!("Only A, B, C, X, Y or Z letters are available."), other => panic!(
"Only A, B, C, X, Y or Z letters are available. Had {}",
other
),
} }
} }
} }
@ -59,17 +62,39 @@ pub fn round_score(play: char, counter_play: char) -> i32 {
let coup = Play::from(play); // translate to the play let coup = Play::from(play); // translate to the play
let counter = Play::from(counter_play); // translate to the play let counter = Play::from(counter_play); // translate to the play
if coup == counter { if coup == counter {
(ResultPlay::Draw as i32) + (counter_play as i32) let draw: i32 = ResultPlay::Draw.into();
let result: i32 = counter.into();
draw + result
} else { } else {
use Play::*; use Play::*;
use ResultPlay::*; use ResultPlay::*;
let loss: i32 = Loss.into();
let win: i32 = Win.into();
match (&coup, &counter) { match (&coup, &counter) {
(Rock, Paper) => (Win as i32) + (counter as i32), (Rock, Paper) => {
(Rock, Scissors) => (Loss as i32) + (counter as i32), let result: i32 = counter.into();
(Paper, Rock) => (Loss as i32) + (counter as i32), win + result
(Paper, Scissors) => (Win as i32) + (counter as i32), }
(Scissors, Rock) => (Win as i32) + (counter as i32), (Rock, Scissors) => {
(Scissors, Paper) => (Loss as i32) + (counter as i32), let result: i32 = counter.into();
loss + result
}
(Paper, Rock) => {
let result: i32 = counter.into();
loss + result
}
(Paper, Scissors) => {
let result: i32 = counter.into();
win + result
}
(Scissors, Rock) => {
let result: i32 = counter.into();
win + result
}
(Scissors, Paper) => {
let result: i32 = counter.into();
loss + result
}
_ => panic!("Case not covered, impossible to reach."), _ => panic!("Case not covered, impossible to reach."),
} }
} }