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

The docs say what they do and don't do - and they don't do that. Just actually read through them for yourself, you don't have to be a lawyer.

This is just a bit of corporate box-ticking, but the pitchfork brigade has read 2 + 2 and is now screaming about 5s.

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

Six minutes? πŸ˜… I was feeling crappy about my 30 seconds (my naive big O cubed(?) logic means my code spends most of its time testing array equalities - 72 billion samples in the flamegraph!)

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

I missed that line too:

Because there is only a single path from the start to the end

So I also did my pathfinding for every variation in the first part, but realised something must be wrong with my approach when I saw part 2.

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

That full version is extremely satisfying. I think it'd be even better if there were sound - maybe ascending notes in a scale like in some puzzle games.

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

Nushell

As I'm still on holiday and normal languages are a PITA to type on a phone, I though I'd try a compiled scripting language. I'm not very familiar with it so it took longer to code and also it's been running the first reduce step for 35 minutes so I've missed the 24h cutoff πŸ˜”

use std repeat
use std/iter

let memory = open input.txt | str trim 
  | split chars | chunks 2
  | enumerate | each {|pair| [
    ...($pair.index | repeat ($pair.item.0 | into int))
    ...("." | repeat (if ($pair.item|length) < 2 {0} else {$pair.item.1 | into int}))
  ]}
  | flatten

let defragged = (($memory | length) - 1)..(($memory | filter {$in != .} | length))
 | reduce --fold $memory {|i, acc| 
    let space = $acc | iter find-index {|$i| $i == .}
    $acc | update $space ($acc | get $i)
      | update $i .
  }

$defragged | enumerate
  | reduce --fold 0 {|i,acc| $acc + ($i.index * if $i.item == "." {0} else {$i.item | into int})}
[-] Deebster@programming.dev 2 points 2 years ago

I think that repo is private

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

is_sorted_by is new to me, could be very useful.

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

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

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

Tests

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sample_part1() {
        assert_eq!(part1("test.txt").unwrap(), 2);
    }

    #[test]
    fn sample_part2() {
        assert_eq!(part2("test.txt").unwrap(), 4);
    }
}

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

Self-hosting email is not at all easy, and I'd recommend paying for hosted email from a service that lets you use a custom domain. Most will let you have multiple inboxes, although this may cost extra.

Then, just buy a domain (NameCheap is fine) and point your MX records at the email provider.

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

I started counting, but my 10-bit counter maxed out. My answer was quite close to Google's though πŸ‘

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

I hate that they've vanished like this, it just means that we'll end up with a few massive all-purpose instances instead of the ideal topic-based instances that the Fediverse wants.

view more: β€Ή prev next β€Ί

Deebster

0 post score
0 comment score
joined 3 years ago