-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.rs
51 lines (45 loc) · 1.12 KB
/
02.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#![feature(test)]
type Input = Vec<Present>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct Present {
l: u64,
w: u64,
h: u64,
}
fn setup(input: &str) -> Input {
input
.trim()
.lines()
.map(|line| {
let mut dims = line.split('x').map(|x| x.parse().unwrap());
let present = Present {
l: dims.next().unwrap(),
w: dims.next().unwrap(),
h: dims.next().unwrap(),
};
assert!(dims.next().is_none());
present
})
.collect()
}
fn part1(input: &Input) -> u64 {
input
.iter()
.map(|&Present { l, w, h }| {
let surface = 2 * l * w + 2 * w * h + 2 * h * l;
let extra = l * w * h / l.max(w).max(h);
surface + extra
})
.sum()
}
fn part2(input: &Input) -> u64 {
input
.iter()
.map(|&Present { l, w, h }| {
let ribbon = 2 * (l + w + h - l.max(w).max(h));
let bow = l * w * h;
ribbon + bow
})
.sum()
}
aoc::main!(2015, 2, ex: 1);