my solutions to advent of code
aoc advent-of-code
1use itertools::Itertools; 2 3fn main() { 4 let input = std::fs::read_to_string("../input.txt").expect("invalid input!!"); 5 let input = input 6 .trim() 7 .split("\n") 8 .map(|v| v.split("x").map(|n| n.parse().expect("not a number"))); 9 10 let part1 = input.clone().fold(0, |acc, v| { 11 let sides: Vec<i32> = v 12 .combinations(2) 13 .map(|pair| pair.into_iter().reduce(|a, b| a * b).unwrap()) 14 .collect(); 15 let sides = sides.into_iter(); 16 17 acc + sides.clone().fold(0, |acc, v| acc + v) * 2 18 + sides.clone().reduce(|a, b| a.min(b)).unwrap() 19 }); 20 println!("{part1}"); 21 22 let part2 = input.clone().fold(0, |acc, v| { 23 let smallest_perimeter = v 24 .clone() 25 .map(|v| v * 2) 26 .combinations(2) 27 .map(|v| v.into_iter().fold(0, |a, b| a + b)) 28 .reduce(|a, b| a.min(b)) 29 .unwrap(); 30 31 acc + smallest_perimeter + v.fold(1, |a, b| a * b) 32 }); 33 println!("{part2}"); 34}