Skip to content

Commit 6ad17a1

Browse files
committed
fun with gatherers
1 parent 2bb2a4f commit 6ad17a1

File tree

6 files changed

+36
-25
lines changed

6 files changed

+36
-25
lines changed

.github/workflows/codacy-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ jobs:
2323
- name: Checkout code
2424
uses: actions/checkout@v2
2525

26-
- name: Set up JDK 21
26+
- name: Set up JDK 24
2727
uses: actions/setup-java@v3
2828
with:
29-
java-version: '21'
29+
java-version: '24'
3030
distribution: 'zulu'
3131
cache: maven
3232
- name: Build with Maven

.github/workflows/codeql-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ jobs:
2929
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection
3030

3131
steps:
32-
- name: Setup Java JDK 21
32+
- name: Setup Java JDK 24
3333
uses: actions/setup-java@v3
3434
with:
35-
java-version: 21
35+
java-version: 24
3636
distribution: 'zulu'
3737

3838
- name: Checkout repository

.github/workflows/maven.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ jobs:
2222
- uses: actions/checkout@v3
2323
- name: Install libncurses5
2424
run: sudo apt-get install -y libncurses5
25-
- name: Set up JDK 21
25+
- name: Set up JDK 24
2626
uses: actions/setup-java@v3
2727
with:
28-
java-version: '21'
28+
java-version: '24'
2929
distribution: 'zulu'
3030
cache: maven
3131
- name: Build with Maven

adventofcode/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14-
<java.version>21</java.version>
14+
<java.version>24</java.version>
1515
<compiler.version>3.11.0</compiler.version>
1616
<surefire.version>3.1.2</surefire.version>
1717
<spottless.version>2.43.0</spottless.version>
Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.ck.adventofcode.year2015;
22

33
import java.util.Scanner;
4-
import java.util.function.IntBinaryOperator;
54
import java.util.function.IntPredicate;
5+
import java.util.function.ToIntFunction;
6+
import java.util.stream.Gatherer;
67
import org.ck.adventofcode.util.AOCSolution;
78
import org.ck.codechallengelib.annotation.Solution;
89

@@ -19,31 +20,41 @@
1920
public class Day01 extends AOCSolution {
2021
@Override
2122
protected void runPartOne(final Scanner in) {
22-
run(in, (floor) -> false, (floor, position) -> floor);
23+
run(in, _ -> true, state -> state.floor);
2324
}
2425

2526
@Override
2627
protected void runPartTwo(final Scanner in) {
27-
run(in, (floor) -> floor < 0, (floor, position) -> position);
28+
run(in, floor -> floor >= 0, state -> state.position - 1);
2829
}
2930

3031
private void run(
31-
final Scanner in, final IntPredicate getBreakCondition, final IntBinaryOperator getResult) {
32-
int floor = 0;
33-
int position = 1;
34-
35-
final String path = in.nextLine();
36-
37-
for (char command : path.toCharArray()) {
38-
floor += command == '(' ? 1 : -1;
32+
final Scanner in,
33+
final IntPredicate getBreakCondition,
34+
final ToIntFunction<State> getResult) {
35+
in.nextLine()
36+
.chars()
37+
.boxed()
38+
.gather(
39+
Gatherer.ofSequential(
40+
() -> new State(0, 1),
41+
(state, element, _) -> {
42+
state.floor += element == '(' ? 1 : -1;
43+
state.position += 1;
44+
45+
return getBreakCondition.test(state.floor);
46+
},
47+
(state, downstream) -> downstream.push(getResult.applyAsInt(state))))
48+
.forEach(this::print);
49+
}
3950

40-
if (getBreakCondition.test(floor)) {
41-
break;
42-
}
51+
private static final class State {
52+
private int floor;
53+
private int position;
4354

44-
++position;
55+
public State(final int floor, final int position) {
56+
this.floor = floor;
57+
this.position = position;
4558
}
46-
47-
print(getResult.applyAsInt(floor, position));
4859
}
4960
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<java.version>21</java.version>
13+
<java.version>24</java.version>
1414
<compiler.version>3.11.0</compiler.version>
1515
<surefire.version>3.1.2</surefire.version>
1616
<spottless.version>2.40.0</spottless.version>

0 commit comments

Comments
 (0)