Advent of Code solutions
at main 70 lines 2.1 kB view raw
1use advent_core::{day_stuff, ex_for_day, Day}; 2 3pub struct Day3; 4 5impl Day for Day3 { 6 day_stuff!(3, "357", "3121910778619", Vec<Vec<usize>>); 7 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(); 47 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()) 57 } 58 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() 69 } 70}