//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; } }