hades

joined 5 months ago
MODERATOR OF
[–] hades@programming.dev 3 points 1 day ago

Rust

use itertools::Itertools;

type Fishbone = Vec<(i64, Option<i64>, Option<i64>)>;

fn parse_fishbone(quality_str: &str) -> Fishbone {
    let mut fishbone: Fishbone = vec![];
    'outer: for num in quality_str.split(",").map(|x| x.parse().unwrap()) {
        for e in fishbone.iter_mut() {
            if num < e.0 && e.1.is_none() {
                e.1 = Some(num);
                continue 'outer;
            }
            if num > e.0 && e.2.is_none() {
                e.2 = Some(num);
                continue 'outer;
            }
        }
        fishbone.push((num, None, None));
    }
    fishbone
}

fn compute_quality(fishbone: &Fishbone) -> i64 {
    fishbone
        .iter()
        .map(|(c, _, _)| c.to_string())
        .join("")
        .parse()
        .unwrap()
}

pub fn solve_part_1(input: &str) -> String {
    let (_, data) = input.split_once(":").unwrap();
    compute_quality(&parse_fishbone(data)).to_string()
}

pub fn solve_part_2(input: &str) -> String {
    let mut worst_quality = i64::MAX;
    let mut best_quality = i64::MIN;
    for sword in input.lines() {
        let (_, data) = sword.split_once(":").unwrap();
        let quality = compute_quality(&parse_fishbone(data));
        worst_quality = worst_quality.min(quality);
        best_quality = best_quality.max(quality);
    }
    (best_quality - worst_quality).to_string()
}

pub fn solve_part_3(input: &str) -> String {
    let mut swords: Vec<_> = input
        .lines()
        .map(|def| {
            let (id, data) = def.split_once(":").unwrap();
            let fishbone = parse_fishbone(data);
            (id.parse::<i64>().unwrap(), fishbone)
        })
        .collect();
    swords.sort_by(|a, b| {
        let cmp = compute_quality(&a.1).cmp(&compute_quality(&b.1));
        if !matches!(cmp, std::cmp::Ordering::Equal) {
            return cmp;
        }
        for (a_seg, b_seg) in a.1.iter().zip(b.1.iter()) {
            let a_val = match a_seg {
                (a, Some(b), Some(c)) => format!("{b}{a}{c}"),
                (a, Some(b), None) => format!("{b}{a}"),
                (a, None, Some(c)) => format!("{a}{c}"),
                (a, None, None) => format!("{a}"),
            };
            let b_val = match b_seg {
                (a, Some(b), Some(c)) => format!("{b}{a}{c}"),
                (a, Some(b), None) => format!("{b}{a}"),
                (a, None, Some(c)) => format!("{a}{c}"),
                (a, None, None) => format!("{a}"),
            };
            let cmp = a_val.parse::<i64>().unwrap().cmp(&b_val.parse().unwrap());
            if !matches!(cmp, std::cmp::Ordering::Equal) {
                return cmp;
            }
        }
        a.0.cmp(&b.0)
    });
    swords.reverse();
    swords
        .into_iter()
        .enumerate()
        .map(|(pos, (id, _))| id * (pos as i64 + 1))
        .sum::<i64>()
        .to_string()
}
[–] hades@programming.dev 2 points 1 day ago

Rust

use num::{BigInt, Integer};

pub fn solve_part_1(input: &str) -> String {
    let gears: Vec<i64> = input.trim().lines().map(|g| g.parse().unwrap()).collect();
    (2025 * gears[0] / gears.last().unwrap()).to_string()
}

pub fn solve_part_2(input: &str) -> String {
    let gears: Vec<i64> = input.trim().lines().map(|g| g.parse().unwrap()).collect();
    let res = (BigInt::parse_bytes(b"10000000000000", 10).unwrap() * gears.last().unwrap())
        .div_ceil(&(BigInt::ZERO + gears[0]));
    res.to_string()
}

pub fn solve_part_3(input: &str) -> String {
    let mut lines = input.trim().lines();
    let first_gear = BigInt::parse_bytes(lines.next().unwrap().as_bytes(), 10).unwrap();
    let mut nominator: BigInt = first_gear * 100;
    let mut denominator: BigInt = BigInt::ZERO + 1;
    for line in lines {
        let mut split = line.split("|");
        denominator *= BigInt::parse_bytes(split.next().unwrap().as_bytes(), 10).unwrap();
        match split.next() {
            Some(size) => {
                nominator *= BigInt::parse_bytes(size.as_bytes(), 10).unwrap();
            }
            None => {
                break;
            }
        }
    }
    (nominator / denominator).to_string()
}
[–] hades@programming.dev 2 points 1 day ago

Rust

pub fn solve_part_1(input: &str) -> String {
    let mut crates: Vec<i64> = input.split(",").map(|s| s.parse().unwrap()).collect();
    crates.sort();
    let mut monotonic_subsequence = vec![crates[0]];
    for size in crates.into_iter().skip(1) {
        if size == *monotonic_subsequence.last().unwrap() {
            continue;
        }
        monotonic_subsequence.push(size);
    }
    monotonic_subsequence.iter().sum::<i64>().to_string()
}

pub fn solve_part_2(input: &str) -> String {
    let mut crates: Vec<i64> = input.split(",").map(|s| s.parse().unwrap()).collect();
    crates.sort();
    let mut monotonic_subsequence = vec![crates[0]];
    for size in crates.into_iter().skip(1) {
        if size == *monotonic_subsequence.last().unwrap() {
            continue;
        }
        monotonic_subsequence.push(size);
        if monotonic_subsequence.len() >= 20 {
            break;
        }
    }
    monotonic_subsequence.iter().sum::<i64>().to_string()
}

pub fn solve_part_3(input: &str) -> String {
    let mut crates: Vec<i64> = input.split(",").map(|s| s.parse().unwrap()).collect();
    crates.sort();
    let mut monotonic_subsequences = vec![vec![crates[0]]];
    for size in crates.into_iter().skip(1) {
        let updateable_sequence = monotonic_subsequences
            .iter_mut()
            .find(|v| *v.last().unwrap() < size);
        match updateable_sequence {
            Some(v) => {
                v.push(size);
            }
            None => {
                monotonic_subsequences.push(vec![size]);
            }
        }
    }
    monotonic_subsequences.len().to_string()
}
 

Quest 5: Fishbone Order

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

 

Quest 4: Teeth of the Wind

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

 

Quest 3: The Deepest Fit

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

Also, don't wait for me to make these posts, feel free to post yourself :)

[–] hades@programming.dev 3 points 5 days ago

Rust

use log::debug;
use std::collections::HashSet;

use regex::Regex;

#[derive(PartialEq, Eq, Hash, Clone)]
struct Number(isize, isize);

impl Number {

  fn add(self: &Number, b: &Number) -> Number {
    Number(self.0 + b.0, self.1 + b.1)
  }

  fn mul(self: &Number, b: &Number) -> Number {
    Number(self.0 * b.0 - self.1 * b.1, self.0 * b.1 + self.1 * b.0)
  }

  fn div(self: &Number, b: &Number) -> Number {
    Number(self.0 / b.0, self.1 / b.1)
  }
}

pub fn solve_part_1(input: &str) -> String {
  let re = Regex::new(r"A=\[(\d+),(\d+)\]").unwrap();
  let (_, [x, y]) = re.captures(input).unwrap().extract();
  let a = Number(x.parse().unwrap(), y.parse().unwrap());
  let mut res = Number(0, 0);
  for _ in 0..3 {
    res = res.mul(&res);
    res = res.div(&Number(10, 10));
    res = res.add(&a);
  }
  format!("[{},{}]", res.0, res.1)
}

