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

  • CameronDevOPM
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    1 month ago

    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
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 month ago

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