Skip to content

Commit 54da43f

Browse files
fix 2015 day 2 solution to be much cleaner
1 parent 00a5c66 commit 54da43f

File tree

4 files changed

+62
-22
lines changed

4 files changed

+62
-22
lines changed

src/main/java/dev/jacobandersen/codechallenges/challenge/adventofcode/AdventOfCodeBootstrap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public void run() {
2424
new Dummy().m();
2525
}
2626

27-
// Year2015.getDays().forEach(Day::run);
27+
Year2015.getDays().forEach(Day::run);
2828
// Year2020.getDays().forEach(Day::run);
2929
// Year2022.getDays().forEach(Day::run);
3030
// Year2023.getDays().forEach(Day::run);
31-
Year2024.getDays().forEach(Day::run);
31+
// Year2024.getDays().forEach(Day::run);
3232
}
3333
}
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
11
package dev.jacobandersen.codechallenges.challenge.adventofcode.year2015.day2;
22

33
import dev.jacobandersen.codechallenges.challenge.adventofcode.Day;
4-
import dev.jacobandersen.codechallenges.util.CombinatoricsUtil;
4+
import dev.jacobandersen.codechallenges.util.RectangularCuboid;
55

66
import java.util.Arrays;
7-
import java.util.Collection;
8-
import java.util.Comparator;
97
import java.util.List;
10-
import java.util.stream.Stream;
118

129
public class Day2 extends Day {
1310
public Day2() {
1411
super(2015, 2, "I Was Told There Would Be No Math");
1512
}
1613

17-
private List<List<Long>> getData() {
18-
return getInputLinesStreamNoBlanks().map(line -> Arrays.stream(line.split("x")).map(Long::parseLong).toList()).toList();
14+
private List<RectangularCuboid> getData() {
15+
return getInputLinesStreamNoBlanks()
16+
.map(line -> Arrays.stream(line.split("x")).map(Integer::parseInt).toList())
17+
.map(coords -> new RectangularCuboid(coords.get(0), coords.get(1), coords.get(2)))
18+
.toList();
1919
}
2020

21-
// Sorry, for what you are about to read. Yes it could be cleaner.
22-
// But, oh well. :D
23-
2421
@Override
2522
public String partOne() {
2623
return String.valueOf(getData().stream()
27-
.map(gift -> (2 * gift.get(0) * gift.get(1)) + (2 * gift.get(1) * gift.get(2)) +
28-
(2 * gift.get(2) * gift.get(0)) + Stream.of(List.of(gift.get(0), gift.get(1)),
29-
List.of(gift.get(0), gift.get(2)), List.of(gift.get(1), gift.get(2)))
30-
.map(part -> part.get(0) * part.get(1))
31-
.min(Long::compareTo).orElseThrow())
32-
.reduce(0L, Long::sum));
24+
.map(gift -> gift.surfaceArea() + gift.areaOfSmallestSide())
25+
.reduce(0, Integer::sum));
3326
}
3427

3528
@Override
3629
public String partTwo() {
3730
return String.valueOf(getData().stream()
38-
.map(gift -> (gift.get(0) * gift.get(1) * gift.get(2)) +
39-
Stream.of(List.of(gift.get(0), gift.get(1)), List.of(gift.get(0), gift.get(2)),
40-
List.of(gift.get(1), gift.get(2)))
41-
.map(part -> (2 * part.get(0)) + (2 * part.get(1))).min(Long::compareTo).orElseThrow())
42-
.reduce(0L, Long::sum));
31+
.map(gift -> gift.perimeterOfSmallestSide() + gift.volume())
32+
.reduce(0, Integer::sum));
4333
}
4434
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dev.jacobandersen.codechallenges.util;
2+
3+
import java.util.Comparator;
4+
import java.util.List;
5+
import java.util.stream.Stream;
6+
7+
public class RectangularCuboid implements Shape {
8+
private final int length;
9+
private final int width;
10+
private final int height;
11+
12+
public RectangularCuboid(int length, int width, int height) {
13+
this.length = length;
14+
this.width = width;
15+
this.height = height;
16+
}
17+
18+
@Override
19+
public int surfaceArea() {
20+
return 2 * ((length * width) + (length * height) + (width * height));
21+
}
22+
23+
@Override
24+
public int volume() {
25+
return length * width * height;
26+
}
27+
28+
@Override
29+
public int areaOfSmallestSide() {
30+
return Stream.of(length * width, length * height, width * height)
31+
.min(Integer::compareTo)
32+
.orElseThrow();
33+
}
34+
35+
@Override
36+
public int perimeterOfSmallestSide() {
37+
return Stream.of(length + width, length + height, width + height)
38+
.map(halfPerimeter -> 2 * halfPerimeter)
39+
.min(Integer::compareTo)
40+
.orElseThrow();
41+
}
42+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.jacobandersen.codechallenges.util;
2+
3+
public interface Shape {
4+
int surfaceArea();
5+
int volume();
6+
int areaOfSmallestSide();
7+
int perimeterOfSmallestSide();
8+
}

0 commit comments

Comments
 (0)