this post was submitted on 02 Dec 2023
24 points (96.2% liked)

Advent Of Code

761 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 2: Cube Conundrum


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


πŸ”’This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

πŸ”“ Edit: Post has been unlocked after 6 minutes

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 5 points 11 months ago

Not too tricky today. Part 2 wasn't as big of a curveball as yesterday thankfully. I don't think it's the cleanest code I've ever written, but hey - the whole point of this is to get better at Rust, so I'll definitely be learning as I go, and coming back at the end to clean a lot of these up. I think for this one I'd like to look into a parsing crate like nom to clean up all the spliting and unwrapping in the two from() methods.

https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day02.rs

#[derive(Debug)]
struct Hand {
    blue: usize,
    green: usize,
    red: usize,
}

impl Hand {
    fn from(input: &str) -> Hand {
        let mut hand = Hand {
            blue: 0,
            green: 0,
            red: 0,
        };

        for color in input.split(", ") {
            let color = color.split_once(' ').unwrap();
            match color.1 {
                "blue" => hand.blue = color.0.parse::().unwrap(),
                "green" => hand.green = color.0.parse::().unwrap(),
                "red" => hand.red = color.0.parse::().unwrap(),
                _ => unreachable!("malformed input"),
            }
        }

        hand
    }
}

#[derive(Debug)]
struct Game {
    id: usize,
    hands: Vec,
}

impl Game {
    fn from(input: &str) -> Game {
        let (id, hands) = input.split_once(": ").unwrap();
        let id = id.split_once(" ").unwrap().1.parse::().unwrap();
        let hands = hands.split("; ").map(Hand::from).collect();
        Game { id, hands }
    }
}

pub struct Day02;

impl Solver for Day02 {
    fn star_one(&self, input: &str) -> String {
        input
            .lines()
            .map(Game::from)
            .filter(|game| {
                game.hands
                    .iter()
                    .all(|hand| hand.blue <= 14 && hand.green <= 13 && hand.red <= 12)
            })
            .map(|game| game.id)
            .sum::()
            .to_string()
    }

    fn star_two(&self, input: &str) -> String {
        input
            .lines()
            .map(Game::from)
            .map(|game| {
                let max_blue = game.hands.iter().map(|hand| hand.blue).max().unwrap();
                let max_green = game.hands.iter().map(|hand| hand.green).max().unwrap();
                let max_red = game.hands.iter().map(|hand| hand.red).max().unwrap();

                max_blue * max_green * max_red
            })
            .sum::()
            .to_string()
    }
}