Gobbel2000

joined 2 years ago

Rust

View on github

I feared that this required some complicated maths to quickly figure out the next "invalid" number, but the total number of IDs to check was only about 2 million, so brute force it is.

use std::ops::RangeInclusive;

fn parse_input(input: &str) -> Vec<RangeInclusive<u64>> {
    input
        .trim()
        .split(',')
        .map(|r| {
            let (a, b) = r.split_once('-').unwrap();
            RangeInclusive::new(a.parse().unwrap(), b.parse().unwrap())
        })
        .collect()
}

fn part1(input: String) {
    let ranges = parse_input(&input);
    let mut sum = 0;
    for e in ranges.into_iter().flatten() {
        let width = e.ilog10() + 1;
        if width % 2 == 0 {
            let top = 10u64.pow(width / 2);
            if e / top == e % top {
                sum += e;
            }
        }
    }
    println!("{sum}");
}

fn part2(input: String) {
    let ranges = parse_input(&input);
    let mut sum = 0;
    'nums: for e in ranges.into_iter().flatten() {
        let width = e.ilog10() + 1;
        for rep in 2..=width {
            if width % rep == 0 {
                let top = 10u64.pow(width / rep);
                let mut a = e;
                let lowest = a % top;
                let mut invalid = true;
                while a > top {
                    a /= top;
                    if a % top != lowest {
                        invalid = false;
                        break;
                    }
                }
                if invalid {
                    sum += e;
                    // Don't check other numbers of repetitions
                    continue 'nums;
                }
            }
        }
    }
    println!("{sum}");
}

util::aoc_main!();
[–] Gobbel2000@programming.dev 5 points 1 week ago (1 children)

Nice solution. Just a little Rust tip if you don't mind: In this case you can avoid cloning the input Vec in the loops by instead looping over references into the list with for n in self.input.iter() or simpler for n in &self.input. The only difference is that n will be of type &i64 instead of i64.

Rust

Almost missed, that "the dial starts by pointing at 50".

View on github

const N: i32 = 100;

fn parse_line(l: &str) -> (i32, i32) {
    let dir = match l.chars().next().unwrap() {
        'L' => -1,
        'R' => 1,
        _ => panic!(),
    };
    let dist = l[1..].parse::<i32>().unwrap();
    (dir, dist)
}

fn part1(input: String) {
    let mut pos = 50;
    let mut count0 = 0;
    for l in input.lines() {
        let (dir, dist) = parse_line(l);
        pos = (pos + dir * dist) % N;
        if pos == 0 {
            count0 += 1;
        }
    }
    println!("{count0}");
}

fn part2(input: String) {
    let mut pos = 50;
    let mut count0 = 0;
    for l in input.lines() {
        let (dir, dist) = parse_line(l);
        if dir == 1 {
            count0 += (pos + dist) / N;
        } else {
            count0 += ((N - pos) % N + dist) / N;
        }
        pos = (pos + dir * dist).rem_euclid(N);
    }
    println!("{count0}");
}

util::aoc_main!();
[–] Gobbel2000@programming.dev 14 points 2 weeks ago (4 children)

The practical answer is: you drive as far as you legally can.

As a disclaimer, pictured here are the Himalayas, which are at a completely different scale to where I've been, but in my experience there are typically parking spaces/bus stops at the end of public roads. At this point you leave the built up infrastructure and enter nature, and these are often located in a place where the flatter valley ends and a steeper ascent begins. In many cases there are smaller private roads further up to service more remote cabins or farmsteads. Sometimes there are even taxi services that drive you further along using private roads, which can be seen as not fully scaling the mountain yourself. Generally, the closest public parking is considered the starting point and most people will therefore start at the same spot.

To be fair, nowadays the Linux kernel does rely quite a bit on resources from major software (and hardware) companies.

[–] Gobbel2000@programming.dev 6 points 1 month ago (1 children)

Russians already far ahead:

Wait, it's not pronounced pffned?

GeneralsekretΓ€rin?

This is actually an air defence drone in action.

[–] Gobbel2000@programming.dev 13 points 1 month ago (1 children)

All offices within the EU administration will be supplied with the officially modified flavors of eumacs and neuvim.

Gustav Holst, Neptune (from The Planets). Really strikingly captures the sense of the outer edges of our solar system, even if it's not the whole world.

[–] Gobbel2000@programming.dev 5 points 1 month ago (3 children)

Did an oopsie. I never realized that after upgrading the OS, my certbot renew service to renew the HTTPS certificate always failed. So now I had an expired certificate. At least it was an easy fix by reinstalling certbot.

view more: β€Ή prev next β€Ί