Created
December 2, 2022 14:59
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs; | |
use std::collections::HashMap; | |
struct RPS { | |
name: String, | |
points: u32, | |
outcomes: HashMap<String, String>, | |
reverse_outcomes: HashMap<String, String> | |
} | |
fn init_rps(val: &String) -> RPS { | |
match val.as_str() { | |
"X" | "A" | "rock" => { | |
RPS { | |
name: "rock".to_string(), | |
points: 1, | |
outcomes: HashMap::from([ | |
("rock".to_string(), "D".to_string()), | |
("paper".to_string(), "L".to_string()), | |
("scissors".to_string(), "W".to_string()) | |
] | |
), | |
reverse_outcomes: HashMap::from([ | |
("Y".to_string(), "rock".to_string()), | |
("Z".to_string(), "paper".to_string()), | |
("X".to_string(), "scissors".to_string()) | |
] | |
) | |
} | |
}, | |
"Y" | "B" | "paper" => { | |
RPS { | |
name: "paper".to_string(), | |
points: 2, | |
outcomes: HashMap::from([ | |
("rock".to_string(), "W".to_string()), | |
("paper".to_string(), "D".to_string()), | |
("scissors".to_string(), "L".to_string()) | |
] | |
), | |
reverse_outcomes: HashMap::from([ | |
("X".to_string(), "rock".to_string()), | |
("Y".to_string(), "paper".to_string()), | |
("Z".to_string(), "scissors".to_string()) | |
] | |
) | |
} | |
}, | |
"Z" | "C" | "scissors" => { | |
RPS { | |
name: "scissors".to_string(), | |
points: 3, | |
outcomes: HashMap::from([ | |
("rock".to_string(), "L".to_string()), | |
("paper".to_string(), "W".to_string()), | |
("scissors".to_string(), "D".to_string()) | |
] | |
), | |
reverse_outcomes: HashMap::from([ | |
("Z".to_string(), "rock".to_string()), | |
("X".to_string(), "paper".to_string()), | |
("Y".to_string(), "scissors".to_string()) | |
] | |
) | |
} | |
}, | |
&_ => { | |
RPS { | |
name: "none".to_string(), | |
points: 0, | |
outcomes: HashMap::from([("A".to_string(), "L".to_string())]), | |
reverse_outcomes: HashMap::from([("A".to_string(), "L".to_string())]) | |
} | |
} | |
} | |
} | |
fn main() { | |
let strat = fs::read_to_string("input.txt").unwrap(); | |
let mut total = 0; | |
let mut total_strat = 0; | |
let round_values = HashMap::from([("W".to_string(), 6), ("D".to_string(), 3), ("L".to_string(), 0)]); | |
let new_strat = HashMap::from([("X".to_string(), "L".to_string()), ("Y".to_string(), "D".to_string()), ("Z".to_string(), "W".to_string())]); | |
for line in strat.lines() { | |
let round_input: Vec<&str> = line.split(" ").collect(); | |
let opponent_input = round_input[0].to_string(); | |
let my_strat = round_input[1].to_string(); | |
let opponent_rps = init_rps(&opponent_input); | |
let my_rps = init_rps(&my_strat); | |
total += my_rps.points + round_values[&my_rps.outcomes[&opponent_rps.name]]; | |
total_strat += round_values[&new_strat[&my_strat]] + init_rps(&opponent_rps.reverse_outcomes[&my_strat]).points; | |
} | |
println!("{}", total); | |
println!("{}", total_strat); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment