[-] Deebster@programming.dev 3 points 5 months ago

Sounds amazing, I'll have to keep an eye out for rhubarb amaro. Growing up, we had rhubarb growing in the garden, but I almost never taste it nowadays.

[-] Deebster@programming.dev 4 points 9 months ago

Another day where the dumb way would have so much quicker and easier, but I'm not competing for time.

I decided to solve it numerically without regex or using to_string(), which was more taxing for the ol' grey matter but is perhaps fairly optimal (if I bothered to pre-compute all those pow() calls, anyway).

Part 2 runs in 35ms (on my AMD Ryzen 7 9800X3D), whereas the to_string() version runs in 40ms. So... not really worth it, and it's less readable.

Rust

use std::fs;

use color_eyre::eyre::{Result, bail};

type InvalidChecker = fn(usize) -> bool;

fn sum_invalids(input: &str, checkfn: InvalidChecker) -> Result<usize> {
    let total = input
        .trim()
        .split(',')
        .map(|idrange| {
            if let Some((start, end)) = idrange.split_once('-') {
                let mut sum = 0;
                for n in start.parse::<usize>()?..=end.parse::<usize>()? {
                    if checkfn(n) {
                        sum += n;
                    }
                }
                Ok(sum)
            } else {
                bail!("Couldn't parse {idrange}")
            }
        })
        .sum::<Result<usize, _>>()?;
    Ok(total)
}

fn is_invalid_p1(n: usize) -> bool {
    let len = n.ilog10() + 1;
    // odd-length numbers can't repeat
    if len % 2 == 1 {
        return false;
    }

    let lhs = n / 10_usize.pow(len / 2);
    let rhs = n - (lhs * 10_usize.pow(len / 2));
    lhs == rhs
}

const SPANS: &[&[u32]] = &[
    &[],              // i = 0
    &[],              // i = 1
    &[1],             // i = 2
    &[1],             // i = 3
    &[1, 2],          // i = 4
    &[1],             // i = 5
    &[1, 2, 3],       // i = 6
    &[1],             // i = 7
    &[1, 2, 4],       // i = 8
    &[1, 3],          // i = 9
    &[1, 2, 5],       // i = 10
    &[1],             // i = 11
    &[1, 2, 3, 4, 6], // i = 12
];

fn is_invalid_p2(n: usize) -> bool {
    let len = n.ilog10() + 1;
    // 1-length numbers can't repeat
    if len == 1 {
        return false;
    }

    SPANS[len as usize].iter().any(|&span| {
        let lhs = n / 10_usize.pow(len - span);
        let mut remainder = n;
        let mut rhs = lhs;
        (2..=(len / span)).all(|i| {
            remainder -= rhs * 10_usize.pow(len - (i - 1) * span);
            rhs = remainder / 10_usize.pow(len - i * span);
            lhs == rhs
        })
    })
}

fn part1(filepath: &str) -> Result<usize> {
    let input = fs::read_to_string(filepath)?;
    let res = sum_invalids(&input, is_invalid_p1)?;
    Ok(res)
}

fn part2(filepath: &str) -> Result<usize> {
    let input = fs::read_to_string(filepath)?;
    let res = sum_invalids(&input, is_invalid_p2)?;
    Ok(res)
}

to_string version:

fn is_invalid_p2(n: usize) -> bool {
    let s = n.to_string();
    let len = s.len();
    // 1-length numbers can't repeat
    if len == 1 {
        return false;
    }

    SPANS[len].iter().any(|&span| {
        let span = span as usize;
        let lhs = &s[0..span].as_bytes();
        s.as_bytes().chunks(span).all(|rhs| *lhs == rhs)
    })
}
[-] Deebster@programming.dev 4 points 11 months ago

I solved it in 1️⃣8️⃣ moves!
⭐ ⭐ ⭐ 🦊 🦔 🎉

@TheTurner@lemmy.zip the permanent link for this puzzle is https://www.merriam-webster.com/games/reunion/47 (you can find it from the archive tab).

[-] Deebster@programming.dev 3 points 1 year ago

btw, you've typoed the name: altwiki makes me think it's an alt-right version of Wikipedia.

[-] Deebster@programming.dev 4 points 2 years ago

I guess it is, otherwise perhaps you could call it a collage... a found object collage?

[-] Deebster@programming.dev 3 points 2 years ago

Huh, how sensible and well considered.

[-] Deebster@programming.dev 4 points 2 years ago

I know they purposely left off some heat tiles to test that eventuality - was the flap where they did that? I guess so, given the number of cameras pointing at it.

[-] Deebster@programming.dev 3 points 2 years ago

Also mixing crops (or non-farmable plants) has big benefits, but it's currently cheaper to use chemically-derived fertilisers and go the monoculture route.

[-] Deebster@programming.dev 4 points 2 years ago

This is a bigger Rodney, although the franchise compensator will have to work even harder:

Photo of Rodney Trotter from Only Fools and Horses

[-] Deebster@programming.dev 4 points 2 years ago

Huh, I'd read both Old Man's War and Red Shirts without noticing they were by the same author.

[-] Deebster@programming.dev 4 points 2 years ago

This is very good advice. I've made a couple - far from beautiful or quiet, but very effective and very cheap.

This is a good page from an early DIY promoter: How to Make a DIY Air Purifier

[-] Deebster@programming.dev 4 points 2 years ago

You're being unnecessarily pedantic. Apple was blocking interoperability and reverse engineering found a solution—and Apple is blocking that solution.

view more: ‹ prev next ›

Deebster

0 post score
0 comment score
joined 2 years ago