My submissions for Advent of Code 2025 adventofcode.com/2025
rust aoc
at main 37 lines 1.4 kB view raw
1use std::{fs::read_to_string, iter}; 2 3fn main() { 4 let input = read_to_string("../input.txt").expect("failed to open input file"); 5 let mut problems = input.lines(); 6 let operator_line = problems.next_back().expect("file was empty"); 7 let numbers: Vec<_> = problems.collect(); 8 9 let operators: Vec<_> = operator_line.match_indices(['+', '*']).collect(); 10 let last_operator = operators.last().expect("couldn't find any operators"); 11 12 let grand_total: u64 = operators 13 .windows(2) 14 .map(|window| (window[0].0..window[1].0 - 1, window[0].1)) 15 .chain(iter::once(( 16 last_operator.0..operator_line.len(), 17 last_operator.1, 18 ))) 19 .map(|(problem_range, operator)| { 20 let numbers = problem_range.map(|column| { 21 numbers 22 .iter() 23 .filter_map(|number_line| (number_line.as_bytes()[column] as char).to_digit(10)) 24 .rev() 25 .enumerate() 26 .map(|(placement, digit)| u64::from(digit) * 10u64.pow(placement as u32)) 27 .sum::<u64>() 28 }); 29 match operator { 30 "+" => numbers.sum::<u64>(), 31 "*" => numbers.product(), 32 operator => panic!("unrecognised operator '{operator}'"), 33 } 34 }) 35 .sum(); 36 println!("The grand total is {grand_total}!"); 37}