Skip to content

Commit 23201e5

Browse files
committed
feat(02/2015): solve second part
1 parent eb6a274 commit 23201e5

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
| Day | Solved | Part 1 time (ms) | Part 2 time (ms) |
8989
|-----------------------------------------------------------------------------|:------:|-----------------:|-----------------:|
9090
| [Day 1: Not Quite Lisp](src/solutions/year2015/day01.rs) | ⭐⭐ | 0.013 | 0.001 |
91-
| [Day 2: I Was Told There Would Be No Math](src/solutions/year2015/day02.rs) | | 0.074 | - |
91+
| [Day 2: I Was Told There Would Be No Math](src/solutions/year2015/day02.rs) | | 0.074 | 0.105 |
9292

9393
# TODO
9494

src/solutions/year2015/day02.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,22 @@ impl Solution for Day02 {
1212
.to_string()
1313
}
1414

15-
fn part_two(&self, _input: &str) -> String {
16-
String::from("0")
15+
fn part_two(&self, input: &str) -> String {
16+
self.parse(input)
17+
.map(|cuboid| {
18+
let around_perimeter = cuboid
19+
.perimeters()
20+
.iter()
21+
.sorted()
22+
.take(2)
23+
.map(|d| 2 * d)
24+
.sum::<u64>();
25+
let bow = cuboid.perimeters().iter().product::<u64>();
26+
27+
around_perimeter + bow
28+
})
29+
.sum::<u64>()
30+
.to_string()
1731
}
1832
}
1933

@@ -56,6 +70,10 @@ impl RectangularCuboid {
5670
fn area_length_height(&self) -> u64 {
5771
self.length * self.height
5872
}
73+
74+
fn perimeters(&self) -> [u64; 3] {
75+
[self.width, self.length, self.height]
76+
}
5977
}
6078

6179
impl FromStr for RectangularCuboid {
@@ -85,4 +103,10 @@ mod tests {
85103
assert_eq!("58", Day02.part_one("2x3x4"));
86104
assert_eq!("43", Day02.part_one("1x1x10"));
87105
}
106+
107+
#[test]
108+
fn part_two_example_test() {
109+
assert_eq!("34", Day02.part_two("2x3x4"));
110+
assert_eq!("14", Day02.part_two("1x1x10"));
111+
}
88112
}

0 commit comments

Comments
 (0)