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

It's how everyone who's anyone does code reviews!

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

How have I never noticed that scan() exists? Very handy.

I liked the zipping of the offset prices, neater than my helper method.

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

Rust

Not too hard today, apart from yesterday's visit to a cocktail bar leaving me a little hazy in the mind.

code

use std::{fs, str::FromStr};

use color_eyre::eyre::{Report, Result};
use gxhash::{HashMap, HashMapExt};

const SECRETS_PER_DAY: usize = 2000;
const SEQ_LEN: usize = 4;

type Sequence = [i8; SEQ_LEN];

fn produce(n: usize) -> usize {
    let n = (n ^ (n * 64)) % 16777216;
    let n = (n ^ (n / 32)) % 16777216;
    (n ^ (n * 2048)) % 16777216
}

#[derive(Debug)]
struct Buyer {
    prices: [u8; SECRETS_PER_DAY + 1],
    changes: [i8; SECRETS_PER_DAY],
}

impl Buyer {
    fn price_at_seq(&self, seq: &Sequence) -> Option<u8> {
        self.changes
            .windows(SEQ_LEN)
            .position(|win| win == *seq)
            .and_then(|i| self.price_for_window(i))
    }

    fn price_for_window(&self, i: usize) -> Option<u8> {
        self.prices.get(i + SEQ_LEN).copied()
    }
}

struct BananaMarket {
    buyers: Vec<Buyer>,
}

impl FromStr for BananaMarket {
    type Err = Report;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let buyer_seeds = s
            .lines()
            .map(|s| s.parse::<usize>())
            .collect::<Result<Vec<_>, _>>()?;

        let buyers = buyer_seeds
            .into_iter()
            .map(|seed| {
                let mut prices = [0; SECRETS_PER_DAY + 1];
                let mut changes = [0; SECRETS_PER_DAY];

                let mut secret = seed;
                let mut price = (seed % 10) as u8;
                prices[0] = price;
                for i in 0..SECRETS_PER_DAY {
                    let last_price = price;
                    secret = produce(secret);
                    price = (secret % 10) as u8;
                    prices[i + 1] = price;
                    changes[i] = price as i8 - last_price as i8;
                }
                Buyer { prices, changes }
            })
            .collect();
        Ok(Self { buyers })
    }
}

impl BananaMarket {
    fn sell_with_seq(&self, seq: &Sequence) -> usize {
        self.buyers
            .iter()
            .map(|b| b.price_at_seq(seq).unwrap_or(0) as usize)
            .sum()
    }

    fn maximise_bananas(&self) -> usize {
        let mut cache: HashMap<Sequence, usize> = HashMap::new();

        for seq in self
            .buyers
            .iter()
            .flat_map(|buyer| buyer.changes.windows(SEQ_LEN))
        {
            let seq = seq.try_into().unwrap();
            cache.entry(seq).or_insert_with(|| self.sell_with_seq(&seq));
        }

        cache.into_values().max().unwrap_or(0)
    }
}

fn part1(filepath: &str) -> Result<usize> {
    let input = fs::read_to_string(filepath)?
        .lines()
        .map(|s| s.parse::<usize>())
        .collect::<Result<Vec<_>, _>>()?;
    let res = input
        .into_iter()
        .map(|n| (0..SECRETS_PER_DAY).fold(n, |acc, _| produce(acc)))
        .sum();
    Ok(res)
}

fn part2(filepath: &str) -> Result<usize> {
    let input = fs::read_to_string(filepath)?;
    let market = BananaMarket::from_str(&input)?;
    Ok(market.maximise_bananas())
}

fn main() -> Result<()> {
    color_eyre::install()?;

    println!("Part 1: {}", part1("d22/input.txt")?);
    println!("Part 2: {}", part2("d22/input.txt")?);
    Ok(())
}

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

Vulnerable: VHD PTZ camera firmware < 6.3.40 used in PTZOptics, Multicam Systems SAS, and SMTAV Corporation devices based on Hisilicon Hi3516A V600 SoC V60, V61, and V63

It looks like they're using AI correctly: to identify patterns in huge amounts of data.

I think they'd struggle to mention their own name more often in that article.

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

I think there's a lot of people who would be happy with a Chromebook in computer form, and those are also the market for Linux.

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

btw, it's a rite of passage.

[-] Deebster@programming.dev 3 points 2 years ago* (last edited 2 years ago)

Ah, so this is the most popular cocktails on punchdrink.com - I thought the title was saying party punch was the most popular!

Edit: I think it's top-rated, not most searched:

these are the recipes you deemed best this month

They're all more obscure than what I'd expect to see on a populist list.

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

That blog's such a treasure! I've ended up on there several times over the years for completely disparate reasons like solving Countdown numbers games, etymology and explaining mathematical paradoxes.

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

You have other options. You can use the separate search box. You can use smart keywords to only trigger searches when you want.

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

The fact that it's Nintendo's IP seems the key thing here.

So did Nintendo get Valve to do this, or is Valve just covering its back from the notoriously-litigious Nintendo?

[-] Deebster@programming.dev 3 points 2 years ago* (last edited 2 years ago)

I'd never even heard of this, but it's even got its own website: https://www.thebalticway.eu/en/history/

Or, here's an AI summary of the Wikipedia article:

The Baltic Way was a peaceful protest that took place on August 23, 1989 involving about 2 million people forming a human chain spanning 690 km across Estonia, Latvia and Lithuania. It marked the 50th anniversary of the secret Molotov-Ribbentrop Pact between the Soviet Union and Nazi Germany dividing Eastern Europe into spheres of influence. The protest highlighted growing independence movements in the Baltic states and solidarity against Soviet occupation. It represented one of the earliest and longest unbroken human chains in history. Notably, Lithuania became the first Soviet republic to declare independence just 7 months after this demonstration.

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

That's not great for user privacy - having a database of people's kinks up for sale is not a headline that will help the Lemmy project. Hopefully everyone's sensible enough to have anonymous alt accounts for their embarrassing Lemmy usage.

view more: โ€น prev next โ€บ

Deebster

0 post score
0 comment score
joined 3 years ago