feat: day2
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
f05a2a1a4d
commit
8427087485
@ -9,3 +9,7 @@ day-1:
|
|||||||
script:
|
script:
|
||||||
- cd day1; cargo run --release ./input
|
- cd day1; cargo run --release ./input
|
||||||
|
|
||||||
|
day-2:
|
||||||
|
stage: build
|
||||||
|
script:
|
||||||
|
- cd day2; cargo run --release ./input
|
||||||
|
8
day2/Cargo.toml
Normal file
8
day2/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "day2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
2500
day2/input
Normal file
2500
day2/input
Normal file
File diff suppressed because it is too large
Load Diff
76
day2/src/main.rs
Normal file
76
day2/src/main.rs
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
use std::{fs, env, char};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum Moves {
|
||||||
|
Rock = 1,
|
||||||
|
Paper = 2,
|
||||||
|
Scissors = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Moves {
|
||||||
|
fn get_move_from_letter(c: char) -> Moves {
|
||||||
|
match c {
|
||||||
|
'A' | 'X' => Self::Rock,
|
||||||
|
'B' | 'Y' => Self::Paper,
|
||||||
|
'C' | 'Z' => Self::Scissors,
|
||||||
|
_ => panic!("Move '{}' is not implemented!", c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_move_from_line(s: &str) -> (Moves, Moves) {
|
||||||
|
let c = s.chars().collect::<Vec<char>>();
|
||||||
|
return (Self::get_move_from_letter(c[0]), Self::get_move_from_letter(c[2]));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_winning_move(&self) -> Moves {
|
||||||
|
match self {
|
||||||
|
Self::Rock => Self::Scissors,
|
||||||
|
Self::Paper => Self::Rock,
|
||||||
|
Self::Scissors => Self::Paper
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_losing_move(&self) -> Moves {
|
||||||
|
match self {
|
||||||
|
Self::Scissors => Self::Rock,
|
||||||
|
Self::Rock => Self::Paper,
|
||||||
|
Self::Paper => Self::Scissors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_input(path: &str) -> String {
|
||||||
|
return fs::read_to_string(path).expect("Cannot read file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(s: &str) -> Vec<(Moves, Moves)> {
|
||||||
|
return s.lines().into_iter().fold(vec![], |mut acc, x| {
|
||||||
|
acc.push(Moves::get_move_from_line(x));
|
||||||
|
return acc;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_score_for_round(round: &(Moves, Moves)) -> i32 {
|
||||||
|
return round.1 as i32 +
|
||||||
|
if round.0 == round.1 { 3 }
|
||||||
|
else if round.1.get_winning_move() == round.0 { 6 }
|
||||||
|
else { 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_expected_score_for_round(round: &(Moves, Moves)) -> i32 {
|
||||||
|
return get_score_for_round(&(round.0,
|
||||||
|
if round.1 == Moves::Paper { round.0 }
|
||||||
|
else if round.1 == Moves::Rock { round.0.get_winning_move() }
|
||||||
|
else { round.0.get_losing_move() }
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
for arg in args.iter().skip(1) {
|
||||||
|
println!("[{}]", &arg);
|
||||||
|
let vec_in = parse_input(&read_input(&arg));
|
||||||
|
println!("\t[Part 1] => Answer is '{}'.", vec_in.iter().fold(0, |acc, x| acc + get_score_for_round(&x)));
|
||||||
|
println!("\t[Part 2] => Answer is '{}'.", vec_in.iter().fold(0, |acc, x| acc + get_expected_score_for_round(&x)));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user