Skip to content

Commit

Permalink
Refactor day 1 code
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 2, 2023
1 parent d281d42 commit cfee857
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/main/java/com/adventofcode/flashk/day01/Trebuchet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.adventofcode.flashk.day01;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class Trebuchet {
Expand All @@ -26,25 +28,13 @@ public int solveB() {
}

private int lineValue(String line) {
List<String> result = line.chars()
.filter(Character::isDigit)
.mapToObj(c -> (char) c)
.map(Object::toString)
.toList();

String first = result.get(0);
String last = result.get(result.size()-1);
String digits = StringUtils.getDigits(line);
String number = String.valueOf(digits.charAt(0)) + digits.charAt(digits.length() - 1);

String number = first + last;
return Integer.parseInt(number);
}
private String replaceWordsWithNumber(String line){

String replaceLine = line;
for(int i = 0; i < numberWords.length; i++) {
replaceLine = replaceLine.replace(numberWords[i], numberDigits[i]);
}
return replaceLine;

private String replaceWordsWithNumber(String line){
return StringUtils.replaceEachRepeatedly(line, numberWords, numberDigits);
}
}
}

0 comments on commit cfee857

Please sign in to comment.