add until day05-1
This commit is contained in:
Generated
+25
@@ -0,0 +1,25 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.14.0"
|
||||
@@ -0,0 +1,2 @@
|
||||
2x3x4
|
||||
1x1x10
|
||||
+1000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,46 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
const FILENAME: &str = "example.txt";
|
||||
#[cfg(not(debug_assertions))]
|
||||
const FILENAME: &str = "input.txt";
|
||||
|
||||
fn main() {
|
||||
let data = std::fs::read_to_string(FILENAME).unwrap();
|
||||
|
||||
part_1(&data);
|
||||
part_2(&data);
|
||||
}
|
||||
|
||||
fn part_2(data: &str) {
|
||||
let mut sum = 0;
|
||||
for line in data.lines() {
|
||||
let edges: Vec<_> = line
|
||||
.split('x')
|
||||
.map(|x| x.parse::<usize>().unwrap())
|
||||
.collect();
|
||||
|
||||
let perimeter = edges.iter().sorted().take(2).sum::<usize>() * 2;
|
||||
let volume = edges.iter().product::<usize>();
|
||||
|
||||
sum += perimeter + volume;
|
||||
}
|
||||
println!("{sum}");
|
||||
}
|
||||
|
||||
fn part_1(data: &str) {
|
||||
let mut sum = 0;
|
||||
for line in data.lines() {
|
||||
let sides: Vec<_> = line
|
||||
.split('x')
|
||||
.map(|x| x.parse::<usize>().unwrap())
|
||||
.combinations(2)
|
||||
.map(|x| x[0] * x[1])
|
||||
.collect();
|
||||
|
||||
let smallest = sides.iter().min().copied().unwrap();
|
||||
|
||||
sum += sides.into_iter().sum::<usize>() * 2 + smallest;
|
||||
}
|
||||
println!("{sum}");
|
||||
}
|
||||
Reference in New Issue
Block a user