Day 2, puzzle 2
This commit is contained in:
@ -1,9 +1,32 @@
|
||||
use aoc22::day2;
|
||||
use aoc22::util;
|
||||
|
||||
fn parse_choice(choice_char: char) -> day2::Choice {
|
||||
match choice_char {
|
||||
'A' => day2::Choice::Rock,
|
||||
'B' => day2::Choice::Paper,
|
||||
'C' => day2::Choice::Scissors,
|
||||
'X' => day2::Choice::Rock,
|
||||
'Y' => day2::Choice::Paper,
|
||||
'Z' => day2::Choice::Scissors,
|
||||
_ => panic!("Unknown choice {}", choice_char),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_round(round: &(char, char)) -> day2::Round {
|
||||
day2::Round {
|
||||
opponent: parse_choice(round.0),
|
||||
own: parse_choice(round.1),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let rounds = day2::parse_rounds(&util::parse_input());
|
||||
|
||||
let score = rounds.iter().map(day2::Round::score).sum::<u32>();
|
||||
let score = rounds
|
||||
.iter()
|
||||
.map(parse_round)
|
||||
.map(|round| round.score())
|
||||
.sum::<u32>();
|
||||
println!("Total score is {}", score);
|
||||
}
|
||||
|
||||
47
src/bin/d2p2.rs
Normal file
47
src/bin/d2p2.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use aoc22::day2::{self, Choice, Round, RoundResult};
|
||||
use aoc22::util;
|
||||
|
||||
fn parse_choice(choice_char: char) -> Choice {
|
||||
match choice_char {
|
||||
'A' => Choice::Rock,
|
||||
'B' => Choice::Paper,
|
||||
'C' => Choice::Scissors,
|
||||
_ => panic!("Unknown choice {}", choice_char),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_result(result_char: char) -> RoundResult {
|
||||
match result_char {
|
||||
'X' => RoundResult::Loss,
|
||||
'Y' => RoundResult::Draw,
|
||||
'Z' => RoundResult::Win,
|
||||
_ => panic!("Unknown result {}", result_char),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_round(round: &(char, char)) -> Round {
|
||||
let opponent = parse_choice(round.0);
|
||||
let result = parse_result(round.1);
|
||||
let (win, loss) = match opponent {
|
||||
Choice::Rock => (Choice::Paper, Choice::Scissors),
|
||||
Choice::Paper => (Choice::Scissors, Choice::Rock),
|
||||
Choice::Scissors => (Choice::Rock, Choice::Paper),
|
||||
};
|
||||
let own = match result {
|
||||
RoundResult::Win => win,
|
||||
RoundResult::Draw => opponent,
|
||||
RoundResult::Loss => loss,
|
||||
};
|
||||
Round { opponent, own }
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let rounds = day2::parse_rounds(&util::parse_input());
|
||||
|
||||
let score = rounds
|
||||
.iter()
|
||||
.map(parse_round)
|
||||
.map(|round| round.score())
|
||||
.sum::<u32>();
|
||||
println!("Total score is {}", score);
|
||||
}
|
||||
Reference in New Issue
Block a user