|
7 | 7 | import java.nio.file.Files;
|
8 | 8 | import java.nio.file.Path;
|
9 | 9 | import java.nio.file.Paths;
|
10 |
| -import java.util.Collections; |
11 |
| -import java.util.Optional; |
12 |
| -import java.util.OptionalInt; |
| 10 | +import java.util.*; |
13 | 11 | import java.util.stream.IntStream;
|
14 | 12 |
|
15 | 13 | public class Day01 {
|
16 | 14 |
|
| 15 | + final static HashMap<String, String> digitsMap = new HashMap<>(); |
| 16 | + final static String FILE = "com/zayzou/day00/input.txt"; |
| 17 | + |
17 | 18 | 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"); |
21 | 28 |
|
22 |
| - private static int sumOfAllValues(String FILE) { |
23 | 29 | //use auto close ressource to read the file
|
24 | 30 | try (BufferedReader br = new BufferedReader(new FileReader(FILE))) {
|
25 |
| - var ligne = ""; |
| 31 | + var line = ""; |
26 | 32 | 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) { |
36 | 36 |
|
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 | + } |
38 | 51 |
|
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 | + } |
45 | 53 |
|
| 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 | + } |
46 | 68 |
|
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); |
50 | 70 |
|
51 | 71 | }
|
52 |
| - return sum; |
| 72 | + System.out.println(sum); |
53 | 73 | } catch (IOException ex) {
|
54 | 74 | throw new RuntimeException(ex.getMessage());
|
55 | 75 | }
|
| 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; |
56 | 99 | }
|
| 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 | + |
57 | 130 | }
|
0 commit comments