Day 2
parent
b7e687e685
commit
76dfcb47a1
@ -0,0 +1,148 @@
|
||||
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);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue