switched to workspaces.
This should let me make a cross-day library.
This commit is contained in:
		
							parent
							
								
									1469c3a32b
								
							
						
					
					
						commit
						242989bb95
					
				
					 57 changed files with 156 additions and 89 deletions
				
			
		
							
								
								
									
										6
									
								
								days/day02/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								days/day02/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,6 @@
 | 
			
		|||
[package]
 | 
			
		||||
name = "day02"
 | 
			
		||||
authors.workspace = true
 | 
			
		||||
description.workspace = true
 | 
			
		||||
version.workspace = true
 | 
			
		||||
edition.workspace = true
 | 
			
		||||
							
								
								
									
										2500
									
								
								days/day02/src/input.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2500
									
								
								days/day02/src/input.txt
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										13
									
								
								days/day02/src/main.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								days/day02/src/main.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,13 @@
 | 
			
		|||
mod part1;
 | 
			
		||||
mod part2;
 | 
			
		||||
mod utilities;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let _input = include_str!("./input.txt");
 | 
			
		||||
 | 
			
		||||
    println!("Part One");
 | 
			
		||||
    println!("Result: {}", part1::part1(_input));
 | 
			
		||||
 | 
			
		||||
    println!("Part Two");
 | 
			
		||||
    println!("Result: {}", part2::part2(_input));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										62
									
								
								days/day02/src/part1.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								days/day02/src/part1.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,62 @@
 | 
			
		|||
use crate::utilities::*;
 | 
			
		||||
pub fn part1(input: &str) -> usize {
 | 
			
		||||
    parse(input).iter().map(calc_score).sum()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn parse(input: &str) -> Vec<Strategy> {
 | 
			
		||||
    input
 | 
			
		||||
        .lines()
 | 
			
		||||
        .map(|line| {
 | 
			
		||||
            let elf = match line.as_bytes()[0] {
 | 
			
		||||
                b'A' => Play::Rock,
 | 
			
		||||
                b'B' => Play::Paper,
 | 
			
		||||
                b'C' => Play::Scissors,
 | 
			
		||||
                _ => panic!("your opponent not playing defined strategy!"),
 | 
			
		||||
            };
 | 
			
		||||
            let you = match line.as_bytes()[2] {
 | 
			
		||||
                b'X' => Play::Rock,
 | 
			
		||||
                b'Y' => Play::Paper,
 | 
			
		||||
                b'Z' => Play::Scissors,
 | 
			
		||||
                _ => panic!("you are not playing defined strategy!"),
 | 
			
		||||
            };
 | 
			
		||||
            Strategy { elf, you }
 | 
			
		||||
        })
 | 
			
		||||
        .collect()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_part1() {
 | 
			
		||||
        let input = "A Y
 | 
			
		||||
B X
 | 
			
		||||
C Z";
 | 
			
		||||
        assert_eq!(part1(input), 15);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_parse() {
 | 
			
		||||
        let input = "A Y
 | 
			
		||||
B X
 | 
			
		||||
C Z";
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            parse(input),
 | 
			
		||||
            vec![
 | 
			
		||||
                Strategy {
 | 
			
		||||
                    elf: Play::Rock,
 | 
			
		||||
                    you: Play::Paper
 | 
			
		||||
                },
 | 
			
		||||
                Strategy {
 | 
			
		||||
                    elf: Play::Paper,
 | 
			
		||||
                    you: Play::Rock
 | 
			
		||||
                },
 | 
			
		||||
                Strategy {
 | 
			
		||||
                    elf: Play::Scissors,
 | 
			
		||||
                    you: Play::Scissors
 | 
			
		||||
                }
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										87
									
								
								days/day02/src/part2.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								days/day02/src/part2.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,87 @@
 | 
			
		|||
use crate::utilities::*;
 | 
			
		||||
 | 
			
		||||
pub enum GameResult {
 | 
			
		||||
    Win,
 | 
			
		||||
    Loss,
 | 
			
		||||
    Draw,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct ResultStrategy {
 | 
			
		||||
    pub elf: Play,
 | 
			
		||||
    pub you: GameResult,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn part2(input: &str) -> usize {
 | 
			
		||||
    parse(input)
 | 
			
		||||
        .iter()
 | 
			
		||||
        .map(gen_strategy)
 | 
			
		||||
        .map(|strat| calc_score(&strat))
 | 
			
		||||
        .sum()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn gen_strategy(input: &ResultStrategy) -> Strategy {
 | 
			
		||||
    match input.you {
 | 
			
		||||
        GameResult::Win => Strategy {
 | 
			
		||||
            elf: input.elf,
 | 
			
		||||
            you: gen_win(input.elf),
 | 
			
		||||
        },
 | 
			
		||||
        GameResult::Draw => Strategy {
 | 
			
		||||
            elf: input.elf,
 | 
			
		||||
            you: input.elf,
 | 
			
		||||
        },
 | 
			
		||||
        GameResult::Loss => Strategy {
 | 
			
		||||
            elf: input.elf,
 | 
			
		||||
            you: gen_loss(input.elf),
 | 
			
		||||
        },
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn gen_win(opponent: Play) -> Play {
 | 
			
		||||
    match opponent {
 | 
			
		||||
        Play::Rock => Play::Paper,
 | 
			
		||||
        Play::Paper => Play::Scissors,
 | 
			
		||||
        Play::Scissors => Play::Rock,
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn gen_loss(opponent: Play) -> Play {
 | 
			
		||||
    match opponent {
 | 
			
		||||
        Play::Rock => Play::Scissors,
 | 
			
		||||
        Play::Paper => Play::Rock,
 | 
			
		||||
        Play::Scissors => Play::Paper,
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn parse(input: &str) -> Vec<ResultStrategy> {
 | 
			
		||||
    input
 | 
			
		||||
        .lines()
 | 
			
		||||
        .map(|line| {
 | 
			
		||||
            let elf = match line.as_bytes()[0] {
 | 
			
		||||
                b'A' => Play::Rock,
 | 
			
		||||
                b'B' => Play::Paper,
 | 
			
		||||
                b'C' => Play::Scissors,
 | 
			
		||||
                _ => panic!("your opponent not playing defined strategy!"),
 | 
			
		||||
            };
 | 
			
		||||
            let you = match line.as_bytes()[2] {
 | 
			
		||||
                b'X' => GameResult::Loss,
 | 
			
		||||
                b'Y' => GameResult::Draw,
 | 
			
		||||
                b'Z' => GameResult::Win,
 | 
			
		||||
                _ => panic!("you are not playing defined strategy!"),
 | 
			
		||||
            };
 | 
			
		||||
            ResultStrategy { elf, you }
 | 
			
		||||
        })
 | 
			
		||||
        .collect()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_part2() {
 | 
			
		||||
        let input = "A Y
 | 
			
		||||
B X
 | 
			
		||||
C Z";
 | 
			
		||||
        assert_eq!(part2(input), 12);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								days/day02/src/utilities.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								days/day02/src/utilities.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,36 @@
 | 
			
		|||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
 | 
			
		||||
pub enum Play {
 | 
			
		||||
    Rock = 1,
 | 
			
		||||
    Paper = 2,
 | 
			
		||||
    Scissors = 3,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, PartialEq, Eq)]
 | 
			
		||||
pub struct Strategy {
 | 
			
		||||
    pub elf: Play,
 | 
			
		||||
    pub you: Play,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn calc_score(input: &Strategy) -> usize {
 | 
			
		||||
    //an enum wins if (you-elf)%3 = 1, looses if it = 2
 | 
			
		||||
    (match (input.you as i8 - input.elf as i8).rem_euclid(3) {
 | 
			
		||||
        1 => 6,
 | 
			
		||||
        2 => 0,
 | 
			
		||||
        0 => 3,
 | 
			
		||||
        _ => unreachable!("you were {}, elf was {}", input.you as i8, input.elf as i8),
 | 
			
		||||
    }) + input.you as usize //play enum has value corresponding to its score.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_calc_score() {
 | 
			
		||||
        let input = Strategy {
 | 
			
		||||
            elf: Play::Scissors,
 | 
			
		||||
            you: Play::Scissors,
 | 
			
		||||
        };
 | 
			
		||||
        assert_eq!(calc_score(&input), 6);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue