Gobbel2000

joined 1 year ago
[โ€“] Gobbel2000@programming.dev 4 points 8 months ago (1 children)

Rust

The function is_sorted_by on Iterators turned out helpful for compactly finding if a report is safe. In part 2 I simply tried the same with each element removed, since all reports are very short.

fn parse(input: String) -> Vec<Vec<i32>> {
    input.lines()
        .map(|l| l.split_whitespace().map(|w| w.parse().unwrap()).collect())
        .collect()
}

fn is_safe(report: impl DoubleEndedIterator<Item=i32> + Clone) -> bool {
    let safety = |a: &i32, b: &i32| (1..=3).contains(&(b - a));
    report.clone().is_sorted_by(safety) || report.rev().is_sorted_by(safety)
}

fn part1(input: String) {
    let reports = parse(input);
    let safe = reports.iter().filter(|r| is_safe(r.iter().copied())).count();
    println!("{safe}");
}

fn is_safe2(report: &[i32]) -> bool {
    (0..report.len()).any(|i| {  // Try with each element removed
        is_safe(report.iter().enumerate().filter(|(j, _)| *j != i).map(|(_, n)| *n))
    })
}

fn part2(input: String) {
    let reports = parse(input);
    let safe = reports.iter().filter(|r| is_safe2(r)).count();
    println!("{safe}");
}

util::aoc_main!();
[โ€“] Gobbel2000@programming.dev 38 points 8 months ago (3 children)

The ruling party, recently reelected in a fraudulent election has declared that EU accession talks will be stopped until 2028. This has stoked ongoing protests over election results.

[โ€“] Gobbel2000@programming.dev 4 points 8 months ago

Rust

Right IDs are directly read into a hash map counter.

use std::str::FromStr;
use std::collections::HashMap;

fn part1(input: String) {
    let mut left = Vec::new();
    let mut right = Vec::new();
    for line in input.lines() {
        let mut parts = line.split_whitespace()
            .map(|p| u32::from_str(p).unwrap());
        left.push(parts.next().unwrap());
        right.push(parts.next().unwrap());
    }
    left.sort_unstable();
    right.sort_unstable();
    let diff: u32 = left.iter().zip(right)
        .map(|(l, r)| l.abs_diff(r))
        .sum();
    println!("{diff}");
}

fn part2(input: String) {
    let mut left = Vec::new();
    let mut right: HashMap<u32, u32> = HashMap::new();
    for line in input.lines() {
        let mut parts = line.split_whitespace()
            .map(|p| u32::from_str(p).unwrap());
        left.push(parts.next().unwrap());
        *right.entry(parts.next().unwrap()).or_default() += 1;
    }
    let similar: u32 = left.iter()
        .map(|n| n * right.get(n).copied().unwrap_or_default())
        .sum();
    println!("{similar}");
}

util::aoc_main!();
[โ€“] Gobbel2000@programming.dev 5 points 8 months ago

I actually agree that I enjoyed playing the first more than the second. In the second, the story just didn't feel very gripping, progression was slow and gameplay ended up quite complicated with weird mechanics. But in the first game, the atmosphere, story and more distraction-free gameplay made up for the overall age of the game.

[โ€“] Gobbel2000@programming.dev 16 points 9 months ago (2 children)

I have been really happy with sway. It does all that I want it to do.

[โ€“] Gobbel2000@programming.dev 3 points 9 months ago

Is it a requirement for journalists now to not understand how the unit Watts works in relation to time?

[โ€“] Gobbel2000@programming.dev 0 points 9 months ago (1 children)

While I can agree with most of this map, you have absolutely no right to include South Tyrolia in this, they value their food a lot and don't lack behind the rest of Italy at all.

[โ€“] Gobbel2000@programming.dev 25 points 9 months ago (3 children)

What bias is it if the only entry I've read in this table is the one for confirmation bias?

[โ€“] Gobbel2000@programming.dev 19 points 9 months ago (1 children)

That P != NP.

[โ€“] Gobbel2000@programming.dev 6 points 9 months ago (2 children)

I can already do that on LineageOS 21 (Android 14), no rooting required.

[โ€“] Gobbel2000@programming.dev 7 points 9 months ago

Debugging CI pipelines is so annoying, why is there no better way than committing a bunch of dumb changes until it works?

view more: โ€น prev next โ€บ