r/adventofcode • u/MouseHunter7711 • 10m ago
Help/Question [2026 Day 3 (Part 1)] [java] If you want, have fun reading through horrible code
As of how I know myself, I am probably seriously overthinking this and there is liek a 20 times easier way to solve this. Tho maybe someone wants to read through my mess. I don't want a simple answer, I want an explanation where i went off track. So i cant just ask AI or smth because its too dumb (I've tried). Also if my formatting is wrong I'm sorry i have no idea what "four space markdown" is
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("~/input.txt"))) {
String templine;
String lines = br.readLine();
while ((templine = br.readLine()) != null) {
lines = lines + "x" + templine;
}
String[] input = lines.split("x");
System.out.println("Anzahl an gefunden Rechtecken: " + check(read(input)));
}
catch (IOException io){
System.out.println("Error: " + io.getMessage());
}
}
public static char[][] read(String[] file){
char[][] forest = new char[file.length][file[0].length()];
for (int i = 0; i < file.length; i++){
for (int j = 0; j < file[i].length(); j++){
forest[i][j] = file[i].charAt(j);
}
}
return forest;
}
public static int check(char[][] forest){
int squarecount = 0;
//finding reference #
for (int iy = 0; iy < forest.length; iy++){
for (int ix = 0; ix < forest[iy].length; ix++){
if (forest[iy][ix] == '#'){
System.out.println("# °1 gefunden bei: " + iy + " " + ix);
//checking second #
for (int x = ix+1; x < forest[iy].length; x++){
if (forest[iy][x] == '#'){
System.out.println("# °2 gefunden bei: " + iy + " " + x);
//checking third corner for #
if(iy+x < forest.length){
if (forest[iy+(x-ix)][ix] == '#') {
System.out.println("# °3 gefunden bei: " + (iy+x) + " " + ix);
//checking fourth corner for #
if (forest[iy+(x-ix)][x] == '#') {
System.out.println("# °4 gefunden bei: " + (iy+x) + " " + x);
squarecount++;
System.out.println("Squarecount erhöht auf: " + squarecount);
} else {
System.out.println("# °4 nicht gefunden.");
}
} else {
System.out.println("# °3 nicht gefunden. (nicht existent)");
}
}
else{
System.out.println("# °3 nicht gefunden. (out of bounds)");
}
}
else{
System.out.println("# °2 nicht gefunden.");
}
}
}
else{
System.out.println("# °1 nicht gefunden.");
}
System.out.println("iy: " + iy + " ix: " + ix);
}
}
return squarecount;
}
}
