fix 1: firstStepl

This commit is contained in:
2024-12-02 18:26:58 -05:00
parent 1c702860be
commit 6a5d9c6e23
8 changed files with 1016 additions and 0 deletions
@@ -0,0 +1,56 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
public class CalibrationPartOne {
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) {
if (line.trim().isEmpty()) {
continue;
}
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();
}
}
}
@@ -0,0 +1,98 @@
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.HashMap;
/**
* 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
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
""";
try {
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
}
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) {
e.printStackTrace();
}
}
}
File diff suppressed because it is too large Load Diff
@@ -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;
}
}
@@ -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