From 67dd1ee1e5dc6f970a73f054d6ba1bff225f8e1a Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Mon, 9 Dec 2024 13:27:31 +0000 Subject: [PATCH] Day 03 p1 Reafctored code to split logic into function to reuse for p2 Signed-off-by: Eoghan Conlon --- src/Day03.java | 127 ++++++++++++++++++------------------------------- 1 file changed, 47 insertions(+), 80 deletions(-) diff --git a/src/Day03.java b/src/Day03.java index 43cfbfc..113dc18 100644 --- a/src/Day03.java +++ b/src/Day03.java @@ -12,46 +12,7 @@ public class Day03 extends Input { int rValue = 0; for(String input : this.input.getSample_input()) { - 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 = ""; - } - } - } + rValue += getMul(input); } return rValue; @@ -61,48 +22,54 @@ public class Day03 extends Input { 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 = ""; - } - } - } + rValue += getMul(input); } 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; + } }