Compare commits
2 Commits
fc49a2add1
...
0d7097dbf8
Author | SHA1 | Date | |
---|---|---|---|
|
0d7097dbf8 | ||
|
c90d69fdb9 |
@ -55,10 +55,38 @@ impl Into<i32> for ResultPlay {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn winning(play: Play) -> char {
|
||||
match play {
|
||||
Play::Rock => Play::Paper.into(),
|
||||
Play::Paper => Play::Scissors.into(),
|
||||
Play::Scissors => Play::Rock.into(),
|
||||
pub fn round_score(play: char, counter_play: char) -> i32 {
|
||||
let coup = Play::from(play); // translate to the play
|
||||
let counter = Play::from(counter_play); // translate to the play
|
||||
if coup == counter {
|
||||
(ResultPlay::Draw as i32) + (counter_play as i32)
|
||||
} else {
|
||||
use Play::*;
|
||||
use ResultPlay::*;
|
||||
match (&coup, &counter) {
|
||||
(Rock, Paper) => (Win as i32) + (counter as i32),
|
||||
(Rock, Scissors) => (Loss as i32) + (counter as i32),
|
||||
(Paper, Rock) => (Loss as i32) + (counter as i32),
|
||||
(Paper, Scissors) => (Win as i32) + (counter as i32),
|
||||
(Scissors, Rock) => (Win as i32) + (counter as i32),
|
||||
(Scissors, Paper) => (Loss as i32) + (counter as i32),
|
||||
_ => panic!("Case not covered, impossible to reach."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn solve_part1(file_path: &str) -> i32 {
|
||||
let contents =
|
||||
std::fs::read_to_string(file_path).expect("Please provide a text file as an argument.");
|
||||
let lines: Vec<&str> = contents.split('\n').collect();
|
||||
lines
|
||||
.iter()
|
||||
.map(|line| {
|
||||
let first: char = line.chars().nth(0).unwrap_or('R');
|
||||
let last: char = line.chars().nth(2).unwrap_or('R');
|
||||
round_score(first, last)
|
||||
})
|
||||
.collect::<Vec<i32>>()
|
||||
.iter()
|
||||
.sum()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user