Skip to content

Commit

Permalink
Add Day 2 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Törnlund committed Dec 2, 2023
1 parent 8972827 commit ebec07e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 41 deletions.
117 changes: 77 additions & 40 deletions src/days/day_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub struct Day2P1 {
max_blue: u32,
}

pub struct Day2P2 {}

#[derive(Debug)]
struct Game {
id: u32,
Expand Down Expand Up @@ -35,6 +37,13 @@ impl Solution for Day2P1 {
}
}

impl Solution for Day2P2 {
fn solve(&self, input: &str) {
let input = self.get_input(input);
println!("Total: {}", self.calculate_game(&input));
}
}

impl Day2P1 {
pub fn new(r: u32, g: u32, b: u32) -> Self {
Self {
Expand All @@ -45,7 +54,7 @@ impl Day2P1 {
}

fn calculate_possible_games(&self, input: &str) -> u32 {
let games = self.parse_games(input);
let games = parse_games(input);

let mut total = 0;

Expand All @@ -67,67 +76,95 @@ impl Day2P1 {

total
}
}

fn parse_games(&self, input: &str) -> Vec<Game> {
/*
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
*/
impl Day2P2 {
pub fn new() -> Self {
Self {}
}

let mut games: Vec<Game> = Vec::new();
fn calculate_game(&self, input: &str) -> u32 {
let games = parse_games(input);

for line in input.lines() {
let colon_index = line.find(":").unwrap();
let game_index = line.find("Game").unwrap();
let game_id = &line[game_index+4..colon_index].trim();
let game_id = game_id.parse::<u32>().expect("Game id is not a number");
let mut res = 0;

for game in games {
let mut highest_red = 0;
let mut highest_green = 0;
let mut highest_blue = 0;

let game_str = line.get(colon_index+1..).unwrap();
for round in game.rounds {
if round.red_count > highest_red {
highest_red = round.red_count;
}
if round.green_count > highest_green {
highest_green = round.green_count;
}
if round.blue_count > highest_blue {
highest_blue = round.blue_count;
}
}

res += highest_red * highest_green * highest_blue;
}

res
}
}

fn parse_games(input: &str) -> Vec<Game> {
/*
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
*/

let mut rounds_result: Vec<Round> = Vec::new();
let mut games: Vec<Game> = Vec::new();

let rounds = game_str.split(";");
for line in input.lines() {
let colon_index = line.find(":").unwrap();
let game_index = line.find("Game").unwrap();
let game_id = &line[game_index+4..colon_index].trim();
let game_id = game_id.parse::<u32>().expect("Game id is not a number");

for round in rounds {
let round = round.trim();
let split = round.split(",");
let game_str = line.get(colon_index+1..).unwrap();

let mut rounds_result: Vec<Round> = Vec::new();

let mut current_round = Round {
red_count: 0,
green_count: 0,
blue_count: 0,
};
let rounds = game_str.split(";");

for s in split {
let s = s.trim();
let split = s.split(" ");
let split: Vec<&str> = split.collect();
for round in rounds {
let round = round.trim();
let split = round.split(",");

let count = split[0].parse::<u32>().expect("Count is not a number");
let color = split.last().expect("Count is not a number");
match color {
&"red" => { current_round.red_count += count; },
&"green" => { current_round.green_count += count; },
&"blue" => { current_round.blue_count += count; },
_ => { panic!("Unknown color: {}", color);
}

let mut current_round = Round {
red_count: 0,
green_count: 0,
blue_count: 0,
};

for s in split {
let s = s.trim();
let split = s.split(" ");
let split: Vec<&str> = split.collect();

let count = split[0].parse::<u32>().expect("Count is not a number");
let color = split.last().expect("Count is not a number");
match color {
&"red" => { current_round.red_count += count; },
&"green" => { current_round.green_count += count; },
&"blue" => { current_round.blue_count += count; },
_ => { panic!("Unknown color: {}", color);
}
}

rounds_result.push(current_round);
}


let game = Game::new(game_id, rounds_result);
games.push(game);
rounds_result.push(current_round);
}

games
games.push(Game::new(game_id, rounds_result));
}

games
}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use days::{day_1::{Day1P1, Day1P2}, solution::Solution};
use days::{day_2::{Day2P1}};
use days::{day_2::{Day2P1, Day2P2}};
mod days;

fn main() {
Day1P1.solve("src/input/day_1.txt");
Day1P2::new().solve("src/input/day_1.txt");

Day2P1::new(12, 13, 14).solve("src/input/day_2.txt");
Day2P2::new().solve("src/input/day_2.txt");
}

0 comments on commit ebec07e

Please sign in to comment.