Skip to content

Commit 2b5f1c4

Browse files
committed
Solved day 1 part 2
1 parent a1eaea0 commit 2b5f1c4

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To run all solutions, simply run `./gradlew run`. If you want to run the solutio
1919

2020
| Day | Part 1 | Part 2 |
2121
|---------|---------|---------|
22-
| 1 || |
22+
| 1 || |
2323
| 2 |||
2424
| 3 |||
2525
| 4 |||
@@ -44,6 +44,6 @@ To run all solutions, simply run `./gradlew run`. If you want to run the solutio
4444
| 23 |||
4545
| 24 |||
4646
| 25 |||
47-
| **SUM** | **1 ⭐** | **0** |
47+
| **SUM** | **1 ⭐** | **1** |
4848

49-
Total: 1
49+
Total: 2

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.0.4'
7+
version '0.0.5'
88

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

day01/src/main/java/de/havox_design/aoc2015/day01/MainClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class MainClass {
77

88
public static void main(String[] args) {
99
LOGGER.info("Solution 1: " + NotQuiteLisp.processTask1("day01.txt"));
10-
LOGGER.info("Solution 2: 23");
10+
LOGGER.info("Solution 2: " + NotQuiteLisp.processTask2("day01.txt"));
1111
}
1212
}

day01/src/main/java/de/havox_design/aoc2015/day01/NotQuiteLisp.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public static int processTask1(String fileName) {
1616
return instance.processTask1();
1717
}
1818

19+
public static int processTask2(String fileName) {
20+
NotQuiteLisp instance = new NotQuiteLisp(fileName);
21+
return instance.processTask2();
22+
}
23+
1924
public int processTask1() {
2025
int currentFloor = 0;
2126

@@ -26,6 +31,22 @@ public int processTask1() {
2631
return currentFloor;
2732
}
2833

34+
public int processTask2() {
35+
int currentFloor = 0;
36+
int step = 0;
37+
38+
for(char c : input.toCharArray()) {
39+
step++;
40+
currentFloor += translateFloor(c);
41+
42+
if(-1 == currentFloor) {
43+
return step;
44+
}
45+
}
46+
47+
throw new IllegalArgumentException("Expected Santa to enter basement (floor '-1').");
48+
}
49+
2950
private int translateFloor(char c) {
3051
if('(' == c) {
3152
return 1;

day01/src/test/java/de/havox_design/aoc2015/day01/Day01Test.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,17 @@ private static Stream<Arguments> getDataForTask1() {
3535
Arguments.of("task1sample9.txt", -3)
3636
);
3737
}
38+
39+
@ParameterizedTest
40+
@MethodSource("getDataForTask2")
41+
void testTask2(String fileName, int expectedStep) {
42+
Assertions.assertEquals(expectedStep, NotQuiteLisp.processTask2(fileName));
43+
}
44+
45+
private static Stream<Arguments> getDataForTask2() {
46+
return Stream.of(
47+
Arguments.of("task2sample1.txt", 1),
48+
Arguments.of("task2sample2.txt", 5)
49+
);
50+
}
3851
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
()())

0 commit comments

Comments
 (0)