day 2 updated to have tests

This commit is contained in:
Dylan Thies
2023-08-30 20:43:36 -04:00
parent b9f9f878fe
commit f8ce0e6253

View File

@@ -1,7 +1,6 @@
#![warn(clippy::all, clippy::pedantic)] #![warn(clippy::all, clippy::pedantic)]
use std::fs::File; use std::fs;
use std::io::{prelude::*, BufReader};
#[derive(Debug)] #[derive(Debug)]
struct HoHoError {} struct HoHoError {}
@@ -51,12 +50,33 @@ impl Choice {
} }
} }
struct Game { struct Game1 {
pub opponent: Choice,
pub you: Choice,
}
impl Game1 {
fn score(self) -> i32 {
let outcome = self.you.cmp(&self.opponent);
(self.you as i32) + outcome
}
}
impl std::str::FromStr for Game1 {
type Err = HoHoError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let str_split = s.split(' ').collect::<Vec<&str>>();
let opponent: Choice = str_split[0].parse()?;
let you: Choice = str_split[1].parse()?;
Ok(Self { opponent, you })
}
}
struct Game2 {
pub opponent: Choice, pub opponent: Choice,
pub you: Choice, pub you: Choice,
} }
impl std::str::FromStr for Game { impl std::str::FromStr for Game2 {
type Err = HoHoError; type Err = HoHoError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let str_split = s.split(' ').collect::<Vec<&str>>(); let str_split = s.split(' ').collect::<Vec<&str>>();
@@ -69,11 +89,11 @@ impl std::str::FromStr for Game {
"Z" => opponent.loses(), "Z" => opponent.loses(),
_ => return Err(HoHoError {}), _ => return Err(HoHoError {}),
}; };
Ok(Game { opponent, you }) Ok(Self { opponent, you })
} }
} }
impl Game { impl Game2 {
fn outcome(&self) -> i32 { fn outcome(&self) -> i32 {
self.you.cmp(&self.opponent) self.you.cmp(&self.opponent)
} }
@@ -84,15 +104,45 @@ impl Game {
} }
} }
fn part1(input: &str) -> String {
input
.lines()
.map(|line| line.parse::<Game1>().unwrap().score())
.sum::<i32>()
.to_string()
}
fn part2(input: &str) -> String {
input
.lines()
.map(|line| line.parse::<Game2>().unwrap().score())
.sum::<i32>()
.to_string()
}
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
//read in file //read in file
let file = File::open("input")?; let file = fs::read_to_string("input")?;
let reader = BufReader::new(file);
let score: i32 = reader println!("Part2: {}", part1(&file));
.lines() println!("Part2: {}", part2(&file));
.map(|line| line.unwrap().parse::<Game>().unwrap().score())
.sum();
println!("Puzzle 1: {score}");
Ok(()) Ok(())
} }
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "A Y
B X
C Z";
#[test]
fn part1_works() {
debug_assert_eq!(part1(INPUT), "15");
}
#[test]
fn part2_works() {
debug_assert_eq!(part2(INPUT), "12");
}
}