Day 1 Complete

This commit is contained in:
Eoghan Conlon 2024-12-01 23:55:21 +00:00
parent e786ef2c55
commit ecab14b792
2 changed files with 137 additions and 0 deletions

104
src/Day01.java Normal file
View file

@ -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<Integer> list1;
private ArrayList<Integer> 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<Integer, Integer> 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<Integer, Integer> 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;
}
}

33
src/Main.java Normal file
View file

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