[rs] Solve 2022_02 part 1
This commit is contained in:
parent
07f272073a
commit
1a8b4df940
3 changed files with 58 additions and 0 deletions
|
|
@ -38,6 +38,7 @@ impl Day {
|
|||
|
||||
days! {
|
||||
Y2022D01: "2022_01",
|
||||
Y2022D02: "2022_02",
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
|
|
@ -71,6 +72,7 @@ fn main() -> anyhow::Result<()> {
|
|||
let input = fs::read_to_string(file)?;
|
||||
match day {
|
||||
Day::Y2022D01 => y2022::d01::solve(input)?,
|
||||
Day::Y2022D02 => y2022::d02::solve(input)?,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
pub mod d01;
|
||||
pub mod d02;
|
||||
|
|
|
|||
55
rs/src/y2022/d02.rs
Normal file
55
rs/src/y2022/d02.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Choice {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
|
||||
impl Choice {
|
||||
fn score_against(self, other: Self) -> u32 {
|
||||
use self::Choice::*;
|
||||
|
||||
(match self {
|
||||
Rock => 1,
|
||||
Paper => 2,
|
||||
Scissors => 3,
|
||||
} + match (self, other) {
|
||||
(Rock, Paper) | (Paper, Scissors) | (Scissors, Rock) => 0,
|
||||
(Rock, Rock) | (Paper, Paper) | (Scissors, Scissors) => 3,
|
||||
(Rock, Scissors) | (Paper, Rock) | (Scissors, Paper) => 6,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn read_round(line: &str) -> (Choice, Choice) {
|
||||
let elems = line.split(' ').take(2).collect::<Vec<_>>();
|
||||
let l = match elems[0] {
|
||||
"A" => Choice::Rock,
|
||||
"B" => Choice::Paper,
|
||||
"C" => Choice::Scissors,
|
||||
_ => panic!(),
|
||||
};
|
||||
let r = match elems[1] {
|
||||
"X" => Choice::Rock,
|
||||
"Y" => Choice::Paper,
|
||||
"Z" => Choice::Scissors,
|
||||
_ => panic!(),
|
||||
};
|
||||
(l, r)
|
||||
}
|
||||
|
||||
pub fn solve(input: String) -> anyhow::Result<()> {
|
||||
let choices = input
|
||||
.lines()
|
||||
.map(|l| read_round(l.trim()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Part 1
|
||||
let score = choices
|
||||
.iter()
|
||||
.map(|(l, r)| r.score_against(*l))
|
||||
.sum::<u32>();
|
||||
println!("Part 1: {score}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue