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