Complete day 1
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -63,7 +63,7 @@
|
|||||||
<artifactId>versions-maven-plugin</artifactId>
|
<artifactId>versions-maven-plugin</artifactId>
|
||||||
<version>2.18.0</version>
|
<version>2.18.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<rulesUri>file://${session.executionRootDirectory}/versionRules.xml</rulesUri>
|
<rulesUri>file://${session.executionRootDirectory}/version-rules.xml</rulesUri>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ import java.util.Scanner;
|
|||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
import com.mattrixwv.adventOfCode24.days.Problem;
|
import com.mattrixwv.adventOfCode24.days.Problem;
|
||||||
|
import com.mattrixwv.adventOfCode24.days.Problem1;
|
||||||
|
import com.mattrixwv.adventOfCode24.days.Problem2;
|
||||||
|
|
||||||
|
|
||||||
public class ProblemSelector{
|
public class ProblemSelector{
|
||||||
private static final Scanner input = new Scanner(System.in);
|
private static final Scanner input = new Scanner(System.in);
|
||||||
//Holds the valid problem numbers
|
//Holds the valid problem numbers
|
||||||
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0);
|
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0, 1, 2);
|
||||||
|
|
||||||
|
|
||||||
private ProblemSelector(){
|
private ProblemSelector(){
|
||||||
@@ -24,9 +26,11 @@ public class ProblemSelector{
|
|||||||
public static Problem getProblem(Integer dayNumber){
|
public static Problem getProblem(Integer dayNumber){
|
||||||
Problem day = null;
|
Problem day = null;
|
||||||
switch(dayNumber){
|
switch(dayNumber){
|
||||||
|
case 1 : day = new Problem1(); break;
|
||||||
|
case 2 : day = new Problem2(); break;
|
||||||
default: throw new InvalidParameterException();
|
default: throw new InvalidParameterException();
|
||||||
}
|
}
|
||||||
//return day;
|
return day;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Print the description of a problem
|
//Print the description of a problem
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.mattrixwv.adventOfCode24.days;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import com.mattrixwv.Stopwatch;
|
||||||
|
|
||||||
|
|
||||||
|
public class Problem1 extends Problem{
|
||||||
|
private static final String inputFileName = "days/Problem1.txt";
|
||||||
|
|
||||||
|
|
||||||
|
public Problem1(){
|
||||||
|
super();
|
||||||
|
description = "Find the sum of the differences between the sorted elements of the left and right columns in the file.";
|
||||||
|
result = "Unresolved";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String runSolution(){
|
||||||
|
Stopwatch timer = new Stopwatch();
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
int totalDifference = 0;
|
||||||
|
|
||||||
|
ArrayList<Integer> column1 = new ArrayList<>(1000);
|
||||||
|
ArrayList<Integer> column2 = new ArrayList<>(1000);
|
||||||
|
|
||||||
|
|
||||||
|
//Read elements from the file into memory
|
||||||
|
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||||
|
scanner.useDelimiter("\\s+");
|
||||||
|
|
||||||
|
while(scanner.hasNext()){
|
||||||
|
String token1 = scanner.next();
|
||||||
|
String token2 = scanner.next();
|
||||||
|
|
||||||
|
column1.add(Integer.parseInt(token1));
|
||||||
|
column2.add(Integer.parseInt(token2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sort the two lists of ids
|
||||||
|
Collections.sort(column1);
|
||||||
|
Collections.sort(column2);
|
||||||
|
|
||||||
|
//Get the difference between the corresponding elements in the two lists
|
||||||
|
for(int cnt = 0;cnt < column1.size();++cnt){
|
||||||
|
totalDifference += Math.abs(column1.get(cnt) - column2.get(cnt));
|
||||||
|
}
|
||||||
|
|
||||||
|
//Save the results
|
||||||
|
timer.stop();
|
||||||
|
result = "The sum of the differences of the two columns is " + totalDifference + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Find the sum of the differences between the sorted elements of the left and right columns in the file.
|
||||||
|
The sum of the differences of the two columns is 1666427.
|
||||||
|
It took 10.285 milliseconds to run the algorithm.
|
||||||
|
*/
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package com.mattrixwv.adventOfCode24.days;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import com.mattrixwv.Stopwatch;
|
||||||
|
|
||||||
|
public class Problem2 extends Problem{
|
||||||
|
private static final String inputFileName = "days/Problem1.txt";
|
||||||
|
|
||||||
|
|
||||||
|
public Problem2(){
|
||||||
|
super();
|
||||||
|
description = "Find the similarity score between the two columsn of numbers";
|
||||||
|
result = "Unresolved";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String runSolution(){
|
||||||
|
Stopwatch timer = new Stopwatch();
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
int totalSimilarityScore = 0;
|
||||||
|
|
||||||
|
ArrayList<Integer> column1 = new ArrayList<>(1000);
|
||||||
|
ArrayList<Integer> column2 = new ArrayList<>(1000);
|
||||||
|
|
||||||
|
|
||||||
|
//Read elements from the file into memory
|
||||||
|
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||||
|
scanner.useDelimiter("\\s+");
|
||||||
|
|
||||||
|
while(scanner.hasNext()){
|
||||||
|
String token1 = scanner.next();
|
||||||
|
String token2 = scanner.next();
|
||||||
|
|
||||||
|
column1.add(Integer.parseInt(token1));
|
||||||
|
column2.add(Integer.parseInt(token2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sort the two lists of ids
|
||||||
|
Collections.sort(column1);
|
||||||
|
Collections.sort(column2);
|
||||||
|
|
||||||
|
//Get the similarity score of each elements and keep a running sum
|
||||||
|
//The similarity score is the element in column 1 multiplied by the number of times it appears in column 2
|
||||||
|
for(Integer num : column1){
|
||||||
|
int count = count(num, column2);
|
||||||
|
totalSimilarityScore += (num * count);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Save the results
|
||||||
|
timer.stop();
|
||||||
|
result = "The similarity score between the two columns of numbers is " + totalSimilarityScore + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get the number of times a number appears in the list
|
||||||
|
private int count(int num, List<Integer> list){
|
||||||
|
int cnt = 0;
|
||||||
|
|
||||||
|
for(Integer n : list){
|
||||||
|
if(n == num){
|
||||||
|
++cnt;
|
||||||
|
}
|
||||||
|
if(n > num){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Find the similarity score between the two columns of numbers
|
||||||
|
The similarity score between the two columns of numbers is 24316233.
|
||||||
|
It took 17.875 milliseconds to run the algorithm.
|
||||||
|
*/
|
||||||
1000
src/main/resources/days/Problem1.txt
Normal file
1000
src/main/resources/days/Problem1.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user