Advent of Code solutions

Day 3

bwc9876.dev 02846257 36e9353c

verified
+67 -6
+59 -6
years/2025/src/day_3.rs
··· 1 - 2 1 use advent_core::{Day, day_stuff, ex_for_day}; 3 2 4 3 pub struct Day3; 5 4 6 5 impl Day for Day3 { 6 + day_stuff!(3, "357", "3121910778619", Vec<Vec<usize>>); 7 7 8 - day_stuff!(3, "", ""); 8 + fn part_1(input: Self::Input) -> Option<String> { 9 + let ans = input 10 + .into_iter() 11 + .map(|bank| { 12 + let (largest_idx, largest) = bank 13 + .iter() 14 + .enumerate() 15 + .take(bank.len() - 1) 16 + .rev() 17 + .max_by(|(_, x), (_, y)| (**x).cmp(*y)) 18 + .map(|(i, x)| (i, *x)) 19 + .unwrap(); 20 + let rest = bank.into_iter().skip(largest_idx + 1); 21 + let next_largest = rest.max().unwrap(); 22 + largest * 10 + next_largest 23 + }) 24 + .sum::<usize>(); 25 + 26 + Some(ans.to_string()) 27 + } 28 + 29 + fn part_2(input: Self::Input) -> Option<String> { 30 + let ans = input 31 + .into_iter() 32 + .map(|bank| { 33 + let mut num = 0; 34 + let mut start = 0; 35 + 36 + for i in 0..12 { 37 + let (idx, largest) = bank 38 + .iter() 39 + .enumerate() 40 + .filter(|(idx, _)| { 41 + (i == 0 || *idx > start) && *idx + (11 - i) <= (bank.len() - 1) 42 + }) 43 + .rev() 44 + .max_by(|(_, x), (_, y)| (**x).cmp(*y)) 45 + .map(|(idx, x)| (idx, *x)) 46 + .unwrap(); 9 47 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 48 + num += largest * 10_usize.pow((11 - i) as u32); 49 + start = idx; 50 + } 51 + 52 + num 53 + }) 54 + .sum::<usize>(); 55 + 56 + Some(ans.to_string()) 12 57 } 13 58 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 59 + fn parse_input(input: &str) -> Self::Input { 60 + input 61 + .lines() 62 + .map(|l| { 63 + l.trim() 64 + .chars() 65 + .map(|c| c.to_digit(10).unwrap() as usize) 66 + .collect() 67 + }) 68 + .collect() 16 69 } 17 70 }
+4
years/2025/src/examples/day_3/1.txt
··· 1 + 987654321111111 2 + 811111111111119 3 + 234234234234278 4 + 818181911112111
+4
years/2025/src/examples/day_3/2.txt
··· 1 + 987654321111111 2 + 811111111111119 3 + 234234234234278 4 + 818181911112111