• # Je me lance ma soluce en Java

    Posté par . En réponse au message Advent of Code 2023 : Day 3. Évalué à 1.

    Un peu long à coder, j'ai passé plus de temps sur la première partie que sur la deuxième partie.

    Partie 1 :

    public class Aoc2023s3p1 {
     public static void main(String[] args) {
     try(Scanner in = new Scanner(Aoc2023s3p1.class.getResourceAsStream("res/Aoc2023s3p1.txt"))) {
     String row;
     int sum =0;
     List<String> rows = new ArrayList<>();
     while(in.hasNext()) {
     row = in.nextLine();
     rows.add(row);
     }
     for(int x=0;x < rows.size();x++) {
     sum += check(x, rows);
     }
     System.out.println(sum);
     }
     }
     public static record Num(int start, int end, String data) {
     }
     public static int check(int rowIndex, List<String> rows) {
     String row = rows.get(rowIndex);
     List<Num> list = new ArrayList<>();
     Integer start = null;
     for(int x=0;x < row.length();x++) {
     int c = row.charAt(x) - '0';
     if(Character.isDigit(row.charAt(x))) {
     if(start == null) {
     start = x;
     }
     } else {
     if(start != null) {
     list.add(new Num(start, x, row.substring(start, x)));
     start = null;
     }
     }
     }
     if(start !=null) {
     list.add(new Num(start, row.length(), row.substring(start)));
     }
     int sum = 0;
     for(Num num : list) {
     System.out.println(num);
     if(checkNum(num, rowIndex, rows)) {
     System.out.println("OK");
     sum += Integer.parseInt(num.data());
     }
     }
     return sum;
     }
     private static boolean checkNum(Num num, int rowIndex, List<String> rows) {
     int before = num.start-1;
     if(checkCharacter(rowIndex, before, rows)) {
     return true; 
     }
     if(checkCharacter(rowIndex, num.end, rows)) {
     return true; 
     }
     for(int x = num.start-1;x < num.end+1;x++) {
     if(checkCharacter(rowIndex - 1, x, rows)
     || checkCharacter(rowIndex + 1, x, rows)) {
     return true;
     }
     }
     return false;
     }
     private static boolean checkCharacter(int rowIndex, int colIndex, List<String> rows) {
     if(rowIndex < 0 || rowIndex >= rows.size()) {
     return false;
     }
     String row = rows.get(rowIndex);
     if(colIndex < 0 || colIndex >= row.length()) {
     return false;
     }
     char c = row.charAt(colIndex);
     if(Character.isDigit(c) || c == '.') {
     return false;
     }
     return true;
     }
    }

    Pour la partie 2, l'idée est assez simple. J'identifie les engrenages '*' par leurs coordonnées et je stocke sur chaque coordonnée la liste des nombres associés (numsById)

    public class Aoc2023s3p2 {
     public static Map<Coord, List<Num>> numsById = new HashMap<>();
     public static void main(String[] args) {
     try(Scanner in = new Scanner(Aoc2023s3p2.class.getResourceAsStream("res/Aoc2023s3p1.txt"))) {
     String row;
     List<String> rows = new ArrayList<>();
     while(in.hasNext()) {
     row = in.nextLine();
     rows.add(row);
     }
     for(int x=0;x < rows.size();x++) {
     check(x, rows);
     }
     int sum =0;
     for(List<Num> nums: numsById.values()) {
     if(nums.size() <= 1) {
     continue;
     }
     System.out.println("=====");
     int prod = 1;
     for(Num num : nums) {
     prod *= Integer.parseInt(num.data);
     System.out.println(num.data);
     }
     System.out.println("=>" + prod);
     sum += prod;
     }
     System.out.println(sum);
     }
     }
     public static record Num(int start, int end, String data) {
     }
     public static void check(int rowIndex, List<String> rows) {
     String row = rows.get(rowIndex);
     List<Num> list = new ArrayList<>();
     Integer start = null;
     for(int x=0;x < row.length();x++) {
     int c = row.charAt(x) - '0';
     if(Character.isDigit(row.charAt(x))) {
     if(start == null) {
     start = x;
     }
     } else {
     if(start != null) {
     list.add(new Num(start, x, row.substring(start, x)));
     start = null;
     }
     }
     }
     if(start !=null) {
     list.add(new Num(start, row.length(), row.substring(start)));
     }
     for(Num num : list) {
     System.out.println(num);
     if(isGearPart(num, rowIndex, rows)) {
     System.out.println("OK");
     }
     }
     }
     private static boolean isGearPart(Num num, int rowIndex, List<String> rows) {
     int before = num.start-1;
     checkGear(num, rowIndex, before, rows);
     checkGear(num, rowIndex, num.end, rows);
     for(int x = num.start-1;x < num.end+1;x++) {
     checkGear(num, rowIndex - 1, x, rows);
     checkGear(num, rowIndex + 1, x, rows);
     }
     return false;
     }
     public static record Coord(int rowIndex, int colIndex) {
     }
     private static boolean checkGear(Num num, int rowIndex, int colIndex, List<String> rows) {
     if(rowIndex < 0 || rowIndex >= rows.size()) {
     return false;
     }
     String row = rows.get(rowIndex);
     if(colIndex < 0 || colIndex >= row.length()) {
     return false;
     }
     char c = row.charAt(colIndex);
     if(c == '*') {
     numsById.computeIfAbsent(new Coord(rowIndex, colIndex),k->new ArrayList<>()).add(num);
     }
     return false;
     }
    }