Skip to content

Commit 7728f64

Browse files
committed
Solved day 4 part 2
1 parent be25120 commit 7728f64

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2015'
7-
version '0.3.4'
7+
version '0.3.5'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day04/src/main/java/de/havox_design/aoc2015/day04/AdventCoins.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,26 @@ public static int solvePart2(String fileName) {
2323
}
2424

2525
public int solvePart1() {
26+
return detectSecretNumberForHashStart("00000");
27+
}
28+
29+
public int solvePart2() {
30+
return detectSecretNumberForHashStart("000000");
31+
}
32+
33+
private int detectSecretNumberForHashStart(String prefix) {
2634
for(int i = 0; i < Integer.MAX_VALUE; i++) {
2735
String combinedKey = input + i;
2836
String md5Hash = DigestUtils.md5Hex(combinedKey);
2937

30-
if(md5Hash.startsWith("00000")) {
38+
if(md5Hash.startsWith(prefix)) {
3139
return i;
3240
}
3341
}
3442

3543
throw new IllegalStateException("An valid hash should be able to be found.");
3644
}
3745

38-
public int solvePart2() {
39-
return 0;
40-
}
41-
4246
private List<String> readData(String fileName) {
4347
return DataReader.readData(fileName, MainClass.class);
4448
}

day04/src/test/java/de/havox_design/aoc2015/day04/Day04Test.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,21 @@ void testPart1(String fileName, int expectedSecretNumber) {
2424

2525
private static Stream<Arguments> getDataForPart1() {
2626
return Stream.of(
27-
Arguments.of("part1sample1.txt", 609043),
28-
Arguments.of("part1sample2.txt", 1048970)
27+
Arguments.of("sample1.txt", 609043),
28+
Arguments.of("sample2.txt", 1048970)
29+
);
30+
}
31+
32+
@ParameterizedTest
33+
@MethodSource("getDataForPart2")
34+
void testPart2(String fileName, int expectedSecretNumber) {
35+
Assertions.assertEquals(expectedSecretNumber, AdventCoins.solvePart2(fileName));
36+
}
37+
38+
private static Stream<Arguments> getDataForPart2() {
39+
return Stream.of(
40+
Arguments.of("sample1.txt", 6742839),
41+
Arguments.of("sample2.txt", 5714438)
2942
);
3043
}
3144
}

0 commit comments

Comments
 (0)