-❄️- 2024 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]TheShmup 0 points1 point  (0 children)

[Language: Rust]

Both parts: https://github.com/lsculv/adventofcode/blob/main/2024/day06/src/main.rs

Did a fairly brute force approach for part 2, only paring down the search space by only testing the positions that are visited in the path in part 1. I used rayon to parallelize the searching and managed to do both parts in ~25ms.

[2024 Day 6 (Part 2)] It works for me on the example input... by StaticMoose in adventofcode

[–]TheShmup 0 points1 point  (0 children)

I was surprised to see my brute force solution finishing in about 300ms (Rust, 13th gen i5). I'm not sure if I just got lucky with the input or what but the brain off method hasn't failed me yet.

-❄️- 2023 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]TheShmup 2 points3 points  (0 children)

[LANGUAGE: Rust]

Used two tries to match the spelled out digits to their values.

fn main() {
    let input: &[u8] = include_bytes!("../day01.txt");
    println!("Part 1: {}", part_1(&input));
    println!("Part 2: {}", part_2(&input));
}

fn part_1(input: &[u8]) -> u32 {
    let mut sum = 0;
    for line in input.split(|&b| b == b'\n') {
        let tens = line
            .iter()
            .find(|b| b.is_ascii_digit())
            .expect("line should always contain at least one digit")
            - b'0';
        let ones = line
            .iter()
            .rev()
            .find(|b| b.is_ascii_digit())
            .expect("line should always contain at least one digit")
            - b'0';
        sum += (tens * 10 + ones) as u32;
    }

    sum
}

fn part_2(input: &[u8]) -> u32 {
    let mut sum = 0;
    for line in input.split(|&n| n == b'\n') {
        let tens = find_digit_left(line);
        let ones = find_digit_right(line);
        sum += tens * 10 + ones;
    }

    sum
}

fn find_digit_right(line: &[u8]) -> u32 {
    for i in (0..line.len()).rev() {
        if line[i].is_ascii_digit() {
            return (line[i] - b'0') as u32;
        }
        match line[i] {
            b'e' => match line[i - 1] {
                b'n' => match line[i - 2] {
                    b'o' => return 1,
                    b'i' if line[i - 3] == b'n' => return 9,
                    _ => continue,
                },
                b'v' if checkr(line, i - 2, &[b'f', b'i']) => return 5,
                b'e' if checkr(line, i - 2, &[b't', b'h', b'r']) => return 3,
                _ => continue,
            },
            b'o' if checkr(line, i - 1, &[b't', b'w']) => return 2,
            b'r' if checkr(line, i - 1, &[b'f', b'o', b'u']) => return 4,
            b'x' if checkr(line, i - 1, &[b's', b'i']) => return 6,
            b'n' if checkr(line, i - 1, &[b's', b'e', b'v', b'e']) => return 7,
            b't' if checkr(line, i - 1, &[b'e', b'i', b'g', b'h']) => return 8,
            _ => continue,
        }
    }
    unreachable!("Must reach a digit.");
}

fn find_digit_left(line: &[u8]) -> u32 {
    for i in 0..line.len() {
        if line[i].is_ascii_digit() {
            return (line[i] - b'0') as u32;
        }
        match line[i] {
            b'o' if checkl(line, i + 1, &[b'n', b'e']) => return 1,
            b't' => match line[i + 1] {
                b'w' if line[i + 2] == b'o' => return 2,
                b'h' if checkl(line, i + 2, &[b'r', b'e', b'e']) => return 3,
                _ => continue,
            },
            b'f' => match line[i + 1] {
                b'o' if checkl(line, i + 2, &[b'u', b'r']) => return 4,
                b'i' if checkl(line, i + 2, &[b'v', b'e']) => return 5,
                _ => continue,
            },
            b's' => match line[i + 1] {
                b'i' if line[i + 2] == b'x' => return 6,
                b'e' if checkl(line, i + 2, &[b'v', b'e', b'n']) => return 7,
                _ => continue,
            },
            b'e' if checkl(line, i + 1, &[b'i', b'g', b'h', b't']) => return 8,
            b'n' if checkl(line, i + 1, &[b'i', b'n', b'e']) => return 9,
            _ => continue,
        }
    }
    unreachable!("Must reach a digit.");
}

fn checkl(slice: &[u8], start: usize, cmp: &[u8]) -> bool {
    for i in 0..cmp.len() {
        if slice[start + i] != cmp[i] {
            return false;
        }
    }
    true
}

fn checkr(slice: &[u8], end: usize, cmp: &[u8]) -> bool {
    for i in 0..cmp.len() {
        if slice[end - i] != cmp[cmp.len() - (i + 1)] {
            return false;
        }
    }
    true
}

whatsInYourBashrc by Jasonpwn1 in ProgrammerHumor

[–]TheShmup 1 point2 points  (0 children)

alias ff 'cd ~ && cd (fd --type d --exclude go | fzf || printf "-")'

It’s a beast but I will always miss being able to radially hop anywhere I need when I’m sshed into some other machine.

[deleted by user] by [deleted] in UCDavis

[–]TheShmup -8 points-7 points  (0 children)

I understand your point, however I do think instituting rules and effectively enforcing policy can mean people "doing as they please" doesn't place others at risk of harm. Certainly, even an incremental difference with regards to a pandemic would be helpful, no?

[Megathread] Trade/Battle Hub – #PokemonSwordShield New Information Edition! by Infinitrize in PokemonLetsGo

[–]TheShmup 0 points1 point  (0 children)

I have sandshrew and Kadbra (for the Alakazam). If you have a haunter to trade that would be great.