From fe6f19ef52b07684c256398feb5c81b6b76ac6ea Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Mon, 9 Dec 2024 11:59:50 +0000 Subject: [PATCH] Day03 initial commit Signed-off-by: Eoghan Conlon --- src/Day03.java | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Main.java | 13 ++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/Day03.java diff --git a/src/Day03.java b/src/Day03.java new file mode 100644 index 0000000..cc55cdd --- /dev/null +++ b/src/Day03.java @@ -0,0 +1,51 @@ +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; + + String input = this.input.getSample_input().getFirst(); + 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; + } + } + } + + return rValue; + } +} diff --git a/src/Main.java b/src/Main.java index 7066f74..be8e5d9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -24,10 +24,21 @@ public class Main { int d1_p2_answer = day01.Part2(); long day01_p2 = System.currentTimeMillis() - start; + 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; + System.out.printf("Day 1 init took %dms", day01_init); System.out.printf("Part 1 sample is: %d (%dms)\n", sample_d1_p1_answer, day01_p1_sample); System.out.printf("Part 1 answer is: %d (%dms)\n", d1_p1_answer, day01_p1); System.out.printf("Part 2 sample answer is: %d (%dms)\n", sample_d1_p2_answer, day01_p2_sample); - System.out.printf("Part 2 answer is: %d (%dms)", d1_p2_answer, day01_p2); + System.out.printf("Part 2 answer is: %d (%dms)\n", d1_p2_answer, day01_p2); + + 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); } }