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
[โ€“] [S] 2 points 10 months ago* (last edited 10 months 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);
    }

  • source
  • hideshow 2 child comments