Day 2, puzzle 2
This commit is contained in:
parent
9c3b97a521
commit
7dde03f46f
@ -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);
|
||||
}
|
30
src/day2.rs
30
src/day2.rs
@ -1,4 +1,4 @@
|
||||
#[derive(PartialEq, Debug)]
|
||||
#[derive(PartialEq, Debug, Clone, Copy)]
|
||||
pub enum Choice {
|
||||
Rock,
|
||||
Paper,
|
||||
@ -15,10 +15,10 @@ impl Choice {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Round {
|
||||
opponent: Choice,
|
||||
own: Choice,
|
||||
pub opponent: Choice,
|
||||
pub own: Choice,
|
||||
}
|
||||
|
||||
pub enum RoundResult {
|
||||
@ -56,28 +56,16 @@ impl Round {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_choice(choice_str: &str) -> Choice {
|
||||
match choice_str.chars().next().unwrap() {
|
||||
'A' => Choice::Rock,
|
||||
'B' => Choice::Paper,
|
||||
'C' => Choice::Scissors,
|
||||
'X' => Choice::Rock,
|
||||
'Y' => Choice::Paper,
|
||||
'Z' => Choice::Scissors,
|
||||
_ => panic!("Unknown choice {}", choice_str),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_rounds(input: &String) -> Vec<Round> {
|
||||
pub fn parse_rounds(input: &String) -> Vec<(char, char)> {
|
||||
let lines = input.lines();
|
||||
let mut rounds = Vec::new();
|
||||
|
||||
for line in lines {
|
||||
let (opponent, own) = line.split_once(' ').unwrap();
|
||||
rounds.push(Round {
|
||||
opponent: parse_choice(opponent),
|
||||
own: parse_choice(own),
|
||||
});
|
||||
rounds.push((
|
||||
opponent.chars().next().unwrap(),
|
||||
own.chars().next().unwrap(),
|
||||
));
|
||||
}
|
||||
|
||||
rounds
|
||||
|
Loading…
x
Reference in New Issue
Block a user