1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
Andrew Coleman cc843cba6b day 2 solution 2022-12-02 08:56:29 -05:00
Andrew Coleman 9a4ecb0907 cargo fmt 2022-12-02 08:56:16 -05:00
6 changed files with 2613 additions and 1 deletions

2500
2022/day2.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,11 @@ pub fn run() -> Result<()> {
let mut sorted_calories = calories.clone();
sorted_calories.sort_by(|a, b| b.cmp(a));
let top_three: u32 = sorted_calories.get(0..3).expect("Not able to be sorted").iter().sum();
let top_three: u32 = sorted_calories
.get(0..3)
.expect("Not able to be sorted")
.iter()
.sum();
println!("Top 3 calories {}", top_three);
Ok(())
}

101
2022/src/days/day2.rs Normal file
View File

@ -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(())
}

View File

@ -1 +1,2 @@
pub mod day1;
pub mod day2;

View File

@ -12,6 +12,9 @@ fn run_day(number: i32) -> Result<()> {
1 => {
days::day1::run()?;
}
2 => {
days::day2::run()?;
}
_ => return Err(anyhow!("Invalid day provided")),
}
Ok(())

3
2022/tests/day2.txt Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z