diff --git a/src/Day03.java b/src/Day03.java new file mode 100644 index 0000000..17014a3 --- /dev/null +++ b/src/Day03.java @@ -0,0 +1,239 @@ +import java.io.IOException; + +public class Day03 extends Input { + private Input input; + + public Day03() throws IOException { + input = new Input(); + input.init("in/Day03"); + } + + public int part1_sample() { + int rValue = 0; + + for (String input : this.input.getSample_input()) { + rValue += getMul(input); + } + + return rValue; + } + + public int part1() { + int rValue = 0; + + for (String input : this.input.getInput()) { + rValue += getMul(input); + } + + return rValue; + } + + 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 = ""; + String num2_s = ""; + String nums = "0123456789"; + int num = 1; + int pattern_cnt = 0; + for (int i = 0; i < input.length(); i += 1) { + if (pattern_cnt != pattern.length()) { + if (input.charAt(i) == pattern.charAt(pattern_cnt)) { + pattern_cnt += 1; + } else { + pattern_cnt = 0; + } + } else { + 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; + pattern_cnt = 0; + num1_s = ""; + num2_s = ""; + } else { + num = 1; + pattern_cnt = 0; + num1_s = ""; + num2_s = ""; + } + } + } + return rValue; + } +} diff --git a/src/Main.java b/src/Main.java index eb5a3a4..1d8c4fb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -56,5 +56,32 @@ public class Main { System.out.printf("Part 1 answer = %d (%dms)\n", d2_p1, day02_p1); System.out.printf("Part 2 sample = %d (%dms)\n", d2_p2_sample, day02_p2_sample); System.out.printf("Part 2 answer = %d (%dms)\n", d2_p2, day02_p2); + + System.out.println("\nDay 3:"); + start = System.currentTimeMillis(); + Day03 day03 = new Day03(); + long day03_init = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int day03_p1_sample = day03.part1_sample(); + long day03_p1_sample_t = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + 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 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); } }