DEV Community

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

Posted on

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.

Top comments (0)