day 2 solution
parent
9a4ecb0907
commit
cc843cba6b
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,101 @@
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
enum Pieces {
|
||||||
|
Rock,
|
||||||
|
Paper,
|
||||||
|
Scissors,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum EndStates {
|
||||||
|
Lose,
|
||||||
|
Draw,
|
||||||
|
Win,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run() -> Result<()> {
|
||||||
|
let file_contents = fs::read_to_string("day2.txt")?;
|
||||||
|
let lines = file_contents.lines();
|
||||||
|
let mut part_one_scores: Vec<u32> = Vec::with_capacity(1000);
|
||||||
|
let mut part_two_scores: Vec<u32> = Vec::with_capacity(1000);
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
let opponent = match &line[0..1] {
|
||||||
|
"A" => Pieces::Rock,
|
||||||
|
"B" => Pieces::Paper,
|
||||||
|
"C" => Pieces::Scissors,
|
||||||
|
_ => return Err(anyhow!("Invalid piece")),
|
||||||
|
};
|
||||||
|
|
||||||
|
let player_one = match &line[2..3] {
|
||||||
|
"X" => Pieces::Rock,
|
||||||
|
"Y" => Pieces::Paper,
|
||||||
|
"Z" => Pieces::Scissors,
|
||||||
|
_ => return Err(anyhow!("Invalid player piece")),
|
||||||
|
};
|
||||||
|
|
||||||
|
let game_result = match opponent {
|
||||||
|
Pieces::Rock => match player_one {
|
||||||
|
Pieces::Rock => EndStates::Draw,
|
||||||
|
Pieces::Paper => EndStates::Win,
|
||||||
|
Pieces::Scissors => EndStates::Lose,
|
||||||
|
},
|
||||||
|
Pieces::Paper => match player_one {
|
||||||
|
Pieces::Rock => EndStates::Lose,
|
||||||
|
Pieces::Paper => EndStates::Draw,
|
||||||
|
Pieces::Scissors => EndStates::Win,
|
||||||
|
},
|
||||||
|
Pieces::Scissors => match player_one {
|
||||||
|
Pieces::Rock => EndStates::Win,
|
||||||
|
Pieces::Paper => EndStates::Lose,
|
||||||
|
Pieces::Scissors => EndStates::Draw,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let player_score = match player_one {
|
||||||
|
Pieces::Rock => 1,
|
||||||
|
Pieces::Paper => 2,
|
||||||
|
Pieces::Scissors => 3,
|
||||||
|
};
|
||||||
|
part_one_scores.push(player_score);
|
||||||
|
let game_score = match game_result {
|
||||||
|
EndStates::Lose => 0,
|
||||||
|
EndStates::Draw => 3,
|
||||||
|
EndStates::Win => 6,
|
||||||
|
};
|
||||||
|
part_one_scores.push(game_score);
|
||||||
|
|
||||||
|
let part_two_goal = match &line[2..3] {
|
||||||
|
"X" => EndStates::Lose,
|
||||||
|
"Y" => EndStates::Draw,
|
||||||
|
"Z" => EndStates::Win,
|
||||||
|
_ => return Err(anyhow!("Invalid part two goal char")),
|
||||||
|
};
|
||||||
|
|
||||||
|
let part_two_score = match part_two_goal {
|
||||||
|
EndStates::Lose => match opponent {
|
||||||
|
Pieces::Rock => 3 + 0,
|
||||||
|
Pieces::Paper => 1 + 0,
|
||||||
|
Pieces::Scissors => 2 + 0,
|
||||||
|
},
|
||||||
|
EndStates::Draw => match opponent {
|
||||||
|
Pieces::Rock => 1 + 3,
|
||||||
|
Pieces::Paper => 2 + 3,
|
||||||
|
Pieces::Scissors => 3 + 3,
|
||||||
|
},
|
||||||
|
EndStates::Win => match opponent {
|
||||||
|
Pieces::Rock => 2 + 6,
|
||||||
|
Pieces::Paper => 3 + 6,
|
||||||
|
Pieces::Scissors => 1 + 6,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
part_two_scores.push(part_two_score);
|
||||||
|
}
|
||||||
|
|
||||||
|
let part_one_sum: u32 = part_one_scores.into_iter().sum();
|
||||||
|
let part_two_sum: u32 = part_two_scores.into_iter().sum();
|
||||||
|
println!("part one {}", part_one_sum);
|
||||||
|
println!("part two {}", part_two_sum);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -1 +1,2 @@
|
||||||
pub mod day1;
|
pub mod day1;
|
||||||
|
pub mod day2;
|
||||||
|
|
|
@ -12,6 +12,9 @@ fn run_day(number: i32) -> Result<()> {
|
||||||
1 => {
|
1 => {
|
||||||
days::day1::run()?;
|
days::day1::run()?;
|
||||||
}
|
}
|
||||||
|
2 => {
|
||||||
|
days::day2::run()?;
|
||||||
|
}
|
||||||
_ => return Err(anyhow!("Invalid day provided")),
|
_ => return Err(anyhow!("Invalid day provided")),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
A Y
|
||||||
|
B X
|
||||||
|
C Z
|
Loading…
Reference in New Issue