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)]
|
#[derive(Debug)]
|
||||||
struct Game {
|
struct Game {
|
||||||
|
|
@ -6,7 +12,6 @@ struct Game {
|
||||||
g_max: u32,
|
g_max: u32,
|
||||||
b_max: u32
|
b_max: u32
|
||||||
}
|
}
|
||||||
use regex::Regex;
|
|
||||||
|
|
||||||
struct GameConfiguration {
|
struct GameConfiguration {
|
||||||
r: u32,
|
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 {
|
fn parse_line(line: &str) -> Game {
|
||||||
|
|
@ -85,16 +94,58 @@ fn parse_line(line: &str) -> Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn solve(lines: Vec<String>) -> u32 {
|
pub fn solve(lines: Vec<String>, part: Part) -> u32 {
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
let conf = GameConfiguration {r: 12, g: 13, b: 14};
|
let conf = GameConfiguration {r: 12, g: 13, b: 14};
|
||||||
|
|
||||||
for line in lines {
|
for line in lines {
|
||||||
let game = parse_line(&line);
|
let game = parse_line(&line);
|
||||||
if game.is_valid(&conf) {
|
match part {
|
||||||
sum += game.id;
|
Part::One => {
|
||||||
|
if game.is_valid(&conf) {
|
||||||
|
sum += game.id;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Part::Two => {
|
||||||
|
sum += game.minimal_power();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sum
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/main.rs
14
src/main.rs
|
|
@ -6,11 +6,8 @@ mod utils;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
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_part1, 54338);
|
||||||
assert_eq!(day1_result_part2, 53389);
|
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