this post was submitted on 06 Dec 2025
25 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 6: Trash Compactor

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 2 points 2 weeks ago* (last edited 2 weeks ago) (2 children)

Rust

Pt1 easy, pt2 cry

edit: Updated with more iterring :)

view code

    #[test]
    fn test_y2025_day6_part1() {
        let input = include_str!("../../input/2025/day_6.txt");
        let lines = input.lines().collect::<Vec<&str>>();
        let nr = lines[0..4]
            .iter()
            .map(|l| {
                l.split_whitespace()
                    .map(|s| s.parse::<usize>().unwrap())
                    .collect::<Vec<usize>>()
            })
            .collect::<Vec<Vec<usize>>>();
        let operations = lines[4].split_whitespace().collect::<Vec<&str>>();

        let total = operations
            .iter()
            .enumerate()
            .map(|(i, op)| match *op {
                "*" => [0, 1, 2, 3].iter().map(|j| nr[*j][i]).product::<usize>(),
                "+" => [0, 1, 2, 3].iter().map(|j| nr[*j][i]).sum::<usize>(),
                _ => panic!("Unknown operation {}", op),
            })
            .sum::<usize>();
        assert_eq!(4412382293768, total);
        println!("Total: {}", total);
    }

    #[test]
    fn test_y2025_day6_part2() {
        let input = std::fs::read_to_string("input/2025/day_6.txt").unwrap();
        let lines = input
            .lines()
            .map(|s| s.chars().collect::<Vec<char>>())
            .collect::<Vec<Vec<char>>>();
        let mut i = lines[0].len();

        let mut numbers = vec![];
        let mut total = 0;
        while i > 0 {
            i -= 1;
            let number = [0, 1, 2, 3]
                .iter()
                .filter_map(|j| lines[*j][i].to_digit(10))
                .fold(0, |acc, x| acc * 10 + x);
            if number == 0 {
                continue;
            }
            numbers.push(number as usize);
            match lines[4][i] {
                '*' => {
                    total += numbers.iter().product::<usize>();
                    numbers.clear();
                }
                '+' => {
                    total += numbers.iter().sum::<usize>();
                    numbers.clear();
                }
                ' ' => {}
                _ => panic!("Unknown operation {}", lines[4][i]),
            }
        }
        assert_eq!(7858808482092, total);
        println!("Total: {}", total);
    }

[โ€“] Deebster@programming.dev 2 points 2 weeks ago (1 children)

Why not use .iter().sum() and .iter().product()?

[โ€“] CameronDev@programming.dev 2 points 2 weeks ago

Because I wasn't aware of them, thanks!