This commit is contained in:
2016-12-01 13:11:04 +01:00
parent 2ebad93d10
commit 204757aba5
4 changed files with 1080 additions and 0 deletions

4
2015/02/Cargo.lock generated Normal file
View File

@@ -0,0 +1,4 @@
[root]
name = "02"
version = "0.1.0"

6
2015/02/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "02"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]

1000
2015/02/input.txt Normal file

File diff suppressed because it is too large Load Diff

70
2015/02/src/main.rs Normal file
View File

@@ -0,0 +1,70 @@
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
let handle = stdin.lock();
let count = handle
.lines()
.map(|x| x.unwrap())
.fold((0, 0), |acc, x| (acc.0 + calculate_paper(&x), acc.1 + calculate_ribbon(&x)));
println!("{:?}", count);
}
fn calculate_paper(dimensions: &str) -> i32 {
let parts = dimensions
.split('x')
.map(|x| x.parse::<i32>().unwrap())
.collect::<Vec<_>>();
let l = parts[0];
let w = parts[1];
let h = parts[2];
let e = l * w * h / parts.iter().max().unwrap();
(2 * l * w) + (2 * w * h) + (2 * h * l) + e
}
fn calculate_ribbon(dimensions: &str) -> i32 {
let parts = dimensions
.split('x')
.map(|x| x.parse::<i32>().unwrap())
.collect::<Vec<_>>();
let l = parts[0];
let w = parts[1];
let h = parts[2];
let e = (2 * l) + (2 * w) + (2 * h) - (2 * parts.iter().max().unwrap());
(l * w * h) + e
}
#[cfg(test)]
mod tests {
use super::{calculate_paper, calculate_ribbon};
#[test]
fn example_01() {
assert_eq!(58, calculate_paper("2x3x4"));
}
#[test]
fn example_02() {
assert_eq!(43, calculate_paper("1x1x10"));
}
#[test]
fn example_03() {
assert_eq!(34, calculate_ribbon("2x3x4"));
}
#[test]
fn example_04() {
assert_eq!(14, calculate_ribbon("1x1x10"));
}
}