Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use itertools::Itertools;
- use crate::year2::day2::RPS::{Paper, Rock, Scissors};
- #[derive(Clone, Copy, PartialEq)]
- enum RPS {
- Rock,
- Paper,
- Scissors,
- }
- pub fn day2_1() {
- let mut input = include_str!("../../input2").lines().map( |x| {
- let line = x.split_once(' ').unwrap();
- let first = match line.0 {
- "A" => Rock,
- "B" => Paper,
- "C" => Scissors,
- _ => {panic!()}
- };
- let second = match line.1 {
- "X" => Rock,
- "Y" => Paper,
- "Z" => Scissors,
- _ => {panic!()}
- };
- (first, second)
- }).collect_vec();
- let ans = input.iter().fold(0, |acc, &elem| {
- acc + match elem {
- (a, b) if a == b => {3 + score(b)},
- (a, b) if beats(a, b) => {6 + score(b)}
- (a, b) if !beats(a, b) => {0 + score(b)}
- _ => {panic!() }
- }
- });
- println!("{}", ans);
- }
- pub fn day2_2() {
- let mut input = include_str!("../../input2").lines().map( |x| {
- let line = x.split_once(' ').unwrap();
- let first = match line.0 {
- "A" => Rock,
- "B" => Paper,
- "C" => Scissors,
- _ => {panic!()}
- };
- let second = match line.1 {
- "X" => match first {
- Rock => {Scissors}
- Paper => {Rock}
- Scissors => {Paper}
- },
- "Y" => first,
- "Z" => match first {
- Rock => {Paper}
- Paper => {Scissors}
- Scissors => {Rock}
- },
- _ => {panic!()}
- };
- (first, second)
- }).collect_vec();
- let ans = input.iter().fold(0, |acc, &elem| {
- acc + match elem {
- (a, b) if a == b => {3 + score(b)},
- (a, b) if beats(a, b) => {6 + score(b)}
- (a, b) if !beats(a, b) => {0 + score(b)}
- _ => {panic!() }
- }
- });
- println!("{}", ans);
- }
- fn score(thing:RPS) -> i32
- {
- return match thing {
- Rock => 1,
- Paper => 2,
- Scissors => 3
- }
- }
- fn beats(opponent:RPS, your:RPS) -> bool
- {
- return match (opponent, your) {
- (a, b) if a == b => {panic!()},
- (Rock, Paper) => true,
- (Paper, Scissors) => true,
- (Scissors, Rock) => true,
- _ => {false}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement