You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
2.4 KiB
Rust
149 lines
2.4 KiB
Rust
pub mod a {
|
|
static INPUT: &str = include_str!("inputs/day2a.txt");
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
|
pub enum Rps {
|
|
Rock,
|
|
Paper,
|
|
Scissors,
|
|
}
|
|
impl Rps {
|
|
pub fn score(&self) -> u64 {
|
|
use Rps::*;
|
|
|
|
match self {
|
|
Rock => 1,
|
|
Paper => 2,
|
|
Scissors => 3,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub enum Outcome {
|
|
ElfWin,
|
|
MeWin,
|
|
Draw,
|
|
}
|
|
impl Outcome {
|
|
pub fn score(&self) -> u64 {
|
|
use Outcome::*;
|
|
|
|
match self {
|
|
ElfWin => 0,
|
|
Draw => 3,
|
|
MeWin => 6,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn get_outcome(elf: &Rps, me: &Rps) -> Outcome {
|
|
use Rps::*;
|
|
|
|
match (elf, me) {
|
|
(Paper, Rock) | (Rock, Scissors) | (Scissors, Paper) => {
|
|
Outcome::ElfWin
|
|
}
|
|
_ if elf == me => Outcome::Draw,
|
|
_ => Outcome::MeWin,
|
|
}
|
|
}
|
|
|
|
pub fn elf_to_rps(i: &str) -> Rps {
|
|
match i {
|
|
"A" => Rps::Rock,
|
|
"B" => Rps::Paper,
|
|
"C" => Rps::Scissors,
|
|
_ => panic!(),
|
|
}
|
|
}
|
|
|
|
pub fn me_to_rps(i: &str) -> Rps {
|
|
match i {
|
|
"X" => Rps::Rock,
|
|
"Y" => Rps::Paper,
|
|
"Z" => Rps::Scissors,
|
|
_ => panic!(),
|
|
}
|
|
}
|
|
|
|
pub fn go() {
|
|
let input = INPUT;
|
|
let score = input
|
|
.split('\n')
|
|
.filter(|line| !line.is_empty())
|
|
.map(|line| {
|
|
let mut it = line.split(' ');
|
|
let elf = it.next().unwrap();
|
|
let me = it.next().unwrap();
|
|
|
|
let elf = elf_to_rps(elf);
|
|
let me = me_to_rps(me);
|
|
|
|
let me_score = me.score() + get_outcome(&elf, &me).score();
|
|
|
|
me_score
|
|
})
|
|
.sum::<u64>();
|
|
|
|
println!("{}", score);
|
|
}
|
|
}
|
|
|
|
pub mod b {
|
|
use super::a::*;
|
|
|
|
static INPUT: &str = include_str!("inputs/day2a.txt");
|
|
|
|
fn to_outcome(me: &str) -> Outcome {
|
|
use Outcome::*;
|
|
match me {
|
|
"X" => ElfWin,
|
|
"Y" => Draw,
|
|
"Z" => MeWin,
|
|
_ => panic!(),
|
|
}
|
|
}
|
|
|
|
fn get_move(elf: &Rps, outcome: &Outcome) -> Rps {
|
|
use Outcome::*;
|
|
use Rps::*;
|
|
|
|
match outcome {
|
|
Draw => *elf,
|
|
ElfWin => match elf {
|
|
Rock => Scissors,
|
|
Scissors => Paper,
|
|
Paper => Rock,
|
|
},
|
|
MeWin => match elf {
|
|
Rock => Paper,
|
|
Paper => Scissors,
|
|
Scissors => Rock,
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn go() {
|
|
let input = INPUT;
|
|
let score = input
|
|
.split('\n')
|
|
.filter(|line| !line.is_empty())
|
|
.map(|line| {
|
|
let mut it = line.split(' ');
|
|
let elf = it.next().unwrap();
|
|
let outcome = it.next().unwrap();
|
|
|
|
let elf = super::a::elf_to_rps(elf);
|
|
let outcome = to_outcome(outcome);
|
|
let me = get_move(&elf, &outcome);
|
|
|
|
let me_score = me.score() + outcome.score();
|
|
|
|
me_score
|
|
})
|
|
.sum::<u64>();
|
|
|
|
println!("{}", score);
|
|
}
|
|
}
|