my solutions to advent of code
aoc
advent-of-code
1// fun fact for this version, my biggest bug was integer overflow when i was using u32 because im not accustumed to this shit
2
3fn solve(input: &Vec<Vec<u64>>, digits: usize) -> u64 {
4 input.iter().fold(0, |acc, bank| {
5 let bank_len = bank.len();
6 let (n, _) = (0..digits).rfold((0, 0), |(number, bank_index), i| {
7 let (loc, max) = bank[bank_index..bank_len - i]
8 .iter()
9 .enumerate()
10 // apparently the compiler is fine with reversing this and then using the standard max_by but im not. and it seems to have the same speed results. im not gonna be using tricks here ok im nice
11 .reduce(
12 |(maxi, max), (i, n)| {
13 if n > max { (i, n) } else { (maxi, max) }
14 },
15 )
16 // #yup #i'm unwrapping #idontwannausefold
17 .unwrap();
18
19 ((number * 10) + max, bank_index + loc + 1)
20 });
21 acc + n
22 })
23}
24
25fn main() {
26 let input: Vec<Vec<u64>> = include_str!("../../input.txt")
27 .trim()
28 .split("\n")
29 .map(|bank| {
30 bank.chars()
31 .map(|s| s.to_digit(10).unwrap() as u64)
32 .collect()
33 })
34 .collect();
35
36 println!("Part 1: {}", solve(&input, 2));
37 println!("Part 2: {}", solve(&input, 12));
38}