From ecab14b7920c564d5e29a5ef8df8c29eef6196fb Mon Sep 17 00:00:00 2001 From: Eoghan Conlon Date: Sun, 1 Dec 2024 23:55:21 +0000 Subject: [PATCH] Day 1 Complete --- src/Day01.java | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Main.java | 33 ++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 src/Day01.java create mode 100644 src/Main.java diff --git a/src/Day01.java b/src/Day01.java new file mode 100644 index 0000000..4a01cd4 --- /dev/null +++ b/src/Day01.java @@ -0,0 +1,104 @@ +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; + +public class Day01 extends Input { + private Input input; + + private ArrayList list1; + private ArrayList list2; + + public Day01() throws IOException { + input = new Input(); + input.init("in/Day01"); + } + + public int Part1_sample(){ + int rValue = 0; + this.list1 = new ArrayList<>(); + this.list2 = new ArrayList<>(); + + sampleParse(); + + return p1_work(rValue); + } + + public int Part1(){ + int rValue = 0; + this.list1 = new ArrayList<>(); + this.list2 = new ArrayList<>(); + + actualParse(); + + return p1_work(rValue); + } + + public int Part2_Sample(){ + this.list1 = new ArrayList<>(); + this.list2 = new ArrayList<>(); + HashMap number_count = new HashMap<>(); + int rValue = 0; + + sampleParse(); + + for(int i : this.list1){ + if(!number_count.containsKey(i)){ + number_count.put(i, Collections.frequency(this.list2, i)); + } + rValue += number_count.get(i) * i; + } + + return rValue; + } + + public int Part2(){ + this.list1 = new ArrayList<>(); + this.list2 = new ArrayList<>(); + HashMap number_count = new HashMap<>(); + int rValue = 0; + + actualParse(); + + for(int i : this.list1){ + if(!number_count.containsKey(i)){ + number_count.put(i, Collections.frequency(this.list2, i)); + } + rValue += number_count.get(i) * i; + } + + return rValue; + } + + private void sampleParse(){ + for(String s: input.getSample_input()){ + String[] s_arr = s.split(" {2}"); + this.list1.add(Integer.parseInt(s_arr[0].strip())); + this.list2.add(Integer.parseInt(s_arr[1].strip())); + } + } + + private void actualParse(){ + for(String s: input.getInput()){ + String[] s_arr = s.split(" {2}"); + this.list1.add(Integer.parseInt(s_arr[0].strip())); + this.list2.add(Integer.parseInt(s_arr[1].strip())); + } + } + + private int p1_work(int rValue) { + this.list1.sort(null); + this.list2.sort(null); + + for(int i = 0; i < this.list1.size(); i += 1){ + int temp = this.list1.get(i) - this.list2.get(i); + if(temp < 0){ + temp *= -1; + } + rValue += temp; + } + + return rValue; + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..7066f74 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,33 @@ +import java.io.IOException; + +public class Main { + public static void main(String[] args) throws IOException { + System.out.println("Advent of Code 2024"); + System.out.println("Day 1:"); + long start = System.currentTimeMillis(); + Day01 day01 = new Day01(); + long day01_init = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int sample_d1_p1_answer = day01.Part1_sample(); + long day01_p1_sample = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int d1_p1_answer = day01.Part1(); + long day01_p1 = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int sample_d1_p2_answer = day01.Part2_Sample(); + long day01_p2_sample = System.currentTimeMillis() - start; + + start = System.currentTimeMillis(); + int d1_p2_answer = day01.Part2(); + long day01_p2 = 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); + } +}