day 2
This commit is contained in:
		@@ -11,6 +11,10 @@ path = "src/lib.rs"
 | 
				
			|||||||
name = "day1"
 | 
					name = "day1"
 | 
				
			||||||
path = "src/day1.rs"
 | 
					path = "src/day1.rs"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[bin]]
 | 
				
			||||||
 | 
					name = "day2"
 | 
				
			||||||
 | 
					path = "src/day2.rs"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
					# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[dependencies]
 | 
					[dependencies]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,21 +1,5 @@
 | 
				
			|||||||
const DATA: &'static str = include_str!("../data/day1.txt");
 | 
					const DATA: &'static str = include_str!("../data/day1.txt");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// const DATA: &'static str = "1000
 | 
					 | 
				
			||||||
// 2000
 | 
					 | 
				
			||||||
// 3000
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 4000
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 5000
 | 
					 | 
				
			||||||
// 6000
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 7000
 | 
					 | 
				
			||||||
// 8000
 | 
					 | 
				
			||||||
// 9000
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 10000
 | 
					 | 
				
			||||||
// ";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() {
 | 
					fn main() {
 | 
				
			||||||
    let mut counts = DATA.trim()
 | 
					    let mut counts = DATA.trim()
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										108
									
								
								2022/src/day2.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								2022/src/day2.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
				
			|||||||
 | 
					use std::str::FromStr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const DATA: &'static str = include_str!("../data/day2.txt");
 | 
				
			||||||
 | 
					// const DATA: &'static str = "
 | 
				
			||||||
 | 
					// A Y
 | 
				
			||||||
 | 
					// B X
 | 
				
			||||||
 | 
					// C Z
 | 
				
			||||||
 | 
					// ";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					enum RPS {
 | 
				
			||||||
 | 
					    Rock = 1,
 | 
				
			||||||
 | 
					    Paper = 2,
 | 
				
			||||||
 | 
					    Scissors = 3,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					impl FromStr for RPS {
 | 
				
			||||||
 | 
					    type Err = ();
 | 
				
			||||||
 | 
					    fn from_str(s: &str) -> Result<Self, Self::Err> {
 | 
				
			||||||
 | 
					        match s {
 | 
				
			||||||
 | 
					            "A" | "X" => Ok(Rock),
 | 
				
			||||||
 | 
					            "B" | "Y" => Ok(Paper),
 | 
				
			||||||
 | 
					            "C" | "Z" => Ok(Scissors),
 | 
				
			||||||
 | 
					            _ => Err(())
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					enum Outcome {
 | 
				
			||||||
 | 
					    Win = 6,
 | 
				
			||||||
 | 
					    Loss = 0,
 | 
				
			||||||
 | 
					    Draw = 3,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					impl FromStr for Outcome {
 | 
				
			||||||
 | 
					    type Err = ();
 | 
				
			||||||
 | 
					    fn from_str(s: &str) -> Result<Self, ()> {
 | 
				
			||||||
 | 
					        match s {
 | 
				
			||||||
 | 
					            "X" => Ok(Loss),
 | 
				
			||||||
 | 
					            "Y" => Ok(Draw),
 | 
				
			||||||
 | 
					            "Z" => Ok(Win),
 | 
				
			||||||
 | 
					            _ => Err(())
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use RPS::*;
 | 
				
			||||||
 | 
					use Outcome::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn compare(me: &RPS, them: &RPS) -> Outcome {
 | 
				
			||||||
 | 
					    match (me, them) {
 | 
				
			||||||
 | 
					        (Rock, Rock) => Draw,
 | 
				
			||||||
 | 
					        (Rock, Paper) => Loss,
 | 
				
			||||||
 | 
					        (Rock, Scissors) => Win,
 | 
				
			||||||
 | 
					        (Paper, Rock) => Win,
 | 
				
			||||||
 | 
					        (Paper, Paper) => Draw,
 | 
				
			||||||
 | 
					        (Paper, Scissors) => Loss,
 | 
				
			||||||
 | 
					        (Scissors, Rock) => Loss,
 | 
				
			||||||
 | 
					        (Scissors, Paper) => Win,
 | 
				
			||||||
 | 
					        (Scissors, Scissors) => Draw,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn get_response(opponent: &RPS, outcome: &Outcome) -> RPS {
 | 
				
			||||||
 | 
					    match (opponent, outcome) {
 | 
				
			||||||
 | 
					        (Rock, Win) => Paper,
 | 
				
			||||||
 | 
					        (Rock, Loss) => Scissors,
 | 
				
			||||||
 | 
					        (Rock, Draw) => Rock,
 | 
				
			||||||
 | 
					        (Paper, Win) => Scissors,
 | 
				
			||||||
 | 
					        (Paper, Loss) => Rock,
 | 
				
			||||||
 | 
					        (Paper, Draw) => Paper,
 | 
				
			||||||
 | 
					        (Scissors, Win) => Rock,
 | 
				
			||||||
 | 
					        (Scissors, Loss) => Paper,
 | 
				
			||||||
 | 
					        (Scissors, Draw) => Scissors,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn score(me: RPS, them: RPS) -> u64 {
 | 
				
			||||||
 | 
					    let outcome = compare(&me, &them);
 | 
				
			||||||
 | 
					    (me as u64) + (outcome as u64)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn main() {
 | 
				
			||||||
 | 
					    let mut total_1 = 0;
 | 
				
			||||||
 | 
					    for pair in DATA.trim().split("\n") {
 | 
				
			||||||
 | 
					        let mut it = pair.split(" ")
 | 
				
			||||||
 | 
					            .map(|v| v.parse::<RPS>().unwrap());
 | 
				
			||||||
 | 
					        let (them, me) = (it.next().unwrap(), it.next().unwrap());
 | 
				
			||||||
 | 
					        total_1 += score(me, them);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("Part 1: {total_1}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut total_2 = 0;
 | 
				
			||||||
 | 
					    for pair in DATA.trim().split("\n") {
 | 
				
			||||||
 | 
					        let mut it = pair.split(" ");
 | 
				
			||||||
 | 
					        let them = it.next().unwrap().parse::<RPS>().unwrap();
 | 
				
			||||||
 | 
					        let outcome = it.next().unwrap().parse::<Outcome>().unwrap();
 | 
				
			||||||
 | 
					        let me = get_response(&them, &outcome);
 | 
				
			||||||
 | 
					        total_2 += me as u64;
 | 
				
			||||||
 | 
					        total_2 += outcome as u64
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    println!("Part 2: {total_2}");
 | 
				
			||||||
 | 
					}   
 | 
				
			||||||
		Reference in New Issue
	
	Block a user