Day 4 part 1 failing

Signed-off-by: Eoghan Conlon <git@eoghanconlon.ie>
This commit is contained in:
Eoghan Conlon 2024-12-12 14:43:54 +00:00
parent ac513e807a
commit 1d7022288a
2 changed files with 255 additions and 6 deletions

View file

@ -1,6 +1,7 @@
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
public class Day04 extends Input{ public class Day04 extends Input {
private Input input; private Input input;
private char[][] sample_in; private char[][] sample_in;
@ -10,18 +11,256 @@ public class Day04 extends Input{
this.input = new Input(); this.input = new Input();
this.input.init("in/Day04"); this.input.init("in/Day04");
this.sample_in = new char[this.input.getSample_input().size()][this.input.getSample_input().getFirst().length()]; this.sample_in = new char[this.input.getSample_input().size()][this.input.getSample_input().getFirst().length()];
for(int i = 0; i < this.sample_in.length; i += 1){ for (int i = 0; i < this.sample_in.length; i += 1) {
String s = this.input.getSample_input().get(i).strip(); String s = this.input.getSample_input().get(i).strip();
for(int j = 0; j < s.length(); j += 1){ for (int j = 0; j < s.length(); j += 1) {
this.sample_in[i][j] = s.charAt(j); this.sample_in[i][j] = s.charAt(j);
} }
} }
this.actual_in = new char[this.input.getInput().size()][this.input.getInput().getFirst().length()]; this.actual_in = new char[this.input.getInput().size()][this.input.getInput().getFirst().length()];
for(int i = 0; i < this.actual_in.length; i += 1){ for (int i = 0; i < this.actual_in.length; i += 1) {
String s = this.input.getInput().get(i).strip(); String s = this.input.getInput().get(i).strip();
for(int j = 0; j < s.length(); j += 1){ for (int j = 0; j < s.length(); j += 1) {
this.actual_in[i][j] = s.charAt(j); this.actual_in[i][j] = s.charAt(j);
} }
} }
} }
public int part1_sample() {
int xmas = 0;
int found = 0;
int temp_i, temp_j;
String xmas_str = "XMAS";
for(int i = 0; i < this.sample_in.length; i += 1){
for(int j = 0; j < this.sample_in[i].length; j += 1){
found = 0;
if(sample_in[i][j] == 'X'){
found += 1;
if(i >= 3){
temp_i = i - 1;
if(j >= 3){
temp_j = j - 1;
while(xmas_str.charAt(found) == this.sample_in[temp_i][temp_j]){
found += 1;
temp_i -= 1;
temp_j -= 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
temp_i = i - 1;
if(j < this.sample_in[i].length - 4){
temp_j = j + 1;
while(xmas_str.charAt(found) == this.sample_in[temp_i][temp_j]){
found += 1;
temp_i -= 1;
temp_j += 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
temp_i = i - 1;
while(xmas_str.charAt(found) == this.sample_in[temp_i][j]){
found += 1;
temp_i -= 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
if(i <= this.sample_in.length - 4){
temp_i = i + 1;
if(j >= 3){
temp_j = j - 1;
while(xmas_str.charAt(found) == this.sample_in[temp_i][temp_j]){
found += 1;
temp_i += 1;
temp_j -= 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
temp_i = i + 1;
if(j <= this.sample_in[i].length - 4){
temp_j = j + 1;
while(xmas_str.charAt(found) == this.sample_in[temp_i][temp_j]){
found += 1;
temp_i += 1;
temp_j += 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
temp_i = i + 1;
while(xmas_str.charAt(found) == this.sample_in[temp_i][j]){
found += 1;
temp_i += 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
if(j >= 3){
temp_j = j - 1;
while(xmas_str.charAt(found) == this.sample_in[i][temp_j]){
found += 1;
temp_j -= 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
if(j <= this.sample_in[i].length - 4){
temp_j = j += 1;
while(xmas_str.charAt(found) == this.sample_in[i][temp_j]){
found += 1;
temp_j += 1;
if(found == 4){
xmas += 1;
break;
}
}
}
}
}
}
return xmas;
}
public int part1() {
int xmas = 0;
int found = 0;
int temp_i, temp_j;
String xmas_str = "XMAS";
for(int i = 0; i < this.actual_in.length; i += 1){
for(int j = 0; j < this.actual_in[i].length; j += 1){
found = 0;
if(actual_in[i][j] == 'X'){
found += 1;
if(i >= 3){
temp_i = i - 1;
if(j >= 3){
temp_j = j - 1;
while(xmas_str.charAt(found) == this.actual_in[temp_i][temp_j]){
found += 1;
temp_i -= 1;
temp_j -= 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
temp_i = i - 1;
if(j <= this.actual_in[i].length - 4){
temp_j = j + 1;
while(xmas_str.charAt(found) == this.actual_in[temp_i][temp_j]){
found += 1;
temp_i -= 1;
temp_j += 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
temp_i = i - 1;
while(xmas_str.charAt(found) == this.actual_in[temp_i][j]){
found += 1;
temp_i -= 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
if(i <= this.actual_in.length - 4){
temp_i = i + 1;
if(j >= 3){
temp_j = j - 1;
while(xmas_str.charAt(found) == this.actual_in[temp_i][temp_j]){
found += 1;
temp_i += 1;
temp_j -= 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
temp_i = i + 1;
if(j <= this.actual_in[i].length - 4){
temp_j = j + 1;
while(xmas_str.charAt(found) == this.actual_in[temp_i][temp_j]){
found += 1;
temp_i += 1;
temp_j += 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
temp_i = i + 1;
while(xmas_str.charAt(found) == this.actual_in[temp_i][j]){
found += 1;
temp_i += 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
if(j >= 3){
temp_j = j - 1;
while(xmas_str.charAt(found) == this.actual_in[i][temp_j]){
found += 1;
temp_j -= 1;
if(found == 4){
xmas += 1;
break;
}
}
found = 1;
}
if(j <= this.actual_in[i].length - 4){
temp_j = j += 1;
while(xmas_str.charAt(found) == this.actual_in[i][temp_j]){
found += 1;
temp_j += 1;
if(found == 4){
xmas += 1;
break;
}
}
}
}
}
}
return xmas;
}
} }

View file

@ -89,6 +89,16 @@ public class Main {
Day04 day04 = new Day04(); Day04 day04 = new Day04();
long day04_init = System.currentTimeMillis() - start; long day04_init = System.currentTimeMillis() - start;
System.out.printf("Day 4 init took %dms", day04_init); start = System.currentTimeMillis();
int day04_p1_sample = day04.part1_sample();
long day04_p1_sample_t = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
int day04_p1 = day04.part1();
long day04_p1_t = System.currentTimeMillis() - start;
System.out.printf("Day 4 init took %dms\n", day04_init);
System.out.printf("Day 4 part 1 (sample): %d (%dms)\n", day04_p1_sample, day04_p1_sample_t);
System.out.printf("Day 4 part 1: %d (%dms)", day04_p1, day04_p1_t);
} }
} }