DEV Community

Cover image for Advent of Code 2023 - DAY 2
Antonio Perrone
Antonio Perrone

Posted on • Updated on

Advent of Code 2023 - DAY 2

Problem Day 2: Cube Conundrum

Here the day 2 problem statement: https://adventofcode.com/2023/day/2

Solution Part 1

pub fn part_one(input: &str) -> Option<u32> {
    let bag = HashMap::from([("red", 12), ("green", 13), ("blue", 14)]);
    let games = input.split('\n').collect::<Vec<&str>>();
    let mut results = vec![true; games.len()];

    for (idx, game) in games.iter().enumerate() {
        if game.is_empty() {
            continue;
        }
        let rounds = game
            .split(": ")
            .collect::<Vec<&str>>()
            .get(1)
            .unwrap()
            .split("; ")
            .collect::<Vec<&str>>();

        for round in rounds {
            let cubes = round.split(", ").collect::<Vec<&str>>();
            cubes.iter().for_each(|cube| {
                let mut parts = cube.split(' ');
                let value = parts.next().unwrap().parse::<i32>().unwrap();
                let key = parts.next().unwrap();

                if *bag.get(key).unwrap() < value {
                    results[idx] = false;
                }
            });
        }
    }

    let mut result = 0_u32;
    for (idx, r) in results.iter().enumerate() {
        if *r {
            result += (idx + 1) as u32;
        }
    }
    Some(result)
}
Enter fullscreen mode Exit fullscreen mode

Solution Part 2

pub fn part_two(input: &str) -> Option<u32> {
    let games = input.split('\n').collect::<Vec<&str>>();
    let mut result = 0;

    for game in games {
        let mut red = 0;
        let mut green = 0;
        let mut blue = 0;

        if game.is_empty() {
            continue;
        }
        let rounds = game
            .split(": ")
            .collect::<Vec<&str>>()
            .get(1)
            .unwrap()
            .split("; ")
            .collect::<Vec<&str>>();

        for round in rounds {
            let cubes = round.split(", ").collect::<Vec<&str>>();
            cubes.iter().for_each(|cube| {
                let mut parts = cube.split(' ');
                let value = parts.next().unwrap().parse::<i32>().unwrap();
                let key = parts.next().unwrap();

                match key {
                    "red" => {
                        if red < value {
                            red = value
                        }
                    }
                    "green" => {
                        if green < value {
                            green = value
                        }
                    }
                    "blue" => {
                        if blue < value {
                            blue = value
                        }
                    }
                    _ => {}
                }
            });
        }

        result += red * green * blue;
    }

    Some(result as u32)
}
Enter fullscreen mode Exit fullscreen mode

Here the repository with all puzzle solutions.

Top comments (0)