this post was submitted on 05 Dec 2025
21 points (100.0% liked)

Advent Of Code

1199 readers
3 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 5: Cafeteria

Megathread guidelines

  • 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

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] CameronDev@programming.dev 1 points 2 weeks ago

Rust

   #[test]
    fn test_y2025_day5_part2() {
        let input = std::fs::read_to_string("input/2025/day_5.txt").unwrap();
        let (fresh, _) = input.split_once("\n\n").unwrap();
        let mut fresh = fresh
            .lines()
            .map(|l| {
                let (p1, p2) = l.split_once("-").unwrap();
                (p1.parse::<usize>().unwrap(), p2.parse::<usize>().unwrap())
            })
            .collect::<Vec<(usize, usize)>>();

        fresh.sort_by_key(|a| a.0);

        let mut non_overlapping = vec![*fresh.first().unwrap()];

        for range in fresh[1..].iter() {
            let last = *non_overlapping.last().unwrap();
            if range.0 > last.1 {
                // println!("Non overlapping: {range:?} -> {last:?}");
                non_overlapping.push((range.0, range.1));
                continue;
            }
            if range.0 <= last.1 && range.1 > last.1 {
                // println!("Overlapping: {range:?} -> {last:?}");
                let new_last = (last.0, range.1);
                non_overlapping.pop();
                non_overlapping.push(new_last);
                continue;
            }
            // println!("{range:?} is entirely within {last:?}");
        }

        let mut count = 0;
        for r in &non_overlapping {
            count += r.1 - r.0 + 1;
        }
        assert_eq!(count, 352556672963116);
        println!("{}", count);
    }

Took me way longer than it should have to work out pt2, got tripped up by a < instead of <=.