Skip Navigation

Posts
14
Comments
261
Joined
2 yr. ago

  • Rust

    Mainly difficult parsing today.

    View on github

     rust
        
    fn part1(input: String) {
        let mut nums: Vec<Vec<u64>> = Vec::new();
        let mut mul: Vec<bool> = Vec::new();
        for l in input.lines() {
            if l.chars().next().unwrap().is_ascii_digit() {
                let row = l
                    .split_ascii_whitespace()
                    .map(|s| s.parse::<u64>().unwrap())
                    .collect();
                nums.push(row);
            } else {
                mul = l.split_ascii_whitespace().map(|s| s == "*").collect();
            }
        }
        let mut sum = 0;
        for (idx, op_mul) in mul.iter().enumerate() {
            let col = nums.iter().map(|row| row[idx]);
            sum += if *op_mul {
                col.reduce(|acc, n| acc * n)
            } else {
                col.reduce(|acc, n| acc + n)
            }
            .unwrap();
        }
        println!("{sum}");
    }
    
    fn part2(input: String) {
        let grid: Vec<&[u8]> = input.lines().map(|l| l.as_bytes()).collect();
        let n_rows = grid.len() - 1; // Not counting operator row
        let mut op_mul = grid[n_rows][0] == b'*';
        let mut cur = if op_mul { 1 } else { 0 };
        let mut sum = 0;
        for x in 0..grid[0].len() {
            let digits: Vec<u8> = (0..n_rows).map(|y| grid[y][x]).collect();
            if digits.iter().all(|d| *d == b' ') {
                sum += cur;
                op_mul = grid[n_rows][x + 1] == b'*';
                cur = if op_mul { 1 } else { 0 };
                continue;
            }
            let n = String::from_utf8(digits)
                .unwrap()
                .trim()
                .parse::<u64>()
                .unwrap();
            if op_mul {
                cur *= n;
            } else {
                cur += n;
            }
        }
        sum += cur;
        println!("{sum}");
    }
    
    util::aoc_main!();
    
      
  • Rust

    Tried to outsmart part 2 by not merging ranges but just subtracting overlapping ranges from the current range's size, but then overlapping overlaps are subtracted more than once. So I ended up merging the ranges. They are kept in a list that is sorted by their starting position. Also, transforming the inclusive ranges in the input into exclusive ranges made things quite a bit easier.

    View on github

     rust
        
    use std::ops::Range;
    
    fn parse_input(input: &str) -> (Vec<Range<u64>>, Vec<u64>) {
        let ranges: Vec<_> = input
            .lines()
            .take_while(|l| !l.is_empty())
            .map(|l| {
                let (a, b) = l.split_once('-').unwrap();
                a.parse().unwrap()..b.parse::<u64>().unwrap() + 1
            })
            .collect();
        let nums = input
            .lines()
            .skip(ranges.len() + 1)
            .map(|n| n.parse().unwrap())
            .collect();
        (ranges, nums)
    }
    
    fn part1(input: String) {
        let (ranges, nums) = parse_input(&input);
        let count = nums
            .iter()
            .filter(|n| ranges.iter().any(|r| r.contains(n)))
            .count();
        println!("{count}");
    }
    
    fn part2(input: String) {
        let (ranges, _) = parse_input(&input);
        // Ranges are added to this Vec always sorted by start and non-overlapping
        let mut merged: Vec<Range<u64>> = Vec::with_capacity(ranges.len());
        for r in ranges {
            // Find index of first intersecting range
            let first_int_o = merged.iter().position(|m| {
                // Intersection range (if any)
                let int_start = r.start.max(m.start);
                let int_end = r.end.min(m.end);
                int_start < int_end
            });
            if let Some(first_int) = first_int_o {
                // Exclusive
                let last_int = merged.len()
                    - merged
                        .iter()
                        .rev()
                        .position(|m| {
                            let int_start = r.start.max(m.start);
                            let int_end = r.end.min(m.end);
                            int_start < int_end
                        })
                        .unwrap();
                // New range replacing all intersecting ranges
                let new = r.start.min(merged[first_int].start)..r.end.max(merged[last_int - 1].end);
                merged[first_int] = new;
                for i in (first_int + 1)..last_int {
                    merged.remove(i);
                }
            } else {
                // Does not overlap with anything. Find index that keeps sorting
                let idx = merged
                    .iter()
                    .position(|m| m.start > r.start)
                    .unwrap_or(merged.len());
                merged.insert(idx, r);
            }
        }
        let count = merged.iter().map(|r| r.end - r.start).sum::<u64>();
        println!("{count}");
    }
    
    util::aoc_main!();
    
      
  • Rust

    View on github

     rust
        
    fn parse_input(input: &str) -> Vec<Vec<bool>> {
        input
            .lines()
            .map(|l| l.chars().map(|c| c == '@').collect())
            .collect()
    }
    
    fn count_adj(grid: &[Vec<bool>], (x, y): (usize, usize)) -> usize {
        let width = grid[0].len();
        let height = grid.len();
        grid.iter()
            .take((y + 2).min(height))
            .skip(y.saturating_sub(1))
            .map(|r| {
                r.iter()
                    .take((x + 2).min(width))
                    .skip(x.saturating_sub(1))
                    .take(3)
                    .filter(|e| **e)
                    .count()
            })
            .sum::<usize>()
    }
    
    fn part1(input: String) {
        let grid = parse_input(&input);
        let mut count = 0u32;
        for (y, row) in grid.iter().enumerate() {
            for (x, _) in row.iter().enumerate().filter(|(_, r)| **r) {
                let n_adj = count_adj(&grid, (x, y));
                // Center roll is counted too
                if n_adj < 5 {
                    count += 1;
                }
            }
        }
        println!("{count}");
    }
    
    fn part2(input: String) {
        let mut grid = parse_input(&input);
        let mut removed = 0u32;
        loop {
            let mut next_grid = grid.clone();
            let prev_removed = removed;
            for (y, row) in grid.iter().enumerate() {
                for (x, _) in row.iter().enumerate().filter(|(_, r)| **r) {
                    let n_adj = count_adj(&grid, (x, y));
                    // Center roll is counted too
                    if n_adj < 5 {
                        next_grid[y][x] = false;
                        removed += 1;
                    }
                }
            }
            if removed == prev_removed {
                break;
            }
            grid = next_grid;
        }
        println!("{}", removed);
    }
    
    util::aoc_main!();
    
      
  • Rust

    Seeing some of the other solutions in this thread, there are definitely simpler (and probably still faster) solutions possible, but I first sorted the bank by the highest batteries (keeping the index information) and then used a recursive greedy algorithm to find the largest battery that still follows the index order.

    View on github

     rust
        
    fn part1(input: String) {
        let mut sum = 0;
        'banks: for l in input.lines() {
            let mut sorted: Vec<(usize, u32)> = l
                .chars()
                .map(|c| c.to_digit(10).unwrap())
                .enumerate()
                .collect();
            sorted.sort_by(|(_, a), (_, b)| a.cmp(b).reverse());
            for (idx, first) in &sorted {
                for (id2, second) in &sorted {
                    if id2 > idx {
                        sum += first * 10 + second;
                        continue 'banks;
                    }
                }
            }
        }
        println!("{sum}");
    }
    
    // Recursive implementation of greedy algorithm.
    // Returns Vec of length 12 if a result was found, guaranteed to be optimal.
    // If there is no solution with the input, a shorter Vec is returned.
    fn recursive(bank: &[(usize, u32)], mut cur: Vec<(usize, u32)>) -> Vec<(usize, u32)> {
        let pos = cur.last().unwrap().0;
        for &(idx, e) in bank.iter().filter(|(idx, _)| *idx > pos) {
            cur.push((idx, e));
            if cur.len() == 12 {
                // Recursion anchor: We have filled all 12 spots and therefore found
                // the best solution
                return cur;
            }
            // Recurse
            cur = recursive(bank, cur);
            if cur.len() == 12 {
                // Result found
                return cur;
            }
            // Nothing found, try next in this position
            cur.pop();
        }
        // Unsuccessful search with given inputs
        cur
    }
    
    fn part2(input: String) {
        let mut sum = 0;
        'banks: for l in input.lines() {
            let mut sorted: Vec<(usize, u32)> = l
                .chars()
                .map(|c| c.to_digit(10).unwrap())
                .enumerate()
                .collect();
            sorted.sort_by(|(_, a), (_, b)| a.cmp(b).reverse());
            let mut cur: Vec<(usize, u32)> = Vec::with_capacity(12);
            for &(idx, first) in &sorted {
                cur.push((idx, first));
                cur = recursive(&sorted, cur);
                if cur.len() == 12 {
                    let num = cur.iter().fold(0u64, |acc, e| acc * 10 + e.1 as u64);
                    sum += num;
                    continue 'banks;
                }
                cur.pop();
            }
        }
        println!("{sum}");
    }
    
    util::aoc_main!();
    
      
  • 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.

     rust
        
    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!();
    
      
  • 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

     rust
        
    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!();
    
      
  • 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.

  • Russians already far ahead:

  • Wait, it's not pronounced pffned?

  • Generalsekretärin?

  • This is actually an air defence drone in action.

  • 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.

  • 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.

  • It's from the Draconic Evolution mod.

  • You're probably on the right track. Every hunk of symbols is probably a valid type expression in some system. Including a square root type.

  • Okay, but even if we assumed (x=b) to be a very small equivalence relation, it should appear in the denominator position to form an equivalence quotient.