Skip Navigation

Posts
57
Comments
958
Joined
2 yr. ago

  • The US was used to dump British convicts (and other undesirables) first, about 52 thousand of them.

  • doit

    I had to check if do was reserved since I couldn't remember seeing it in the language, and it's in the reserved for future use section.

  • Rust feat. pest

    No Zalgo here! I wasted a huge amount of time by not noticing that the second part's example input was different - my code worked fine but my test failed 🤦‍♂️

    pest.rs is lovely, although part two made my PEG a bit ugly.

     pest
        
    part1    =  { SOI ~ (mul_expr | junk)+ ~ EOI }
    part2    =  { (enabled | disabled)+ ~ EOI }
    mul_expr =  { "mul(" ~ number ~ "," ~ number ~ ")" }
    number   =  { ASCII_DIGIT{1,3} }
    junk     = _{ ASCII }
    on       = _{ "do()" }
    off      = _{ "don't()" }
    enabled  = _{ (SOI | on) ~ (!(off) ~ (mul_expr | junk))+ }
    disabled = _{ off ~ (!(on) ~ junk)+ }
    
      
     rust
        
    use std::fs;
    
    use color_eyre::eyre;
    use pest::Parser;
    use pest_derive::Parser;
    
    #[derive(Parser)]
    #[grammar = "memory.pest"]
    pub struct MemoryParser;
    
    fn parse(input: &str, rule: Rule) -> eyre::Result<usize> {
        let sum = MemoryParser::parse(rule, input)?
            .next()
            .expect("input must be ASCII")
            .into_inner()
            .filter(|pair| pair.as_rule() == Rule::mul_expr)
            .map(|pair| {
                pair.into_inner()
                    .map(|num| num.as_str().parse::<usize>().unwrap())
                    .product::<usize>()
            })
            .sum();
        Ok(sum)
    }
    
    fn part1(filepath: &str) -> eyre::Result<usize> {
        let input = fs::read_to_string(filepath)?;
        parse(&input, Rule::part1)
    }
    
    fn part2(filepath: &str) -> eyre::Result<usize> {
        let input = fs::read_to_string(filepath)?;
        parse(&input, Rule::part2)
    }
    
    fn main() -> eyre::Result<()> {
        color_eyre::install()?;
    
        let part1 = part1("d03/input.txt")?;
        let part2 = part2("d03/input.txt")?;
        println!("Part 1: {part1}\nPart 2: {part2}");
        Ok(())
    }
    
      
  • I think that repo is private

  • is_sorted_by is new to me, could be very useful.

  • I forgot that this started yesterday, so I'm already behind. I quite like my solution for part one, but part two will have to wait edit: part 2 was a lot simpler than I thought after a night's sleep.

    Rust

     rust
        
    use color_eyre::eyre;
    use std::{fs, num, str::FromStr};
    
    #[derive(Debug, PartialEq, Eq)]
    struct Report(Vec<isize>);
    
    impl FromStr for Report {
        type Err = num::ParseIntError;
    
        fn from_str(s: &str) -> Result<Self, Self::Err> {
            let v: Result<Vec<isize>, _> = s
                .split_whitespace()
                .map(|num| num.parse::<isize>())
                .collect();
            Ok(Report(v?))
        }
    }
    
    impl Report {
        fn is_safe(&self) -> bool {
            let ascending = self.0[1] > self.0[0];
            let (low, high) = if ascending { (1, 3) } else { (-3, -1) };
            self.0.windows(2).all(|w| {
                let a = w[0];
                let b = w[1];
                b >= a + low && b <= a + high
            })
        }
    
        fn is_dampsafe(&self) -> bool {
            if self.is_safe() {
                return true;
            }
            for i in 0..self.0.len() {
                let damped = {
                    let mut v = self.0.clone();
                    v.remove(i);
                    Self(v)
                };
                if damped.is_safe() {
                    return true;
                }
            }
            false
        }
    }
    
    fn main() -> eyre::Result<()> {
        color_eyre::install()?;
    
        let part1 = part1("d02/input.txt")?;
        let part2 = part2("d02/input.txt")?;
        println!("Part 1: {part1}\nPart 2: {part2}");
        Ok(())
    }
    
    fn part1(filepath: &str) -> eyre::Result<isize> {
        let mut num_safe = 0;
        for l in fs::read_to_string(filepath)?.lines() {
            if Report::from_str(l)?.is_safe() {
                num_safe += 1;
            }
        }
        Ok(num_safe)
    }
    
    fn part2(filepath: &str) -> eyre::Result<isize> {
        let mut num_safe = 0;
        for l in fs::read_to_string(filepath)?.lines() {
            if Report::from_str(l)?.is_dampsafe() {
                num_safe += 1;
            }
        }
        Ok(num_safe)
    }
    
      
  • I assume Peta just wants to be in the news? The Sly Old Fox is not an offensive name.

  • That's the first argument they make in their petition.

  • Quality post, with plenty of useful and good advice.

    I'm happy to see sqlite being recommended - I think people don't realise how resilient and performant a database it is.

    Very surprising to hear you need to restart GoToSocial to add new users!

  • Probably start with Rust again this year, although it definitely makes some of the days a lot harder. I might switch to something better for quick code if I fall too far behind.

    I might even try PHP - I coded it professionally at the start of my career but haven't touched it for a decade and I'm curious to know if its improvements make it pleasant to use.

  • Not funny, but interesting!

  • Their spelling was moulded by the US

  • Palikúr

    I'd never heard of this language - Palikúr is an indigenous South American language spoken in Brazil and French Guiana (only 1500 speakers).

  • Andy still won the series (hurrah), so the hotdog failure is just funny - it's not like all the other hotdogs worked out either!

  • He breaks his miserable front quite a few times, my favourite being when the robot shows where to plug in the charging cable.

  • I've been coding long enough that I still think of that as a fairly new thing in JS.

  • I assumed it was vandalism, not insurance fraud. I guess it's easier to get away with slashing tyres looking like a human than as a dubious bear.

  • I thought we were finally agreeing fully! My understanding of the question is "what is the difference between a third (of a pizza, say) and a half?"

    1/2 - 1/3 = 1/6
    1/2 = 1/3 + 1/6
    a half is one sixth more than a third.

    btw, I fixed my Kagi screenshot since I'd missed a word from the question (reading comprehension's clearly not my strong point today)