Advent of Code 2023 - DAY 3

fushji

Antonio Perrone

Posted on December 3, 2023

Advent of Code 2023 - DAY 3

Problem Day 3: Gear Ratios

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

Solution Part 1

pub fn part_one(input: &str) -> Option<u32> {
    let grid = input.split('\n').collect::<Vec<&str>>();

    let mut coordinates: HashSet<[usize; 2]> = HashSet::new();

    for (r, line) in grid.iter().enumerate() {
        for (c, ch) in line.chars().enumerate() {
            if ch.is_numeric() || ch == '.' {
                continue;
            }

            for i in r - 1..r + 2 {
                for j in c - 1..c + 2 {
                    if i >= grid.len()
                        || j >= grid[i].len()
                        || !(grid[i].chars().nth(j).unwrap().is_numeric())
                    {
                        continue;
                    }
                    let mut k = j;
                    while k > 0 && grid[i].chars().nth(k - 1).unwrap().is_numeric() {
                        k -= 1;
                    }

                    coordinates.insert([i, k]);
                }
            }
        }
    }

    let mut result = 0;

    for c in coordinates.iter() {
        let mut digits: Vec<String> = vec![];

        let mut col = c[1];
        while col < grid[c[0]].len() && grid[c[0]].chars().nth(col).unwrap().is_numeric() {
            digits.push(grid[c[0]].chars().nth(col).unwrap().to_string());
            col += 1
        }

        result += digits.join("").parse::<u32>().unwrap();
    }
    Some(result)
}
Enter fullscreen mode Exit fullscreen mode

Solution Part 2

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

    for (r, line) in grid.iter().enumerate() {
        for (c, ch) in line.chars().enumerate() {
            if ch != '*' {
                continue;
            }

            let mut coordinates: HashSet<[usize; 2]> = HashSet::new();

            for i in r - 1..r + 2 {
                for j in c - 1..c + 2 {
                    if i >= grid.len()
                        || j >= grid[i].len()
                        || !(grid[i].chars().nth(j).unwrap().is_numeric())
                    {
                        continue;
                    }
                    let mut k = j;
                    while k > 0 && grid[i].chars().nth(k - 1).unwrap().is_numeric() {
                        k -= 1;
                    }

                    coordinates.insert([i, k]);
                }
            }

            if coordinates.len() != 2 {
                continue;
            }

            let mut product = 1;

            for c in coordinates.iter() {
                let mut digits: Vec<String> = vec![];

                let mut col = c[1];
                while col < grid[c[0]].len() && grid[c[0]].chars().nth(col).unwrap().is_numeric() {
                    digits.push(grid[c[0]].chars().nth(col).unwrap().to_string());
                    col += 1
                }

                product *= digits.join("").parse::<u32>().unwrap();
            }

            result += product;
        }
    }

    Some(result)
}
Enter fullscreen mode Exit fullscreen mode

Here the repository with all puzzle solutions.

💖 💪 🙅 🚩
fushji
Antonio Perrone

Posted on December 3, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Advent of Code 2023 - DAY 11
rust Advent of Code 2023 - DAY 11

January 3, 2024

Advent of Code 2023 - DAY 9
rust Advent of Code 2023 - DAY 9

December 18, 2023

Advent of Code 2023 - DAY 8
rust Advent of Code 2023 - DAY 8

December 15, 2023

Advent of Code 2023 - DAY 7
rust Advent of Code 2023 - DAY 7

December 14, 2023

Advent of Code 2023 - DAY 6
rust Advent of Code 2023 - DAY 6

December 12, 2023