Added part 2 + tests
This commit is contained in:
parent
3d30a16abe
commit
f1681143d3
|
|
@ -1,3 +1,9 @@
|
|||
use regex::Regex;
|
||||
|
||||
pub enum Part {
|
||||
One,
|
||||
Two
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Game {
|
||||
|
|
@ -6,7 +12,6 @@ struct Game {
|
|||
g_max: u32,
|
||||
b_max: u32
|
||||
}
|
||||
use regex::Regex;
|
||||
|
||||
struct GameConfiguration {
|
||||
r: u32,
|
||||
|
|
@ -58,6 +63,10 @@ impl Game {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn minimal_power(&self) -> u32 {
|
||||
self.r_max * self.g_max * self.b_max
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_line(line: &str) -> Game {
|
||||
|
|
@ -85,16 +94,58 @@ fn parse_line(line: &str) -> Game {
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn solve(lines: Vec<String>) -> u32 {
|
||||
pub fn solve(lines: Vec<String>, part: Part) -> u32 {
|
||||
let mut sum = 0;
|
||||
let conf = GameConfiguration {r: 12, g: 13, b: 14};
|
||||
|
||||
for line in lines {
|
||||
let game = parse_line(&line);
|
||||
if game.is_valid(&conf) {
|
||||
sum += game.id;
|
||||
match part {
|
||||
Part::One => {
|
||||
if game.is_valid(&conf) {
|
||||
sum += game.id;
|
||||
}
|
||||
},
|
||||
Part::Two => {
|
||||
sum += game.minimal_power();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sum
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parser(){
|
||||
let line = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green";
|
||||
|
||||
let game = parse_line(line);
|
||||
|
||||
assert_eq!(game.id, 1);
|
||||
assert_eq!(game.b_max, 6);
|
||||
assert_eq!(game.r_max, 4);
|
||||
assert_eq!(game.g_max, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_validity() {
|
||||
let conf1 = GameConfiguration{r: 2, b: 2, g: 2};
|
||||
let conf2 = GameConfiguration{r: 3, b: 3, g: 3};
|
||||
let game = Game{id: 1, r_max: 3, b_max: 2, g_max: 2};
|
||||
|
||||
assert!(!game.is_valid(&conf1));
|
||||
assert!(game.is_valid(&conf2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_power() {
|
||||
let game = Game{id: 1, r_max: 3, b_max: 2, g_max: 2};
|
||||
|
||||
assert_eq!(game.minimal_power(), 12);
|
||||
}
|
||||
}
|
||||
16
src/main.rs
16
src/main.rs
|
|
@ -6,11 +6,8 @@ mod utils;
|
|||
|
||||
|
||||
fn main() {
|
||||
let data_path = "./datasets/adventofcode.com_2023_day_2_input.txt";
|
||||
let result = day2::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for the day"));
|
||||
|
||||
|
||||
println!("Day 1 (part 1) result is {}", result);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -27,4 +24,15 @@ mod tests {
|
|||
assert_eq!(day1_result_part1, 54338);
|
||||
assert_eq!(day1_result_part2, 53389);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn day2() {
|
||||
let data_path = "./datasets/adventofcode.com_2023_day_2_input.txt";
|
||||
let result = day2::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for the day"), day2::Part::One);
|
||||
let result2 = day2::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for the day"), day2::Part::Two);
|
||||
|
||||
|
||||
assert_eq!(result, 2795);
|
||||
assert_eq!(result2, 75561);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue