fix: another Account, Part two

This commit is contained in:
dadgam3er 2024-09-13 16:35:29 -04:00
parent da3bf82579
commit 1c702860be
4 changed files with 1268 additions and 20 deletions

View File

@ -1,10 +1,30 @@
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
/**
* CalibrationPartTwo
*/
public class CalibrationPartTwo {
public static void main(String[] args) {
// HashMap to map word representations of digits to their corresponding numeric
// values
HashMap<String, Integer> stringToDigit = new HashMap<>();
stringToDigit.put("one", 1);
stringToDigit.put("two", 2);
stringToDigit.put("three", 3);
stringToDigit.put("four", 4);
stringToDigit.put("five", 5);
stringToDigit.put("six", 6);
stringToDigit.put("seven", 7);
stringToDigit.put("eight", 8);
stringToDigit.put("nine", 9);
// String input
String str = """
two1nine
two1nine
eightwothree
abcone2threexyz
xtwone3four
@ -12,30 +32,67 @@ public class CalibrationPartTwo {
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;
BufferedReader br = new BufferedReader(new StringReader(str));
String line;
int total = 0;
while ((line = br.readLine()) != null) {
if (line.trim().isEmpty()) {
continue; // Skip empty lines
}
for (String c : line.split("")) {
System.out.println(c);
StringBuilder wordBuffer = new StringBuilder();
int firstDigit = -1;
int lastDigit = -1;
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
// Build a word if we encounter alphabetic characters
if (Character.isLetter(c)) {
wordBuffer.append(c);
String currentWord = wordBuffer.toString();
// Check if the current word matches a digit word
if (stringToDigit.containsKey(currentWord)) {
int num = stringToDigit.get(currentWord);
if (firstDigit == -1) {
firstDigit = num;
}
lastDigit = num;
wordBuffer.setLength(0); // Clear the buffer after processing the word
}
}
// If we encounter a digit, process it directly
else if (Character.isDigit(c)) {
int num = Character.getNumericValue(c);
if (firstDigit == -1) {
firstDigit = num;
}
lastDigit = num;
}
// If we encounter a non-alphabetic, non-digit character, reset the word buffer
else {
wordBuffer.setLength(0);
}
}
// Combine the first and last digits
if (firstDigit != -1 && lastDigit != -1) {
int sum = (firstDigit * 10) + lastDigit;
System.out.println(sum);
total += sum;
}
}
// Output the final total
System.out.println("Total for Part Two = " + total);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,82 @@
//import java.io.BufferedReader;
//import java.io.FileReader;
//import java.io.StringReader;
//
//public class TrebuchetPartOne {
// public static void main(String[] args) {
// String str = """
// 1abc2
// pqr3stu8vwx
// a1b2c3d4e5f
//
// treb7uchet
// """;
//
// try {
// String line;
// int total = 0;
// // BufferedReader br = new BufferedReader(new StringReader(str));
// BufferedReader br = new BufferedReader(new FileReader("/home/ilyes/AdventOfCode/Trebuchet/snowyCoordinates.txt"));
// while ((line = br.readLine()) != null) {
// if (line.trim().isEmpty()) {
// continue;
// }
// int first = -1;
// int last = -1;
// for (String s : line.split("")) {
// if (Character.isDigit(s.charAt(0))) {
// if (first == -1) {
// first = Integer.parseInt(s);
// }
// last = Integer.parseInt(s);
// }
// }
// first = first * 10;
// total += first + last;
// }
// System.out.println(total);
// br.close();
// } catch (Exception e) {
// // TODO: handle exception
// }
// }
//}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class TrebuchetPartOne {
public static void main(String[] args) {
String filePath = "/home/ilyes/AdventOfCode/Trebuchet/snowyCoordinates.txt";
try {
int total = Files.lines(Paths.get(filePath))
.filter(line -> !line.trim().isEmpty())
.mapToInt(TrebuchetPartOne::calculateCalibrationValue)
.sum();
System.out.println("Total calibration value: " + total);
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
}
private static int calculateCalibrationValue(String line) {
int first = -1;
int last = -1;
for (char c : line.toCharArray()) {
if (Character.isDigit(c)) {
last = Character.getNumericValue(c);
if (first == -1) {
first = last;
}
}
}
return (first != -1) ? first * 10 + last : 0;
}
}

View File

@ -0,0 +1,95 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
public class TrebuchetPartTwo {
public static void main(String[] args) {
// Mapping of words to string digits
HashMap<String, String> stringToDigit = new HashMap<>();
stringToDigit.put("one", "1");
stringToDigit.put("two", "2");
stringToDigit.put("three", "3");
stringToDigit.put("four", "4");
stringToDigit.put("five", "5");
stringToDigit.put("six", "6");
stringToDigit.put("seven", "7");
stringToDigit.put("eight", "8");
stringToDigit.put("nine", "9");
// Sample string input
String str = """
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
""";
try {
String line;
int total = 0;
// Read the string line by line
BufferedReader br = new BufferedReader(new FileReader("/home/ilyes/AdventOfCode/Trebuchet/snowyCoordinates.txt"));
// // stringreader(str)
while ((line = br.readLine()) != null) {
// Skip empty lines
if (line.trim().isEmpty()) {
continue;
}
String firstDigit = null;
String lastDigit = null;
// Find the first digit
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (Character.isDigit(c)) {
firstDigit = String.valueOf(c);
break;
}
for (Map.Entry<String, String> entry : stringToDigit.entrySet()) {
if (line.startsWith(entry.getKey(), i)) {
firstDigit = entry.getValue();
break;
}
}
if (firstDigit != null)
break;
}
// Find the last digit
for (int i = line.length() - 1; i >= 0; i--) {
char c = line.charAt(i);
if (Character.isDigit(c)) {
lastDigit = String.valueOf(c);
break;
}
for (Map.Entry<String, String> entry : stringToDigit.entrySet()) {
if (line.startsWith(entry.getKey(), i)) {
lastDigit = entry.getValue();
break;
}
}
if (lastDigit != null)
break;
}
// Combine first and last digits if found
if (firstDigit != null && lastDigit != null) {
int sum = (Integer.parseInt(firstDigit) * 10) + Integer.parseInt(lastDigit);
total += sum;
}
}
// Output the total sum
System.out.println("Total for Part Two = " + total);
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

File diff suppressed because it is too large Load Diff