Day 2, puzzle 2
This commit is contained in:
@ -1,9 +1,32 @@
|
|||||||
use aoc22::day2;
|
use aoc22::day2;
|
||||||
use aoc22::util;
|
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() {
|
pub fn main() {
|
||||||
let rounds = day2::parse_rounds(&util::parse_input());
|
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);
|
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 {
|
pub enum Choice {
|
||||||
Rock,
|
Rock,
|
||||||
Paper,
|
Paper,
|
||||||
@ -15,10 +15,10 @@ impl Choice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Round {
|
pub struct Round {
|
||||||
opponent: Choice,
|
pub opponent: Choice,
|
||||||
own: Choice,
|
pub own: Choice,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum RoundResult {
|
pub enum RoundResult {
|
||||||
@ -56,28 +56,16 @@ impl Round {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_choice(choice_str: &str) -> Choice {
|
pub fn parse_rounds(input: &String) -> Vec<(char, char)> {
|
||||||
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> {
|
|
||||||
let lines = input.lines();
|
let lines = input.lines();
|
||||||
let mut rounds = Vec::new();
|
let mut rounds = Vec::new();
|
||||||
|
|
||||||
for line in lines {
|
for line in lines {
|
||||||
let (opponent, own) = line.split_once(' ').unwrap();
|
let (opponent, own) = line.split_once(' ').unwrap();
|
||||||
rounds.push(Round {
|
rounds.push((
|
||||||
opponent: parse_choice(opponent),
|
opponent.chars().next().unwrap(),
|
||||||
own: parse_choice(own),
|
own.chars().next().unwrap(),
|
||||||
});
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
rounds
|
rounds
|
||||||
|
|||||||
Reference in New Issue
Block a user