Skip to content

Commit 7a118ee

Browse files
committed
Add Not Quite Lisp solution for Advent of code 2015, Day 1
1 parent 176a72d commit 7a118ee

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/main/java/by/andd3dfx/game/NotQuiteLisp.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,21 @@
3434
* To what floor do the instructions take Santa?
3535
*
3636
* Your puzzle answer was 74.
37+
*
38+
* --- Part Two ---
39+
*
40+
* Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.
41+
*
42+
* For example:
43+
*
44+
* ) causes him to enter the basement at character position 1.
45+
* ()()) causes him to enter the basement at character position 5.
46+
*
47+
* What is the position of the character that causes Santa to first enter the basement?
3748
* </pre>
3849
*
39-
* @see <a href="https://youtu.be/7En4RJa6384">Video solution</a>
50+
* @see <a href="https://youtu.be/7En4RJa6384">Video solution, Part 1</a>
51+
* @see <a href="https://youtu.be/8YI5GRQTltw">Video solution, Part 2</a>
4052
*/
4153
public class NotQuiteLisp {
4254

@@ -57,11 +69,34 @@ public static int determineFloor(String input) {
5769
return result;
5870
}
5971

72+
public static int determinePosition(String input) {
73+
var floor = 0;
74+
var pos = 1;
75+
for (var ch: input.toCharArray()) {
76+
switch (ch) {
77+
case '(':
78+
floor++;
79+
break;
80+
81+
case ')':
82+
floor--;
83+
if (floor == -1) {
84+
return pos;
85+
}
86+
break;
87+
}
88+
pos++;
89+
}
90+
throw new IllegalArgumentException("Basement was not reached!");
91+
}
92+
6093
@SneakyThrows
6194
public static void main(String[] args) {
6295
var inputString = read("/game/not-quite-lisp.txt");
63-
var result = determineFloor(inputString);
64-
System.out.println(result);
96+
var floor = determineFloor(inputString);
97+
var position = determinePosition(inputString);
98+
99+
System.out.println(floor + ", " + position);
65100
}
66101

67102
private static String read(String filePathName) throws IOException {

src/test/java/by/andd3dfx/game/NotQuiteLispTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ public void determineFloor() {
1717
assertThat(NotQuiteLisp.determineFloor(")))")).isEqualTo(-3);
1818
assertThat(NotQuiteLisp.determineFloor(")())())")).isEqualTo(-3);
1919
}
20+
21+
@Test
22+
public void determinePosition() {
23+
assertThat(NotQuiteLisp.determinePosition(")")).isEqualTo(1);
24+
assertThat(NotQuiteLisp.determinePosition("()())")).isEqualTo(5);
25+
assertThat(NotQuiteLisp.determinePosition("((()()))))))")).isEqualTo(9);
26+
}
2027
}

0 commit comments

Comments
 (0)