Day 03 p2 complete

Signed-off-by: Eoghan Conlon <git@eoghanconlon.ie>
This commit is contained in:
Eoghan Conlon 2024-12-09 14:58:50 +00:00
parent 67dd1ee1e5
commit f755002b51
2 changed files with 180 additions and 6 deletions

View file

@ -8,27 +8,191 @@ public class Day03 extends Input {
input.init("in/Day03"); input.init("in/Day03");
} }
public int part1_sample(){ public int part1_sample() {
int rValue = 0; int rValue = 0;
for(String input : this.input.getSample_input()) { for (String input : this.input.getSample_input()) {
rValue += getMul(input); rValue += getMul(input);
} }
return rValue; return rValue;
} }
public int part1(){ public int part1() {
int rValue = 0; int rValue = 0;
for(String input : this.input.getInput()) { for (String input : this.input.getInput()) {
rValue += getMul(input); rValue += getMul(input);
} }
return rValue; return rValue;
} }
private int getMul(String input){ public int part2_sample() {
int rValue = 0;
boolean mul = true;
String pattern_mul = "mul(";
String pattern_do = "do()";
String pattern_dont = "don't()";
String nums = "0123456789";
String num1_s = "";
String num2_s = "";
int num = 1;
int mul_cnt = 0;
int do_cnt = 0;
for (String input : this.input.getSample_input()) {
for (int i = 0; i < input.length(); i += 1) {
if (mul) {
if (mul_cnt != pattern_mul.length()) {
if (input.charAt(i) == pattern_mul.charAt(mul_cnt)) {
do_cnt = 0;
mul_cnt += 1;
} else if (input.charAt(i) == pattern_dont.charAt(do_cnt)) {
mul_cnt = 0;
do_cnt += 1;
if (do_cnt == pattern_dont.length()) {
mul = false;
do_cnt = 0;
}
} else {
mul_cnt = 0;
}
} else if (input.charAt(i) == pattern_dont.charAt(do_cnt)) {
mul_cnt = 0;
do_cnt += 1;
if (do_cnt == pattern_dont.length()) {
mul = false;
do_cnt = 0;
}
} else if (mul_cnt == pattern_mul.length()) {
if (nums.contains(Character.toString(input.charAt(i)))) {
if (num == 1) {
num1_s += input.charAt(i);
} else if (num == 2) {
num2_s += input.charAt(i);
} else {
System.err.println("Something went wrong");
}
} else if (input.charAt(i) == ',' && num == 1) {
num += 1;
} else if (input.charAt(i) == ')' && num == 2) {
int num1 = Integer.parseInt(num1_s);
int num2 = Integer.parseInt(num2_s);
rValue += (num1 * num2);
num = 1;
mul_cnt = 0;
num1_s = "";
num2_s = "";
} else {
num = 1;
mul_cnt = 0;
num1_s = "";
num2_s = "";
}
} else {
mul_cnt = 0;
do_cnt = 0;
}
} else {
if (input.charAt(i) == pattern_do.charAt(do_cnt)) {
do_cnt += 1;
if (do_cnt == pattern_do.length()) {
mul = true;
do_cnt = 0;
}
} else {
do_cnt = 0;
}
}
}
}
return rValue;
}
public int part2() {
int rValue = 0;
boolean mul = true;
String pattern_mul = "mul(";
String pattern_do = "do()";
String pattern_dont = "don't()";
String nums = "0123456789";
String num1_s = "";
String num2_s = "";
int num = 1;
int mul_cnt = 0;
int do_cnt = 0;
for (String input : this.input.getInput()) {
for (int i = 0; i < input.length(); i += 1) {
if (mul) {
if (mul_cnt != pattern_mul.length()) {
if (input.charAt(i) == pattern_mul.charAt(mul_cnt)) {
do_cnt = 0;
mul_cnt += 1;
} else if (input.charAt(i) == pattern_dont.charAt(do_cnt)) {
mul_cnt = 0;
do_cnt += 1;
if (do_cnt == pattern_dont.length()) {
mul = false;
do_cnt = 0;
}
} else {
mul_cnt = 0;
}
} else if (input.charAt(i) == pattern_dont.charAt(do_cnt)) {
mul_cnt = 0;
do_cnt += 1;
if (do_cnt == pattern_dont.length()) {
mul = false;
do_cnt = 0;
}
} else if (mul_cnt == pattern_mul.length()) {
if (nums.contains(Character.toString(input.charAt(i)))) {
if (num == 1) {
num1_s += input.charAt(i);
} else if (num == 2) {
num2_s += input.charAt(i);
} else {
System.err.println("Something went wrong");
}
} else if (input.charAt(i) == ',' && num == 1) {
num += 1;
} else if (input.charAt(i) == ')' && num == 2) {
int num1 = Integer.parseInt(num1_s);
int num2 = Integer.parseInt(num2_s);
rValue += (num1 * num2);
num = 1;
mul_cnt = 0;
num1_s = "";
num2_s = "";
} else {
num = 1;
mul_cnt = 0;
num1_s = "";
num2_s = "";
}
} else {
mul_cnt = 0;
do_cnt = 0;
}
} else {
if (input.charAt(i) == pattern_do.charAt(do_cnt)) {
do_cnt += 1;
if (do_cnt == pattern_do.length()) {
mul = true;
do_cnt = 0;
}
} else {
do_cnt = 0;
}
}
}
}
return rValue;
}
private int getMul(String input) {
int rValue = 0; int rValue = 0;
String pattern = "mul("; String pattern = "mul(";
String num1_s = ""; String num1_s = "";

View file

@ -43,8 +43,18 @@ public class Main {
int day03_p1 = day03.part1(); int day03_p1 = day03.part1();
long day03_p1_t = System.currentTimeMillis() - start; long day03_p1_t = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
int day03_p2_sample = day03.part2_sample();
long day03_p2_sample_t = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
int day03_p2 = day03.part2();
long day03_p2_t = System.currentTimeMillis() - start;
System.out.printf("Day 3 init took %dms\n", day03_init); System.out.printf("Day 3 init took %dms\n", day03_init);
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 (sample): %d (%dms)\n", day03_p1_sample, day03_p1_sample_t);
System.out.printf("Day 3 part 2 : %d (%dms)", day03_p1, day03_p1_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);
} }
} }