this post was submitted on 03 Dec 2024
15 points (100.0% liked)

programming

258 readers
15 users here now

  1. Post about programming, interesting repos, learning to program, etc. Let's try to keep free software posts in the c/libre comm unless the post is about the programming/is to the repo.

  2. Do not doxx yourself by posting a repo that is yours and in any way leads to your personally identifying information. Use reports if necessary to alert mods to a potential doxxing.

  3. Be kind, keep struggle sessions focused on the topic of programming.

founded 2 years ago
MODERATORS
15
Advent of Code 2024 Day 3 (adventofcode.com)
submitted 7 months ago* (last edited 7 months ago) by zongor@hexbear.net to c/programming@hexbear.net
 

Time to break out the hex editors and fix some corrupted memory.

Also fun with regex I expect. Or if you go full throttle and make a lexer/parser out of it.

@Enjoyer_of_Games@hexbear.net @Speaker@hexbear.net @gay_king_prince_charles@hexbear.net @iByteABit@hexbear.net

top 5 comments
sorted by: hot top controversial new old
[–] iByteABit@hexbear.net 2 points 7 months ago (1 children)

Can you add me to the tags?

programming-communism

[–] zongor@hexbear.net 2 points 7 months ago

Je suis struggling kitty-cri-potato (in rust catgirl-happy )

[–] gay_king_prince_charles@hexbear.net 2 points 7 months ago (1 children)

Can I get some rigorous critique? I have a feeling that I did this very poorly, and I would like to turn this into a learning experience.

abandon all hope ye who enter

use std::{char, fs, vec};

pub fn day3() {
    let mut ret = 0;
    for i in extract() {
        ret += i.0 * i.1;
    }
    println!("{}", ret);
}

fn extract() -> Vec<(i32, i32)> {
    let text = fs::read_to_string("input").unwrap();
    let mut ret: Vec<(i32, i32)> = vec::Vec::new();
    let text_vec = text.chars().collect::<Vec<char>>();
    let mut in_mul = false;
    let mut lhs = true;
    let mut collector: (Vec<char>, Vec<char>) = (Vec::new(), Vec::new());
    for (i, v) in text.chars().enumerate() {
        println!("---START({},{})---", i, v);
        if i < 3 {
            //Jump to line 3 as loop peeks backwards
            continue;
        }
        if v == '(' && text_vec[i - 1] == 'l' && text_vec[i - 2] == 'u' && text_vec[i - 3] == 'm' {
            // If the code is here, the last 3 chars spell 'mul'
            println!("Last 4 spelled mul(");
            in_mul = true;
            continue;
        } else if in_mul {
            // You are inside of a mul(... block
            // volcel police have been alerted to your position
            if lhs && v.is_numeric() {
                println!("pushed left: {}", v);
                collector.0.push(v);
                continue;
            } else if !lhs && v.is_numeric() {
                println!("pushed right: {}", v);
                collector.1.push(v);
                continue;
            } else if v == ',' {
                println!("comma");
                lhs = false; // Swap to the right because you are on the other side of the comma
            } else if v == ')' && in_mul {
                println!("pushing... {}", in_mul);
                lhs = true;
                in_mul = false;

                // Collect chars into ints and then add to vec
                ret.push((
                    (collector
                        .0
                        .iter()
                        .cloned()
                        .collect::<String>()
                        .parse::<i32>()
                        .unwrap()),
                    (collector
                        .1
                        .iter()
                        .cloned()
                        .collect::<String>()
                        .parse::<i32>()
                        .unwrap()),
                ));
                collector.1.clear();
                collector.0.clear();
                println!("current final vec: {:?}", ret);
            } else {
                println!("cleared");
                collector.1.clear();
                collector.0.clear();
                lhs = true;
                in_mul = false;
            }
        } else {
        }

        println!("---END({},{})---", i, v);
    }
    return ret;
}

[–] VOLCEL_POLICE@hexbear.net 1 points 7 months ago

The VOLCEL POLICE are on the scene! PLEASE KEEP YOUR VITAL ESSENCES TO YOURSELVES AT ALL TIMES.

نحن شرطة VolCel.بناءا على تعليمات الهيئة لترويج لألعاب الفيديو و النهي عن الجنس نرجوا الإبتعاد عن أي أفكار جنسية و الحفاظ على حيواناتكم المنويَّة حتى يوم الحساب. اتقوا الله، إنك لا تراه لكنه يراك.

volcel-police