Deebster

New account since lemmyrs.org went down, other @Deebsters are available.

  • 54 Posts
  • 924 Comments
Joined 1 year ago
cake
Cake day: October 16th, 2023

help-circle







  • 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(())
    }
    






  • I feel this article is looking at a bit of silver as if it’s the lining and writing an article about the cloud.

    Definitely there’s less knowledge about how computers work nowadays: e.g. universities now have to teach Computer Science students how file systems and directories work, because kids are used to saving everything in the default folder and just searching for what they want (and we’re talking to those geeky enough to be doing CS).

    But there’s more complexity now, and a lot of that stuff is just irrelevant busy work. I remember juggling IRQ lines when installing hardware but luckily that’s auto-configured now. I used to create boot discs and eke out enough memory by skipping the better graphics, mouse driver, etc when not needed but did that teach me much?

    On the other hand, we used to have LAN parties as teenagers and would rebuild the networking stack for each game, including some performance tweaks (“Red Alert needs NetBIOS over IPX, right?” “Yeah, but disable the turbo encabulator”) and ¾ of us went on to do CS at uni. We certainly never thought of a web browser as “the internet” and we’re confident to explore knowing we could fix what we broke (and did, often).



  • I think just announce it’s a thing and let people post as they will. I don’t think a daily thread is necessary, for the reason you say but also that individual posts would get more attention - and extra attention is warranted since there’s a lot of extra work that goes in to them.