From c5d08570e8b51059edb967467aea13e514142ff4 Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Mon, 9 Dec 2024 12:07:00 +0000 Subject: [PATCH] Day03 p1 Feat. Part 1 solution complete Signed-off-by: Eoghan Conlon --- src/Day03.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Main.java | 5 +++++ 2 files changed, 54 insertions(+) diff --git a/src/Day03.java b/src/Day03.java index b20a2e4..43cfbfc 100644 --- a/src/Day03.java +++ b/src/Day03.java @@ -56,4 +56,53 @@ public class Day03 extends Input { return rValue; } + + public int part1(){ + int rValue = 0; + + for(String input : this.input.getInput()) { + 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 be8e5d9..c3df515 100644 --- a/src/Main.java +++ b/src/Main.java @@ -32,6 +32,10 @@ public class Main { 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; + 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); @@ -40,5 +44,6 @@ public class Main { 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); } }