this post was submitted on 03 Dec 2023
20 points (95.5% liked)

Advent Of Code

777 readers
43 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
20
submitted 11 months ago* (last edited 11 months ago) by Ategon to c/advent_of_code
 

Day 3: Gear Ratios


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 11 minutes

you are viewing a single comment's thread
view the rest of the comments
[–] Ategon 2 points 11 months ago* (last edited 11 months ago)

[Rust] Harder one today, for part 1 I ended up getting stuck for a bit since I wasnt taking numbers at the end of lines into account and in part 2 I defined my gears vector in the wrong spot and spent a bit debugging that

Code(lemmy removes some chars, all chars are in code link)

use std::fs;

fn part1(input: String) -> u32 {
    let lines = input.lines().collect::>();
    let mut sum = 0;

    for i in 0..lines.len() {
        let mut num = 0;
        let mut valid = false;
        let chars = lines[i].chars().collect::>();

        for j in 0..chars.len() {
            let character = chars[j];
            let parts = ['*', '#', '+', '$', '/', '%', '=', '-', '&', '@'];

            if character.is_digit(10) {
                num = num * 10 + character.to_digit(10).unwrap();

                if i > 0 {
                    if parts.contains(&lines[i - 1].chars().collect::>()[j]) {
                        valid = true;
                    }

                    if j > 0 {
                        if parts.contains(&lines[i - 1].chars().collect::>()[j - 1]) {
                            valid = true;
                        }
                    }

                    if j < chars.len() - 1 {
                        if parts.contains(&lines[i - 1].chars().collect::>()[j + 1]) {
                            valid = true;
                        }
                    }
                }

                if i < lines.len() - 1 {
                    if parts.contains(&lines[i + 1].chars().collect::>()[j]) {
                        valid = true;
                    }

                    if j > 0 {
                        if parts.contains(&lines[i + 1].chars().collect::>()[j - 1]) {
                            valid = true;
                        }
                    }

                    if j < chars.len() - 1 {
                        if parts.contains(&lines[i + 1].chars().collect::>()[j + 1]) {
                            valid = true;
                        }
                    }
                }

                if j > 0 {
                    if parts.contains(&lines[i].chars().collect::>()[j - 1]) {
                        valid = true;
                    }
                }

                if j < chars.len() - 1 {
                    if parts.contains(&lines[i].chars().collect::>()[j + 1]) {
                        valid = true;
                    }
                }
            }
            else {
                if valid == true {
                    sum += num;
                }

                num = 0;
                valid = false;
            }

            if j == chars.len() - 1 {
                if valid == true {
                    sum += num;
                }

                num = 0;
                valid = false;
            }
        }
    }

    return sum;
}

fn part2(input: String) -> u32 {
    let lines = input.lines().collect::>();
    let mut gears: Vec<(usize, usize, u32)> = Vec::new();
    let mut sum = 0;

    for i in 0..lines.len() {
        let mut num = 0;
        let chars = lines[i].chars().collect::>();
        let mut pos: (usize, usize) = (0, 0);
        let mut valid = false;

        for j in 0..chars.len() {
            let character = chars[j];
            let parts = ['*'];

            if character.is_digit(10) {
                num = num * 10 + character.to_digit(10).unwrap();

                if i > 0 {
                    if parts.contains(&lines[i - 1].chars().collect::>()[j]) {
                        valid = true;
                        pos = (i - 1, j);
                    }

                    if j > 0 {
                        if parts.contains(&lines[i - 1].chars().collect::>()[j - 1]) {
                            valid = true;
                            pos = (i - 1, j - 1);
                        }
                    }

                    if j < chars.len() - 1 {
                        if parts.contains(&lines[i - 1].chars().collect::>()[j + 1]) {
                            valid = true;
                            pos = (i - 1, j + 1);
                        }
                    }
                }

                if i < lines.len() - 1 {
                    if parts.contains(&lines[i + 1].chars().collect::>()[j]) {
                        valid = true;
                        pos = (i + 1, j);
                    }

                    if j > 0 {
                        if parts.contains(&lines[i + 1].chars().collect::>()[j - 1]) {
                            valid = true;
                            pos = (i + 1, j - 1);
                        }
                    }

                    if j < chars.len() - 1 {
                        if parts.contains(&lines[i + 1].chars().collect::>()[j + 1]) {
                            valid = true;
                            pos = (i + 1, j + 1);
                        }
                    }
                }

                if j > 0 {
                    if parts.contains(&lines[i].chars().collect::>()[j - 1]) {
                        valid = true;
                        pos = (i, j - 1);
                    }
                }

                if j < chars.len() - 1 {
                    if parts.contains(&lines[i].chars().collect::>()[j + 1]) {
                        valid = true;
                        pos = (i, j + 1);
                    }
                }
            }
            else {
                if valid == true {
                    let mut current_gear = false;
                    
                    for gear in &gears {
                        if gear.0 == pos.0 && gear.1 == pos.1 {
                            sum += num * gear.2;
                            current_gear = true;
                            break;
                        }
                    }
                    
                    if !current_gear {
                        let tuple_to_push = (pos.0.clone(), pos.1.clone(), num.clone());
                        gears.push((pos.0.clone(), pos.1.clone(), num.clone()));
                    }
                }

                num = 0;
                valid = false;
            }

            if j == chars.len() - 1 {
                if valid == true {
                    let mut current_gear = false;
                    
                    for gear in &gears {
                        if gear.0 == pos.0 && gear.1 == pos.1 {
                            sum += num * gear.2;
                            current_gear = true;
                            break;
                        }
                    }
                    
                    if !current_gear {
                        let tuple_to_push = (pos.0.clone(), pos.1.clone(), num.clone());
                        gears.push((pos.0.clone(), pos.1.clone(), num.clone()));
                    }
                }

                num = 0;
                valid = false;
            }
        }
    }

    return sum;
}

fn main() {
    let input = fs::read_to_string("data/input.txt").unwrap();

    println!("{}", part1(input.clone()));
    println!("{}", part2(input.clone()));
}

Code Link