Advertisement
Guest User

day2

a guest
Dec 2nd, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.41 KB | None | 0 0
  1. use itertools::Itertools;
  2. use crate::year2::day2::RPS::{Paper, Rock, Scissors};
  3.  
  4. #[derive(Clone, Copy, PartialEq)]
  5. enum RPS {
  6.     Rock,
  7.     Paper,
  8.     Scissors,
  9. }
  10.  
  11. pub fn day2_1() {
  12.     let mut input = include_str!("../../input2").lines().map( |x| {
  13.         let line = x.split_once(' ').unwrap();
  14.         let first = match line.0 {
  15.             "A" => Rock,
  16.             "B" => Paper,
  17.             "C" => Scissors,
  18.             _ => {panic!()}
  19.         };
  20.         let second = match line.1 {
  21.             "X" => Rock,
  22.             "Y" => Paper,
  23.             "Z" => Scissors,
  24.             _ => {panic!()}
  25.         };
  26.         (first, second)
  27.     }).collect_vec();
  28.  
  29.     let ans = input.iter().fold(0, |acc, &elem| {
  30.         acc + match elem {
  31.             (a, b) if a == b => {3 + score(b)},
  32.             (a, b) if beats(a, b) => {6 + score(b)}
  33.             (a, b) if !beats(a, b) => {0 + score(b)}
  34.             _ => {panic!() }
  35.         }
  36.     });
  37.     println!("{}", ans);
  38. }
  39.  
  40. pub fn day2_2() {
  41.     let mut input = include_str!("../../input2").lines().map( |x| {
  42.         let line = x.split_once(' ').unwrap();
  43.         let first = match line.0 {
  44.             "A" => Rock,
  45.             "B" => Paper,
  46.             "C" => Scissors,
  47.             _ => {panic!()}
  48.         };
  49.         let second = match line.1 {
  50.             "X" => match first {
  51.                 Rock => {Scissors}
  52.                 Paper => {Rock}
  53.                 Scissors => {Paper}
  54.             },
  55.             "Y" => first,
  56.             "Z" => match first {
  57.                 Rock => {Paper}
  58.                 Paper => {Scissors}
  59.                 Scissors => {Rock}
  60.             },
  61.             _ => {panic!()}
  62.         };
  63.         (first, second)
  64.     }).collect_vec();
  65.  
  66.     let ans = input.iter().fold(0, |acc, &elem| {
  67.         acc + match elem {
  68.             (a, b) if a == b => {3 + score(b)},
  69.             (a, b) if beats(a, b) => {6 + score(b)}
  70.             (a, b) if !beats(a, b) => {0 + score(b)}
  71.             _ => {panic!() }
  72.         }
  73.     });
  74.     println!("{}", ans);
  75. }
  76.  
  77. fn score(thing:RPS) -> i32
  78. {
  79.     return match thing {
  80.         Rock => 1,
  81.         Paper => 2,
  82.         Scissors => 3
  83.     }
  84. }
  85.  
  86. fn beats(opponent:RPS, your:RPS) -> bool
  87. {
  88.     return match (opponent, your) {
  89.         (a, b) if a == b => {panic!()},
  90.         (Rock, Paper) => true,
  91.         (Paper, Scissors) => true,
  92.         (Scissors, Rock) => true,
  93.         _ => {false}
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement