diff --git a/src/Day02.java b/src/Day02.java new file mode 100644 index 0000000..3b2af2f --- /dev/null +++ b/src/Day02.java @@ -0,0 +1,166 @@ +import java.io.IOException; +import java.util.ArrayList; + +public class Day02 extends Input{ + private Input input; + + private ArrayList sample; + private ArrayList actual; + + public Day02() throws IOException { + this.input = new Input(); + this.input.init("in/Day02"); + } + + public int part1Sample(){ + int safeReadings = 0; + + this.sample = new ArrayList<>(); + + for(String s1: this.input.getSample_input()){ + String[] teamp_arr = s1.split(" "); + int[] temp_intArr = new int[teamp_arr.length]; + for(int i = 0; i < temp_intArr.length; i += 1){ + temp_intArr[i] = Integer.parseInt(teamp_arr[i]); + } + this.sample.add(temp_intArr); + } + + return p1_work(safeReadings, sample); + } + + public int part1(){ + int safeReadings = 0; + + this.actual = new ArrayList<>(); + + for(String s1: this.input.getInput()){ + String[] teamp_arr = s1.split(" "); + int[] temp_intArr = new int[teamp_arr.length]; + for(int i = 0; i < temp_intArr.length; i += 1){ + temp_intArr[i] = Integer.parseInt(teamp_arr[i]); + } + this.actual.add(temp_intArr); + } + + return p1_work(safeReadings, actual); + } + + private int p1_work(int safeReadings, ArrayList actual) { + for(int[] readings : actual){ + int i = 0; + int j = 1; + boolean increasing = readings[i] < readings[j]; + boolean safe = true; + while(j < readings.length && safe){ + int temp = readings[i] - readings[j]; + if((increasing && ((temp < -3) || (temp > -1))) || (!increasing && ((temp < 1) || (temp > 3)))){ + safe = false; + } + j += 1; + i += 1; + } + if(safe){ + safeReadings += 1; + } + } + + return safeReadings; + } + + public int part2Sample(){ + int safeReadings = 0; + + this.sample = new ArrayList<>(); + + for(String s1: this.input.getSample_input()){ + String[] teamp_arr = s1.split(" "); + int[] temp_intArr = new int[teamp_arr.length]; + for(int i = 0; i < temp_intArr.length; i += 1){ + temp_intArr[i] = Integer.parseInt(teamp_arr[i]); + } + this.sample.add(temp_intArr); + } + + for(int[] readings : this.sample){ + safeReadings = getSafeReadings(safeReadings, readings); + } + + return safeReadings; + } + + public int part2(){ + int safeReadings = 0; + + this.actual = new ArrayList<>(); + + for(String s1: this.input.getInput()){ + String[] teamp_arr = s1.split(" "); + int[] temp_intArr = new int[teamp_arr.length]; + for(int i = 0; i < temp_intArr.length; i += 1){ + temp_intArr[i] = Integer.parseInt(teamp_arr[i]); + } + this.actual.add(temp_intArr); + } + + for(int[] readings : this.actual){ + safeReadings = getSafeReadings(safeReadings, readings); + } + + return safeReadings; + } + + private int getSafeReadings(int safeReadings, int[] readings) { + int safe = 1; + int oldSafeRead = safeReadings; + while(safe >= 0 && oldSafeRead == safeReadings){ + int danger = safe_p2(readings); + if(danger == -1){ + safeReadings += 1; + } else { + safe -= 1; + int[] reading_cut = new int[readings.length - 1]; + while(danger >= 0 && oldSafeRead == safeReadings){ + for(int a = 0, b = 0; a < readings.length; a += 1){ + if(a != danger){ + reading_cut[b] = readings[a]; + b += 1; + } + } + if(safe_p2(reading_cut) == -1){ + safeReadings += 1; + } else { + danger -= 1; + } + } + } + } + return safeReadings; + } + + public int safe_p2(int[] readings){ + int safe = 0; + boolean increasing = readings[0] < readings[1]; + int i = 0; + int j = 1; + while(j < readings.length){ + int diff = readings[i] - readings[j]; + if(increasing && ((diff < -3) || (diff > -1))){ + break; + } else if (!increasing && ((diff < 1) || (diff > 3))) { + break; + } else { + j += 1; + i += 1; + } + } + + if(j < readings.length){ + safe = j; + } else { + safe = -1; + } + + return safe; + } +} diff --git a/src/Main.java b/src/Main.java index 7066f74..eb5a3a4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -28,6 +28,33 @@ public class Main { 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.println("\nDay 2:"); + start = System.currentTimeMillis(); + Day02 day02 = new Day02(); + long day02_init = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int d2_p1_sample = day02.part1Sample(); + long day02_p1_sample = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int d2_p1 = day02.part1(); + long day02_p1 = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int d2_p2_sample = day02.part2Sample(); + long day02_p2_sample = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int d2_p2 = day02.part2(); + long day02_p2 = System.currentTimeMillis() - start; + + System.out.printf("Day 2 init took %dms\n", day02_init); + System.out.printf("Part 1 sample = %d (%dms)\n", d2_p1_sample, day02_p1_sample); + 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); } }