Advertisement
nairby

Advent of Code 2022 Day 02

Dec 2nd, 2022 (edited)
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.21 KB | None | 0 0
  1. use std::env;
  2. use std::io::{self, prelude::*, BufReader};
  3. use std::fs::File;
  4.  
  5. #[derive(Debug,PartialEq,Eq)]
  6. enum GameResult {
  7.     Win = 6,
  8.     Draw = 3,
  9.     Lose = 0,
  10. }
  11. impl From<&str> for GameResult {
  12.     fn from(c: &str) -> Self {
  13.         match c {
  14.             "X" => Self::Lose,
  15.             "Y" => Self::Draw,
  16.             "Z" => Self::Win,
  17.             other => panic!("Unknown directive: {}",other),
  18.         }
  19.     }
  20. }
  21.  
  22. #[derive(Copy,Clone,Debug,PartialEq,Eq)]
  23. enum Play {
  24.     Rock = 1,
  25.     Paper = 2,
  26.     Scissors = 3,
  27. }
  28. impl From<&str> for Play {
  29.     fn from(c: &str) -> Self {
  30.         match c {
  31.             "A" | "X" => Self::Rock,
  32.             "B" | "Y" => Self::Paper,
  33.             "C" | "Z" => Self::Scissors,
  34.             other => panic!("Unknown play: {}",other),
  35.         }
  36.     }
  37. }
  38.  
  39. // plays: (their play, my play)
  40. fn result(plays: &(Play,Play)) -> GameResult {
  41.     use Play::*;
  42.     use GameResult::*;
  43.     match plays {
  44.         (Rock,Rock) | (Paper,Paper) | (Scissors,Scissors) => { Draw },
  45.         (Rock,Paper)     => { Win },
  46.         (Rock,Scissors)  => { Lose },
  47.         (Paper,Rock)     => { Lose },
  48.         (Paper,Scissors) => { Win },
  49.         (Scissors,Rock)  => { Win },
  50.         (Scissors,Paper) => { Lose },
  51.     }
  52. }
  53.  
  54. fn play_from_result(their_play: Play, result: &GameResult) -> Play {
  55.     use Play::*;
  56.     use GameResult::*;
  57.     match result {
  58.         Draw => their_play,
  59.         Win => {
  60.             match their_play {
  61.                 Rock     => Paper,
  62.                 Paper    => Scissors,
  63.                 Scissors => Rock,
  64.             }},
  65.         Lose => {
  66.             match their_play {
  67.                 Rock     => Scissors,
  68.                 Paper    => Rock,
  69.                 Scissors => Paper,
  70.             }},
  71.     }
  72. }
  73.  
  74. fn plays(s: &String) -> (Play,Play) {
  75.     let plays: Vec<_> = s.split_whitespace().collect();
  76.     (Play::from(plays[0]), Play::from(plays[1]))
  77. }
  78.  
  79. fn play_and_result(s: &String) -> (Play,GameResult) {
  80.     let play_and_result: Vec<_> = s.split_whitespace().collect();
  81.     (Play::from(play_and_result[0]), GameResult::from(play_and_result[1]))
  82. }
  83.  
  84. fn solve(input: &str) -> io::Result<()> {
  85.     let file = File::open(input).expect("Input file not found.");
  86.     let reader = BufReader::new(file);
  87.  
  88.     // Input
  89.     let input: Vec<String> = match reader.lines().collect() {
  90.         Err(err) => panic!("Unknown error reading input: {}", err),
  91.         Ok(result) => result,
  92.     };
  93.  
  94.     // Part 1
  95.     let part1 = input
  96.         .iter()
  97.         .map(|x| {
  98.             let (their_play,my_play) = plays(x);
  99.             let result = result(&(their_play,my_play));
  100.             my_play as usize + result as usize
  101.         })
  102.         .sum::<usize>();
  103.     println!("Part 1: {}", part1); // 11906
  104.  
  105.     // Part 2
  106.     let part2 = input
  107.         .iter()
  108.         .map(|x| {
  109.             let (play,result) = play_and_result(x);
  110.             let my_play = play_from_result(play,&result);
  111.             my_play as usize + result as usize
  112.         })
  113.         .sum::<usize>();
  114.     println!("Part 2: {}", part2); //  11186
  115.  
  116.     Ok(())
  117. }
  118.  
  119. fn main() {
  120.     let args: Vec<String> = env::args().collect();
  121.     let filename = &args[1];
  122.     solve(&filename).unwrap();
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement