completed day 3, ready for day 4.

This commit is contained in:
gabe 2022-12-02 11:56:57 -06:00
parent d8c125572f
commit 4a9c8b37b9
15 changed files with 2778 additions and 2 deletions

View file

@ -14,5 +14,13 @@ path="src/template/solve.rs"
name="day01"
path="src/day01/solve.rs"
[[bin]]
name="day02"
path="src/day02/solve.rs"
[[bin]]
name="day03"
path="src/day03/solve.rs"
[dependencies]
regex="1"

2500
src/day02/input.txt Normal file

File diff suppressed because it is too large Load diff

63
src/day02/part1.rs Normal file
View file

@ -0,0 +1,63 @@
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
src/day02/part2.rs Normal file
View 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);
}
}

13
src/day02/solve.rs Normal file
View 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));
}

40
src/day02/utilities.rs Normal file
View file

@ -0,0 +1,40 @@
#[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{
//play enum has value corresponding to its score.
let mut score = input.you as usize;
//an enum wins if (you-elf)%3 = 1, looses if it = 2
let gamestatus = (input.you as i8 - input.elf as i8).rem_euclid(3);
match gamestatus {
1 => score+=6,
2 => (),
0 => score += 3,
_ => panic!("gamestatus was {}!, you were {}, elf was {}", gamestatus, input.you as i8, input.elf as i8)
}
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);
}
}

0
src/day03/input.txt Normal file
View file

15
src/day03/part1.rs Normal file
View file

@ -0,0 +1,15 @@
use crate::utilities::*;
pub fn part1() -> usize {
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() {
assert_eq!(part1(), 0);
}
}

15
src/day03/part2.rs Normal file
View file

@ -0,0 +1,15 @@
use crate::utilities::*;
pub fn part2() -> usize {
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part2() {
assert_eq!(part2(), 0);
}
}

14
src/day03/solve.rs Normal file
View file

@ -0,0 +1,14 @@
mod part1;
mod part2;
mod utilities;
fn main() {
let _input = include_str!("./input.txt");
let _structured_input = utilities::parse(_input);
println!("Part One");
println!("Result: {}", part1::part1());
println!("Part Two");
println!("Result: {}", part2::part2());
}

View file

@ -1,3 +1,5 @@
use crate::utilities::*;
pub fn part1() -> usize {
0
}

View file

@ -1,3 +1,5 @@
use crate::utilities::*;
pub fn part2() -> usize {
0
}

View file

@ -1,10 +1,10 @@
mod part1;
mod part2;
mod parse;
mod utilities;
fn main() {
let _input = include_str!("./input.txt");
let _structured_input = parse::parse(_input);
let _structured_input = utilities::parse(_input);
println!("Part One");
println!("Result: {}", part1::part1());

17
src/template/utilities.rs Normal file
View file

@ -0,0 +1,17 @@
pub fn parse(input: &str) -> usize {
println!("{}", input);
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
let input =
"test"
;
assert_eq!(parse(input), 0);
}
}