aoc2022

Advent of Code 2022 solutions in Rust
git clone git://code.mfashby.net:/aoc2022
Log | Files | Refs

day2.rs (3598B)


      1 // Accumulator structure... 
      2 // parse each line
      3 
      4 use simple_error::{SimpleError,bail};
      5 
      6 #[derive(PartialEq)]
      7 enum Move{ Rock, Paper, Scissors }
      8 enum Res { Win, Draw, Loss }
      9 
     10 fn parse_m1(c: char) -> Result<Move, SimpleError> {
     11     match c {
     12         'A' => Ok(Move::Rock),
     13         'B' => Ok(Move::Paper),
     14         'C' => Ok(Move::Scissors),
     15         _ => bail!("invalid character {}", c)
     16     }
     17 }
     18 // fn parse_m2(c: char) -> Result<Move, SimpleError> {
     19 //     match c {
     20 //         'X' => Ok(Move::Rock),
     21 //         'Y' => Ok(Move::Paper),
     22 //         'Z' => Ok(Move::Scissors),
     23 //        _ => bail!("invalid character {}", c)
     24 //     }
     25 // }
     26 fn parse_req_res(c: char) -> Result<Res, SimpleError> {
     27     match c {
     28         'X' => Ok(Res::Loss),
     29         'Y' => Ok(Res::Draw),
     30         'Z' => Ok(Res::Win),
     31         _ => bail!("invalid character {}", c)
     32     }
     33 }
     34 // fn win(opp: Move, you: Move) -> Res {
     35 //     match opp {
     36 //         Move::Rock => match you {
     37 //             Move::Rock => Res::Draw,
     38 //             Move::Paper => Res::Win,
     39 //             Move::Scissors => Res::Loss,
     40 //         },
     41 //         Move::Paper => match you {
     42 //             Move::Rock => Res::Loss,
     43 //             Move::Paper => Res::Draw,
     44 //             Move::Scissors => Res::Win,
     45 //         },
     46 //         Move::Scissors => match you {
     47 //             Move::Rock => Res::Win,
     48 //             Move::Paper => Res::Loss,
     49 //             Move::Scissors => Res::Draw,
     50 //         }
     51 //     }
     52 // }
     53 fn calc_req_move(opp: Move, req_res: Res) -> Move {
     54     match req_res {
     55         Res::Win => match opp {
     56             Move::Rock => Move::Paper,
     57             Move::Paper => Move::Scissors,
     58             Move::Scissors => Move::Rock
     59         }
     60         Res::Draw => opp,
     61         Res::Loss => match opp {
     62             Move::Rock => Move::Scissors,
     63             Move::Paper => Move::Rock,
     64             Move::Scissors => Move::Paper   
     65         }
     66     }
     67 }
     68 
     69 // fn score(opp: Move, you: Move) -> u32 {
     70 //     let ms: u32 = match you {
     71 //         Move::Rock => 1,
     72 //         Move::Paper => 2,
     73 //         Move::Scissors => 3,
     74 //     };
     75 //     let ws: u32 = match win(opp, you) {
     76 //         Res::Win => 6,
     77 //         Res::Draw => 3,
     78 //         Res::Loss => 0,
     79 //     };
     80 //     return ms+ws;
     81 // }
     82 fn score(opp: Move, req_res: Res) -> u32 {
     83     let ws: u32 = match req_res {
     84         Res::Win => 6,
     85         Res::Draw => 3,
     86         Res::Loss => 0,
     87     };
     88     let mm = calc_req_move(opp, req_res);
     89     let ms: u32 = match mm {
     90         Move::Rock => 1,
     91         Move::Paper => 2,
     92         Move::Scissors => 3,
     93     };
     94     return ms+ws;
     95 }
     96 
     97 // fn parse_turn(line: &str) -> Result<(Move, Move), SimpleError> {
     98 //     let mut lc = line.chars();
     99 //     let e1: SimpleError = SimpleError::new("not enough characters in input!");
    100 //     let c1 = lc.next().ok_or(e1.clone())?;
    101 //     lc.next().ok_or(e1.clone())?;
    102 //     let c2 = lc.next().ok_or(e1.clone())?;
    103 //     let m_opp = parse_m1(c1)?;
    104 //     let m_you = parse_m2(c2)?;
    105 //     Ok((m_opp, m_you))
    106 // }
    107 fn parse_turn(line: &str) -> Result<(Move, Res), SimpleError> {
    108     let mut lc = line.chars();
    109     let e1: SimpleError = SimpleError::new("not enough characters in input!");
    110     let c1 = lc.next().ok_or(e1.clone())?;
    111     lc.next().ok_or(e1.clone())?;
    112     let c2 = lc.next().ok_or(e1.clone())?;
    113     let m_opp = parse_m1(c1)?;
    114     let req_res = parse_req_res(c2)?;
    115     Ok((m_opp, req_res))
    116 }
    117 
    118 pub fn run(input: String) {
    119     let score: u32 = input.lines().map(|line| {
    120         let (m_opp, m_you) = parse_turn(line).expect("invalid input line!");
    121         score(m_opp, m_you)
    122     }).sum::<u32>();
    123     println!("day 2: {}", score);
    124 }