fix: 1
This commit is contained in:
commit
9c5889863d
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
out/
|
||||||
|
.idea
|
||||||
|
AdventOfCode.iml
|
||||||
|
|
||||||
53
Trebuchet/CalibrationPartOne.java
Normal file
53
Trebuchet/CalibrationPartOne.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
|
public class Calibration {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String coor = """
|
||||||
|
1abc2
|
||||||
|
pqr3stu8vwx
|
||||||
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
||||||
|
""";
|
||||||
|
|
||||||
|
int total = 0;
|
||||||
|
try (
|
||||||
|
// let's first remove all the spaced lines and go through char by char
|
||||||
|
BufferedReader br = new BufferedReader(new FileReader("/home/ilyes/AdventOfCode/Trebuchet/Coordinates.txt"))) {
|
||||||
|
// BufferedReader br = new BufferedReader(new StringReader(coor))) {
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
int first = -1;
|
||||||
|
int last = -1;
|
||||||
|
for (String c : line.split("")) {
|
||||||
|
// check for digits
|
||||||
|
if (Character.isDigit(c.charAt(0))) {
|
||||||
|
if (first == -1) {
|
||||||
|
// we add the first digit
|
||||||
|
first = Integer.parseInt(c);
|
||||||
|
}
|
||||||
|
// we add the last digit
|
||||||
|
last = Integer.parseInt(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// let's combine the first and the last digits then we do the addition (12 + 38
|
||||||
|
// + 15 + 77 = 142)
|
||||||
|
first = first * 10;
|
||||||
|
total += first + last;
|
||||||
|
// System.out.println(first + "" + last);
|
||||||
|
// the output would be
|
||||||
|
// 12
|
||||||
|
// 38
|
||||||
|
// 15
|
||||||
|
// 77 resulting in total of 142
|
||||||
|
}
|
||||||
|
System.out.println(total);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
Trebuchet/CalibrationPartTwo.java
Normal file
41
Trebuchet/CalibrationPartTwo.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CalibrationPartTwo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str = """
|
||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
||||||
|
""";
|
||||||
|
HahsMap<String, String> elements = new HashMap<>();
|
||||||
|
elements.put("one", "1");
|
||||||
|
elements.put("two", "2");
|
||||||
|
elements.put("three", "3");
|
||||||
|
elements.put("four", "4");
|
||||||
|
elements.put("five", "5");
|
||||||
|
elements.put("six", "6");
|
||||||
|
elements.put("seven", "7");
|
||||||
|
elements.put("eight", "8");
|
||||||
|
elements.put("nine", "9");
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (String line : str.split("\n")) {
|
||||||
|
for (Map.Entry<String, String> entry : elements.entrySet()) {
|
||||||
|
line.replace(entry.getKey(), entry.getValue());
|
||||||
|
int first = -1;
|
||||||
|
int last = -1;
|
||||||
|
}
|
||||||
|
for (String c : line.split("")) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO: handle exception
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1000
Trebuchet/Coordinates.txt
Normal file
1000
Trebuchet/Coordinates.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user