my solutions to advent of code
aoc advent-of-code

day 2 2015 in rust - gleam is nicer

aylac.top 0745e810 f761602e

verified
Changed files
+67 -4
2015
template
rust
src
+25
2015/2/rust/Cargo.lock
···
+
# This file is automatically @generated by Cargo.
+
# It is not intended for manual editing.
+
version = 4
+
+
[[package]]
+
name = "either"
+
version = "1.15.0"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
+
+
[[package]]
+
name = "itertools"
+
version = "0.14.0"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
+
dependencies = [
+
"either",
+
]
+
+
[[package]]
+
name = "rust"
+
version = "0.1.0"
+
dependencies = [
+
"itertools",
+
]
+7
2015/2/rust/Cargo.toml
···
+
[package]
+
name = "rust"
+
version = "0.1.0"
+
edition = "2024"
+
+
[dependencies]
+
itertools = "0.14.0"
+34
2015/2/rust/src/main.rs
···
+
use itertools::Itertools;
+
+
fn main() {
+
let input = std::fs::read_to_string("../input.txt").expect("invalid input!!");
+
let input = input
+
.trim()
+
.split("\n")
+
.map(|v| v.split("x").map(|n| n.parse().expect("not a number")));
+
+
let part1 = input.clone().fold(0, |acc, v| {
+
let sides: Vec<i32> = v
+
.combinations(2)
+
.map(|pair| pair.into_iter().reduce(|a, b| a * b).unwrap())
+
.collect();
+
let sides = sides.into_iter();
+
+
acc + sides.clone().fold(0, |acc, v| acc + v) * 2
+
+ sides.clone().reduce(|a, b| a.min(b)).unwrap()
+
});
+
println!("{part1}");
+
+
let part2 = input.clone().fold(0, |acc, v| {
+
let smallest_perimeter = v
+
.clone()
+
.map(|v| v * 2)
+
.combinations(2)
+
.map(|v| v.into_iter().fold(0, |a, b| a + b))
+
.reduce(|a, b| a.min(b))
+
.unwrap();
+
+
acc + smallest_perimeter + v.fold(1, |a, b| a * b)
+
});
+
println!("{part2}");
+
}
+1 -4
template/rust/src/main.rs
···
fn main() {
-
let input: String = match std::fs::read_to_string("../input.txt") {
-
Ok(input) => input,
-
_ => panic!("invalid input!!!"),
-
};
+
let input = std::fs::read_to_string("../input.txt").expect("invalid input!!");
println!("{}", &input);
}