Skip to content

Commit 38909f8

Browse files
committed
presolution of part 2
1 parent ea16513 commit 38909f8

File tree

3 files changed

+109
-29
lines changed

3 files changed

+109
-29
lines changed

com/zayzou/day00/Day01.java

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,124 @@
77
import java.nio.file.Files;
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
10-
import java.util.Collections;
11-
import java.util.Optional;
12-
import java.util.OptionalInt;
10+
import java.util.*;
1311
import java.util.stream.IntStream;
1412

1513
public class Day01 {
1614

15+
final static HashMap<String, String> digitsMap = new HashMap<>();
16+
final static String FILE = "com/zayzou/day00/input.txt";
17+
1718
public static void main(String[] args) {
18-
final var FILE = "com/zayzou/day00/input.txt";
19-
System.out.println(sumOfAllValues(FILE));
20-
}
19+
digitsMap.put("one", "1");
20+
digitsMap.put("two", "2");
21+
digitsMap.put("three", "3");
22+
digitsMap.put("four", "4");
23+
digitsMap.put("five", "5");
24+
digitsMap.put("six", "6");
25+
digitsMap.put("seven", "7");
26+
digitsMap.put("eight", "8");
27+
digitsMap.put("nine", "9");
2128

22-
private static int sumOfAllValues(String FILE) {
2329
//use auto close ressource to read the file
2430
try (BufferedReader br = new BufferedReader(new FileReader(FILE))) {
25-
var ligne = "";
31+
var line = "";
2632
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-
33+
String first = "";
34+
String last = "";
35+
while ((line = br.readLine()) != null) {
3636

37-
//look for the last digit
37+
final String l = line;
38+
for (int i = 0; i < line.length(); i++) {
39+
final int index = i;
40+
if (Character.isDigit(line.charAt(i))) {
41+
first = String.valueOf(line.charAt(i));
42+
break;
43+
}
44+
Optional<String> firstOptional = digitsMap.keySet().stream()
45+
.filter(key -> key.equals(l.substring(index, index + key.length())))
46+
.findFirst();
47+
if (firstOptional.isPresent()) {
48+
first = digitsMap.get(firstOptional.get());
49+
break;
50+
}
3851

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();
52+
}
4553

54+
for (int i = line.length() - 1; i >= 0; i--) {
55+
final int index = i;
56+
if (Character.isDigit(line.charAt(i))) {
57+
last = String.valueOf(line.charAt(i));
58+
break;
59+
}
60+
Optional<String> lastOptional = digitsMap.keySet().stream()
61+
.filter(ee -> l.lastIndexOf(ee) == ((l.length()) - ee.length()))
62+
.findFirst();
63+
if (lastOptional.isPresent()) {
64+
last = digitsMap.get(lastOptional.get());
65+
break;
66+
}
67+
}
4668

47-
//create the number from the 2 digits
48-
Integer number = Integer.valueOf(first.getAsInt() + "" + last.get());
49-
sum += number;
69+
sum+=Integer.valueOf(first + "" + last);
5070

5171
}
52-
return sum;
72+
System.out.println(sum);
5373
} catch (IOException ex) {
5474
throw new RuntimeException(ex.getMessage());
5575
}
76+
77+
78+
}
79+
80+
private static int sumOfAllValues(String line) {
81+
82+
//look for the first digit
83+
OptionalInt first = line.chars()
84+
.filter(Character::isDigit)
85+
.map(Character::getNumericValue)
86+
.findFirst();
87+
88+
//look for the last digit
89+
Optional<Integer> last = IntStream.range(0, line.length())
90+
.boxed()
91+
.sorted(Collections.reverseOrder())
92+
.filter(i -> Character.isDigit(line.charAt(i)))
93+
.map(i -> Character.getNumericValue(line.charAt(i)))
94+
.findFirst();
95+
96+
//create the number from the 2 digits
97+
Integer number = Integer.valueOf(first.getAsInt() + "" + last.get());
98+
return number;
5699
}
100+
101+
102+
public static void part2(String line) {
103+
//look for the last digit
104+
// Map.Entry<String, String> entry =
105+
// getFirstDigit(line);
106+
//look for the last digit
107+
/* Map.Entry<String, Integer> last = digitsMap.entrySet().stream()
108+
.filter(ee -> line.lastIndexOf(ee.getKey()) == ((line.length()) - ee.getKey().length()))
109+
.findFirst().get();*/
110+
// System.out.println(entry.getValue() + "" + last.getValue());
111+
112+
}
113+
114+
/*
115+
private static Integer getFirstDigit(String line) {
116+
return digitsMap.entrySet().stream()
117+
118+
.filter(ee -> line.indexOf(ee.getKey()) == 0)
119+
.findFirst().get().getValue();
120+
}
121+
*/
122+
123+
/* private static Integer getLastDigit(String line) {
124+
return digitsMap.entrySet().stream()
125+
.filter(ee -> line.lastIndexOf(ee.getKey()) == ((line.length()) - ee.getKey().length()))
126+
.findFirst().get().getValue();
127+
}*/
128+
129+
57130
}

com/zayzou/day00/example.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
1abc2
22
pqr3stu8vwx
33
a1b2c3d4e5f
4-
treb7uchet
4+
treb7uchet

com/zayzou/day00/part2.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
two1nine
2+
eightwothree
3+
abcone2threexyz
4+
xtwone3four
5+
4nineeightseven2
6+
zoneight234
7+
7pqrstsixteen

0 commit comments

Comments
 (0)