Finished day 2
This commit is contained in:
parent
bc0ba67c16
commit
8dfa008aae
File diff suppressed because it is too large
Load Diff
|
@ -1,10 +1,10 @@
|
|||
fn elf_calorie_counts(input: &String) -> Vec<u64> {
|
||||
fn elf_calorie_counts(input: &str) -> Vec<u64> {
|
||||
input.split("\n\n").map(|elf| {
|
||||
elf.split_whitespace().map(|meal| meal.parse().unwrap_or(0)).sum()
|
||||
}).collect()
|
||||
}
|
||||
|
||||
pub fn part_1(input: &String) -> u64 {
|
||||
pub fn part_1(input: &str) -> u64 {
|
||||
let mut elf_calorie_counts= elf_calorie_counts(input);
|
||||
|
||||
elf_calorie_counts.sort_unstable();
|
||||
|
@ -12,7 +12,7 @@ pub fn part_1(input: &String) -> u64 {
|
|||
elf_calorie_counts.pop().unwrap()
|
||||
}
|
||||
|
||||
pub fn part_2(input: &String) -> u64 {
|
||||
pub fn part_2(input: &str) -> u64 {
|
||||
let mut elf_calorie_counts= elf_calorie_counts(input);
|
||||
|
||||
elf_calorie_counts.sort_unstable();
|
||||
|
|
|
@ -0,0 +1,133 @@
|
|||
#[derive(Debug)]
|
||||
enum Error {
|
||||
InvalidMove,
|
||||
InvalidOutcome,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
enum Move {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors
|
||||
}
|
||||
|
||||
impl Move {
|
||||
fn from_str(input: &str) -> Result<Self, Error> {
|
||||
match input {
|
||||
"A"|"X" => Ok(Self::Rock),
|
||||
"B"|"Y" => Ok(Self::Paper),
|
||||
"C"|"Z" => Ok(Self::Scissors),
|
||||
_ => Err(Error::InvalidMove)
|
||||
}
|
||||
}
|
||||
|
||||
fn beats(&self) -> Move {
|
||||
match self {
|
||||
Move::Rock => Move::Scissors,
|
||||
Move::Paper => Move::Rock,
|
||||
Move::Scissors => Move::Paper,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_beaten_by(&self) -> Move {
|
||||
match self {
|
||||
Move::Rock => Move::Paper,
|
||||
Move::Paper => Move::Scissors,
|
||||
Move::Scissors => Move::Rock,
|
||||
}
|
||||
}
|
||||
|
||||
/// I don't really know why, but these moves have different scores
|
||||
fn score(&self) -> u64 {
|
||||
match self {
|
||||
Move::Rock => 1,
|
||||
Move::Paper => 2,
|
||||
Move::Scissors => 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Outcome {
|
||||
Win,
|
||||
Draw,
|
||||
Lose,
|
||||
}
|
||||
|
||||
impl Outcome {
|
||||
pub fn from_str(input: &str) -> Result<Self, Error> {
|
||||
match input {
|
||||
"X" => Ok(Self::Lose),
|
||||
"Y" => Ok(Self::Draw),
|
||||
"Z" => Ok(Self::Win),
|
||||
_ => Err(Error::InvalidOutcome)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn play(player: Move, opponent: Move) -> Outcome {
|
||||
if player.beats() == opponent {
|
||||
Outcome::Win
|
||||
} else if player == opponent {
|
||||
Outcome::Draw
|
||||
} else {
|
||||
Outcome::Lose
|
||||
}
|
||||
}
|
||||
|
||||
/// decrypt a rock-paper-scissors strategy guide, run through it and calculate the outcome
|
||||
pub fn part_1(input: &str) -> u64 {
|
||||
let mut score = 0;
|
||||
let throws = input.lines();
|
||||
|
||||
for throw in throws {
|
||||
let mut throw: Vec<&str> = throw.split_whitespace().take(2).collect();
|
||||
let player = Move::from_str(throw.pop().unwrap()).unwrap();
|
||||
let opponent = Move::from_str(throw.pop().unwrap()).unwrap();
|
||||
|
||||
score += player.score();
|
||||
|
||||
match play(player, opponent) {
|
||||
Outcome::Win => {
|
||||
score += 6;
|
||||
}
|
||||
Outcome::Draw => {
|
||||
score += 3;
|
||||
}
|
||||
Outcome::Lose => {}
|
||||
}
|
||||
}
|
||||
|
||||
score
|
||||
}
|
||||
|
||||
/// decrypt a rock-paper-scissors strategy guide, run through it and calculate the outcome
|
||||
pub fn part_2(input: &str) -> u64 {
|
||||
let mut score = 0;
|
||||
let throws = input.lines();
|
||||
|
||||
for throw in throws {
|
||||
let mut throw: Vec<&str> = throw.split_whitespace().take(2).collect();
|
||||
let player = throw.pop().unwrap();
|
||||
let opponent = Move::from_str(throw.pop().unwrap()).unwrap();
|
||||
|
||||
let player = match Outcome::from_str(player).unwrap() {
|
||||
Outcome::Win => opponent.is_beaten_by(),
|
||||
Outcome::Draw => opponent.clone(),
|
||||
Outcome::Lose => opponent.beats(),
|
||||
};
|
||||
|
||||
score += player.score();
|
||||
|
||||
match play(player, opponent) {
|
||||
Outcome::Win => {
|
||||
score += 6;
|
||||
}
|
||||
Outcome::Draw => {
|
||||
score += 3;
|
||||
}
|
||||
Outcome::Lose => {}
|
||||
}
|
||||
}
|
||||
|
||||
score
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
mod day_1;
|
||||
mod day_2;
|
||||
|
||||
fn main() {
|
||||
let day_1_input = std::fs::read_to_string("input/day_1.txt").unwrap();
|
||||
println!("Day 1, part 1: {}", day_1::part_1(&day_1_input));
|
||||
println!("Day 1, part 2: {:?}", day_1::part_2(&day_1_input));
|
||||
println!("Day 1, part 1: {}", day_1::part_1(include_str!("../input/day_1.txt")));
|
||||
println!("Day 1, part 2: {}", day_1::part_2(include_str!("../input/day_1.txt")));
|
||||
println!("Day 2, part 1: {}", day_2::part_1(include_str!("../input/day_2.txt")));
|
||||
println!("Day 2, part 2: {}", day_2::part_2(include_str!("../input/day_2.txt")));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue