day03 #1

Merged
eoghanconlon73 merged 17 commits from day03 into main 2024-12-09 15:05:01 +00:00
Showing only changes of commit 08e3b9b27c - Show all commits

51
src/Day03.java Normal file
View file

@ -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;
}
}