Compare commits

..

2 commits
main ... day04

Author SHA1 Message Date
Eoghan Conlon
1d7022288a Day 4 part 1 failing
Signed-off-by: Eoghan Conlon <git@eoghanconlon.ie>
2024-12-12 14:43:54 +00:00
Eoghan Conlon
ac513e807a Day 4 initial commit
Signed-off-by: Eoghan Conlon <git@eoghanconlon.ie>
2024-12-09 15:22:49 +00:00
2 changed files with 284 additions and 1 deletions

266
src/Day04.java Normal file
View file

@ -0,0 +1,266 @@
import java.io.IOException;
import java.util.ArrayList;
public class Day04 extends Input {
private Input input;
private char[][] sample_in;
private char[][] actual_in;
public Day04() throws IOException {
this.input = new Input();
this.input.init("in/Day04");
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) {
String s = this.input.getSample_input().get(i).strip();
for (int j = 0; j < s.length(); j += 1) {
this.sample_in[i][j] = s.charAt(j);
}
}
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) {
String s = this.input.getInput().get(i).strip();
for (int j = 0; j < s.length(); j += 1) {
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

@ -82,6 +82,23 @@ public class Main {
System.out.printf("Day 3 part 1 (sample): %d (%dms)\n", day03_p1_sample, day03_p1_sample_t);
System.out.printf("Day 3 part 1 : %d (%dms)\n", day03_p1, day03_p1_t);
System.out.printf("Day 3 part 2 (sample): %d (%dms)\n", day03_p2_sample, day03_p2_sample_t);
System.out.printf("Day 3 part 2: %d (%dms)", day03_p2, day03_p2_t);
System.out.printf("Day 3 part 2: %d (%dms)\n", day03_p2, day03_p2_t);
System.out.println("\nDay 4:");
start = System.currentTimeMillis();
Day04 day04 = new Day04();
long day04_init = System.currentTimeMillis() - start;
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);
}
}