day 2
parent
38123f3a00
commit
c4266bb16c
@ -1,3 +1,4 @@
|
||||
target/
|
||||
input
|
||||
testinput
|
||||
main
|
||||
|
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,75 @@
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
enum Outcome {
|
||||
Won = 6,
|
||||
Drew = 3,
|
||||
Lost = 0
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let lines = io::stdin().lock().lines().map(|l| l.unwrap());
|
||||
let mut p1_sum = 0;
|
||||
let mut p2_sum = 0;
|
||||
|
||||
for line in lines {
|
||||
let (opponent, player) = line.split_once(' ').unwrap();
|
||||
p1_sum += play_score(opponent, player);
|
||||
p2_sum += result_score(opponent, player);
|
||||
}
|
||||
println!("P1: {}, P2: {}", p1_sum, p2_sum);
|
||||
}
|
||||
|
||||
fn player_win_score(player: &str) -> usize {
|
||||
match player {
|
||||
"X" => 1,
|
||||
"Y" => 2,
|
||||
"Z" => 3,
|
||||
_ => panic!("at the disco")
|
||||
}
|
||||
}
|
||||
|
||||
fn play_score(opponent: &str, player: &str) -> usize {
|
||||
let outcome = match (player, opponent) {
|
||||
("X", "C") | ("Y", "A") | ("Z", "B") => Outcome::Won,
|
||||
("X", "A") | ("Y", "B") | ("Z", "C") => Outcome::Drew,
|
||||
("X", "B") | ("Y", "C") | ("Z", "A") => Outcome::Lost,
|
||||
_ => panic!("on the streets of london")
|
||||
};
|
||||
player_win_score(player) + outcome as usize
|
||||
}
|
||||
|
||||
fn player_hand_for_outcome<'a>(outcome: &Outcome, opponent: &str) -> &'a str {
|
||||
match outcome {
|
||||
Outcome::Won => match opponent {
|
||||
"A" => "Y",
|
||||
"B" => "Z",
|
||||
"C" => "X",
|
||||
_ => panic!("on the streets of birmingham")
|
||||
}
|
||||
Outcome::Drew => match opponent {
|
||||
"A" => "X",
|
||||
"B" => "Y",
|
||||
"C" => "Z",
|
||||
_ => panic!("on the streets of carlisle")
|
||||
},
|
||||
Outcome::Lost => match opponent {
|
||||
"A" => "Z",
|
||||
"B" => "X",
|
||||
"C" => "Y",
|
||||
_ => panic!("dublin, dundee, humberside")
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn result_score(opponent: &str, result: &str) -> usize {
|
||||
let outcome = match result {
|
||||
"X" => Outcome::Lost,
|
||||
"Y" => Outcome::Drew,
|
||||
"Z" => Outcome::Won,
|
||||
_ => panic!("the leeds side streets that you slip down")
|
||||
};
|
||||
|
||||
let player = player_hand_for_outcome(&outcome, opponent);
|
||||
|
||||
player_win_score(player) + outcome as usize
|
||||
}
|
Loading…
Reference in New Issue