diff --git a/src/Day03.java b/src/Day03.java index 113dc18..17014a3 100644 --- a/src/Day03.java +++ b/src/Day03.java @@ -8,27 +8,191 @@ public class Day03 extends Input { input.init("in/Day03"); } - public int part1_sample(){ + public int part1_sample() { int rValue = 0; - for(String input : this.input.getSample_input()) { + for (String input : this.input.getSample_input()) { rValue += getMul(input); } return rValue; } - public int part1(){ + public int part1() { int rValue = 0; - for(String input : this.input.getInput()) { + for (String input : this.input.getInput()) { rValue += getMul(input); } 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; String pattern = "mul("; String num1_s = ""; diff --git a/src/Main.java b/src/Main.java index 23592ec..5597d71 100644 --- a/src/Main.java +++ b/src/Main.java @@ -69,8 +69,18 @@ public class Main { int day03_p1 = day03.part1(); 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 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); } }