solution: day2

This commit is contained in:
Matej Janezic 2022-11-28 23:28:19 +01:00
parent 0000001084
commit 0000002038
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
1 changed files with 55 additions and 0 deletions

55
src/bin/02.rs Normal file
View File

@ -0,0 +1,55 @@
pub fn part_one(input: &str) -> Option<u32> {
let present_dimensions: Vec<Vec<u32>> = input
.trim()
.split('\n')
.map(|x| x.split('x').map(|y| y.parse::<u32>().unwrap()).collect())
.collect();
let mut wrapping = 0;
for present in present_dimensions.iter() {
if let [x, y, z] = &present[..] {
wrapping +=
2 * (x * y + y * z + z * x) + vec![x * y, y * z, z * x].iter().min().unwrap();
}
}
Some(wrapping)
}
pub fn part_two(input: &str) -> Option<u32> {
let present_dimensions: Vec<Vec<u32>> = input
.trim()
.split('\n')
.map(|x| x.split('x').map(|y| y.parse::<u32>().unwrap()).collect())
.collect();
let mut wrapping = 0;
for present in present_dimensions.iter() {
if let [x, y, z] = &present[..] {
// ribbon
wrapping += x * y * z;
// ribbon base
wrapping += 2 * (x + y + z - present.iter().max().unwrap());
}
}
Some(wrapping)
}
fn main() {
let input = &aoc::read_file("inputs", 2);
aoc::solve!(1, part_one, input);
aoc::solve!(2, part_two, input);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
// let input = aoc::read_file("test_inputs", 2);
let input = "2x3x4";
assert_eq!(part_one(&input), Some(58));
}
#[test]
fn test_part_two() {
// let input = aoc::read_file("test_inputs", 2);
let input = "2x3x4";
assert_eq!(part_two(&input), Some(34));
}
}