pub fn solve_part_2(input: &str) -> String {
  let re = Regex::new(r"A=\[([-0-9]+),([-0-9]+)\]").unwrap();
  let (_, [x, y]) = re.captures(input).unwrap().extract();
  let a = Number(x.parse().unwrap(), y.parse().unwrap());
  let mut engraved_points = 0;
  let mut pts: HashSet<_> = HashSet::new();
  for i in 0..=100 {
    for j in 0..=100 {
      let pt = Number(a.0 + 10 * i, a.1 + 10 * j);
      let mut res = Number(0, 0);
      engraved_points += 1;
      pts.insert(pt.clone());
      for _ in 0..100 {
        res = res.mul(&res);
        res = res.div(&Number(100_000, 100_000));
        res = res.add(&pt);
        if res.0.abs() > 1_000_000 || res.1.abs() > 1_000_000 {
          engraved_points -= 1;
          pts.remove(&pt);
          break;
        }
      }
    }
  }
  for i in 0..=100 {
    debug!("{}", (0..=100).map(|j| if pts.contains(&Number(a.0 + 10*i, a.1 + 10*j)) { 'X' } else {'.'}).collect::<String>());
  }
  engraved_points.to_string()
}

pub fn solve_part_3(input: &str) -> String {
  let re = Regex::new(r"A=\[([-0-9]+),([-0-9]+)\]").unwrap();
  let (_, [x, y]) = re.captures(input).unwrap().extract();
  let a = Number(x.parse().unwrap(), y.parse().unwrap());
  let mut engraved_points = 0;
  for i in 0..=1000 {
    for j in 0..=1000 {
      let pt = Number(a.0 + i, a.1 + j);
      let mut res = Number(0, 0);
      engraved_points += 1;
      for _ in 0..100 {
        res = res.mul(&res);
        res = res.div(&Number(100_000, 100_000));
        res = res.add(&pt);
        if res.0.abs() > 1_000_000 || res.1.abs() > 1_000_000 {
          engraved_points -= 1;
          break;
        }
      }
    }
  }
  engraved_points.to_string()
}
 

Quest 2: From Complex to Clarity

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

 

Quest 1: Whispers in the Shell

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

 

I was solving old Advent of Code the other day, and someone suggested to me another seasonal coding challenge website: https://everybody.codes/home

As there are many people here who are actively solving AoC, I'm sure you'd be interested in that too. However, I'm not sure which community we should be using to discuss these? I'm seeing three options:

  • use this community for everybody.codes specifically (as it has largely the same format as AoC, and probably a lot of AoC solvers would be interested in that too),
  • create a dedicated community for everybody.codes (I can do that using this community as a blueprint),
  • use !challenges (the format doesn't really match, but in spirit it kinda makes sense).

WDYT?

[–] hades@programming.dev 10 points 1 month ago

but how many COMEFROMs are there

[–] hades@programming.dev 2 points 2 months ago

imagine the diffs

[–] hades@programming.dev 1 points 3 months ago

Uninitialized automatic variables. E.g. (in C/C++):

int get_time() {
  int time;
  syscall(/* something that fails */, &time);
  return time;
}
[–] hades@programming.dev 2 points 3 months ago (2 children)

probably the worst option for a library

Even worse than returning garbage? :)

[–] hades@programming.dev 4 points 3 months ago

Interestingly, Rust is what brought me to this rabbit hole. It does indeed panic in now()[1], but the devs seem to be reluctant to provide the try_now() variant[2].

[1] https://doc.rust-lang.org/nightly/src/std/sys/pal/unix/time.rs.html#124 [2] https://github.com/rust-lang/rust/issues/115482

 

If you were designing a standard library of a high level language (like Rust, C++, etc.) what would it do if while getting the current time of day (e.g. SystemTime::now()) it encountered a failed kernel system call (e.g. to clock_gettime) and why?

What do you think the existing implementations do?

  • Return error or raise exception
  • Return 0
  • Return undefined values (like stack garbage)
  • Panic/crash/segfault