Skip to content

Commit ea16513

Browse files
committed
solution for part one
1 parent dfd8cbf commit ea16513

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

com/zayzou/day00/Day01.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.zayzou.day00;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.Collections;
11+
import java.util.Optional;
12+
import java.util.OptionalInt;
13+
import java.util.stream.IntStream;
14+
15+
public class Day01 {
16+
17+
public static void main(String[] args) {
18+
final var FILE = "com/zayzou/day00/input.txt";
19+
System.out.println(sumOfAllValues(FILE));
20+
}
21+
22+
private static int sumOfAllValues(String FILE) {
23+
//use auto close ressource to read the file
24+
try (BufferedReader br = new BufferedReader(new FileReader(FILE))) {
25+
var ligne = "";
26+
int sum = 0;
27+
//while there is new line we go through...
28+
while ((ligne = br.readLine()) != null) {
29+
final var l = ligne;
30+
//look for the first digit
31+
OptionalInt first = ligne.chars()
32+
.filter(Character::isDigit)
33+
.map(Character::getNumericValue)
34+
.findFirst();
35+
36+
37+
//look for the last digit
38+
39+
Optional<Integer> last = IntStream.range(0, ligne.length())
40+
.boxed()
41+
.sorted(Collections.reverseOrder())
42+
.filter(i -> Character.isDigit(l.charAt(i)))
43+
.map(i -> Character.getNumericValue(l.charAt(i)))
44+
.findFirst();
45+
46+
47+
//create the number from the 2 digits
48+
Integer number = Integer.valueOf(first.getAsInt() + "" + last.get());
49+
sum += number;
50+
51+
}
52+
return sum;
53+
} catch (IOException ex) {
54+
throw new RuntimeException(ex.getMessage());
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)