Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jabrena committed Jan 5, 2025
1 parent 49492a4 commit a7254f5
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 40 deletions.
12 changes: 4 additions & 8 deletions 2015/src/main/java/info/jab/aoc/day10/Day10.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@
public class Day10 implements Day<Integer> {

@Override
public Integer getPart1Result(String fileName) {
String input = "1321131112";
public Integer getPart1Result(String input) {
int iterations = 40;

String result = new LookAndSay3().applyLookAndSay(input, iterations);
String result = new LookAndSay().applyLookAndSay(input, iterations);
return Integer.valueOf(result.length());
}

@Override
public Integer getPart2Result(String fileName) {
String input = "1321131112";
public Integer getPart2Result(String input) {
int iterations = 50;

String result = new LookAndSay3().applyLookAndSay(input, iterations);
String result = new LookAndSay().applyLookAndSay(input, iterations);
return Integer.valueOf(result.length());
}
}
4 changes: 2 additions & 2 deletions 2015/src/main/java/info/jab/aoc/day10/LookAndSay.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ public class LookAndSay {

public String applyLookAndSay(String input, int iterations) {
String sequence = input;

for (int i = 0; i < iterations; i++) {
sequence = generateNextSequence(sequence);
}

return sequence;
}

Expand All @@ -23,6 +21,8 @@ private String generateNextSequence(String sequence) {
count = 1;
}
}

// Append the last run
nextSequence.append(count).append(sequence.charAt(sequence.length() - 1));
return nextSequence.toString();
}
Expand Down
6 changes: 2 additions & 4 deletions 2015/src/main/java/info/jab/aoc/day10/LookAndSay2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
public class LookAndSay2 {

public String applyLookAndSay(String input, int iterations) {
return IntStream.range(0, iterations)
.boxed()
return IntStream.range(0, iterations).boxed()
.reduce(input, (seq, step) -> generateNextSequence(seq), (s1, s2) -> s2);
}

Expand All @@ -24,10 +23,9 @@ private String generateNextSequence(String sequence) {
return result.toString();
}

private static int countConsecutive(String sequence, int endIndex) {
private int countConsecutive(String sequence, int endIndex) {
char currentChar = sequence.charAt(endIndex);
int count = 0;

for (int i = endIndex; i >= 0 && sequence.charAt(i) == currentChar; i--) {
count++;
}
Expand Down
1 change: 0 additions & 1 deletion 2015/src/main/java/info/jab/aoc/day10/LookAndSay3.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public String applyLookAndSay(String sequence, int iterations) {
public String generateNextSequence(String sequence) {
StringBuilder nextSequence = new StringBuilder();
int count = 1;

for (int i = 1; i < sequence.length(); i++) {
if (sequence.charAt(i) == sequence.charAt(i - 1)) {
count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Feature: Look-and-Say Sequence Length
| 21 | 1 | 1211 (1 two, 1 one) | 4 |
| 1211 | 1 | 111221 (1 one, 1 two, 2 ones) | 6 |
| 111221 | 1 | 312211 (3 ones, 2 twos, 1 one) | 6 |
| 1321131112 | 40 | N/A | (final length after 40 steps) |
| 1321131112 | 40 | N/A | (final length after 40 steps) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions 2015/src/test/java/info/jab/aoc/GherkinUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package info.jab.aoc;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import io.cucumber.gherkin.GherkinParser;
import io.cucumber.messages.types.Envelope;
import io.cucumber.messages.types.GherkinDocument;
import io.cucumber.messages.types.Source;
import io.cucumber.messages.types.SourceMediaType;

public class GherkinUtils {

public static GherkinDocument getGherkinDocument(String fileName) {
try {
var feature = Files.readString(Paths.get(fileName));
var envelope = Envelope.of(new Source("minimal.feature", feature, SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN));

return GherkinParser.builder()
.includeSource(false)
.includePickles(false)
.build()
.parse(envelope)
.findFirst().get()
.getGherkinDocument().get();
} catch (IOException e) {
throw new RuntimeException("Error reading file: " + fileName, e);
}
}

}
24 changes: 4 additions & 20 deletions 2015/src/test/java/info/jab/aoc/GherkinValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,16 @@

public class GherkinValidatorTest {

private static final String GHERKIN_DIR = "src/test/gherkin";

@Test
void validateAllGherkinFiles() throws IOException {
try (Stream<Path> paths = Files.walk(Paths.get("src/main/gherkin"))) {
try (Stream<Path> paths = Files.walk(Paths.get(GHERKIN_DIR))) {
paths.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".feature"))
.map(Path::toString)
.peek(System.out::println)
.forEach(this::getGherkinDocument);
.forEach(GherkinUtils::getGherkinDocument);
}
}

private GherkinDocument getGherkinDocument(String fileName) {
try {
var feature = Files.readString(Paths.get(fileName));
var envelope = Envelope.of(new Source("minimal.feature", feature, SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN));

return GherkinParser.builder()
.includeSource(false)
.includePickles(false)
.build()
.parse(envelope)
.findFirst().get()
.getGherkinDocument().get();
} catch (IOException e) {
throw new RuntimeException("Error reading file: " + fileName, e);
}
}

}
14 changes: 10 additions & 4 deletions 2015/src/test/java/info/jab/aoc/day10/Day10Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@

import com.putoet.utils.Timer;

import info.jab.aoc.GherkinUtils;

class Day10Test {

@Test
void should_solve_day10_part1() {
Timer.run(() -> {
//Given
String fileName = "/day9/day9-input.txt";
String input = "1321131112";

//var gherkinFileName = "src/test/gherkin/day10-part1.feature";
//var gherkinDocument = GherkinUtils.getGherkinDocument(gherkinFileName);
//System.out.println(gherkinDocument.getFeature().get().getChildren().get(0));

//When
var day = new Day10();
var result = day.getPart1Result(fileName);
var result = day.getPart1Result(input);

//Then
then(result).isEqualTo(492982);
Expand All @@ -28,11 +34,11 @@ void should_solve_day10_part1() {
void should_solve_day9_part2() {
Timer.run(() -> {
//Given
String fileName = "/day8/day8-input.txt";
String input = "1321131112";

//When
var day = new Day10();
var result = day.getPart2Result(fileName);
var result = day.getPart2Result(input);

//Then
then(result).isEqualTo(6989950);
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Advent of Code is an annual set of Christmas-themed computer programming challen

```bash
java -version
sdk env install
./mvnw clean verify
./mvnw clean verify -T 8C
./mvnw -pl 2015 clean compile -am
Expand Down

0 comments on commit a7254f5

Please sign in to comment.