From 1d7022288a5066d5b37ba593750f705aacb11ab1 Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Thu, 12 Dec 2024 14:43:54 +0000 Subject: [PATCH] Day 4 part 1 failing Signed-off-by: Eoghan Conlon --- src/Day04.java | 249 ++++++++++++++++++++++++++++++++++++++++++++++++- src/Main.java | 12 ++- 2 files changed, 255 insertions(+), 6 deletions(-) diff --git a/src/Day04.java b/src/Day04.java index 41f23db..9621783 100644 --- a/src/Day04.java +++ b/src/Day04.java @@ -1,6 +1,7 @@ import java.io.IOException; +import java.util.ArrayList; -public class Day04 extends Input{ +public class Day04 extends Input { private Input input; private char[][] sample_in; @@ -10,18 +11,256 @@ public class Day04 extends Input{ 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){ + 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){ + 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){ + 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){ + 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; + } } diff --git a/src/Main.java b/src/Main.java index 6e7ae51..18ed1c9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -89,6 +89,16 @@ public class Main { Day04 day04 = new Day04(); 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); } }