2015 day 2
This commit is contained in:
parent
351fc11bed
commit
6ad8e84551
28
2015/src/bin/day2.rs
Normal file
28
2015/src/bin/day2.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
use aoc::input;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let lines = input::read_lines();
|
||||||
|
let dimensions: Vec<Vec<u32>> = lines.iter().map(|line| {
|
||||||
|
line.split('x').map(|dim| dim.parse::<u32>().unwrap()).collect()
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
// Part 1
|
||||||
|
let total: u32 = dimensions.iter().map(|dim| {
|
||||||
|
let (l, w, h) = (dim[0], dim[1], dim[2]);
|
||||||
|
let areas = vec!(l * w, w * h, h * l);
|
||||||
|
let slack = areas.iter().min().unwrap();
|
||||||
|
areas.iter().map(|a| 2 * a).sum::<u32>() + slack
|
||||||
|
}).sum();
|
||||||
|
println!("Needed paper: {}.", total);
|
||||||
|
|
||||||
|
// Part 2
|
||||||
|
let total: u32 = dimensions.iter().map(|dim| {
|
||||||
|
let mut sdim = dim.clone();
|
||||||
|
sdim.sort();
|
||||||
|
let (d1, d2, d3) = (sdim[0], sdim[1], sdim[2]);
|
||||||
|
let wrap = 2 * d1 + 2 * d2;
|
||||||
|
let bow = d1 * d2 * d3;
|
||||||
|
wrap + bow
|
||||||
|
}).sum();
|
||||||
|
println!("Needed ribbon: {}.", total);
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
use aoc::input;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue