Complete day 9
This commit is contained in:
@@ -15,6 +15,8 @@ import com.mattrixwv.adventOfCode24.days.Problem13;
|
|||||||
import com.mattrixwv.adventOfCode24.days.Problem14;
|
import com.mattrixwv.adventOfCode24.days.Problem14;
|
||||||
import com.mattrixwv.adventOfCode24.days.Problem15;
|
import com.mattrixwv.adventOfCode24.days.Problem15;
|
||||||
import com.mattrixwv.adventOfCode24.days.Problem16;
|
import com.mattrixwv.adventOfCode24.days.Problem16;
|
||||||
|
import com.mattrixwv.adventOfCode24.days.Problem17;
|
||||||
|
import com.mattrixwv.adventOfCode24.days.Problem18;
|
||||||
import com.mattrixwv.adventOfCode24.days.Problem2;
|
import com.mattrixwv.adventOfCode24.days.Problem2;
|
||||||
import com.mattrixwv.adventOfCode24.days.Problem3;
|
import com.mattrixwv.adventOfCode24.days.Problem3;
|
||||||
import com.mattrixwv.adventOfCode24.days.Problem4;
|
import com.mattrixwv.adventOfCode24.days.Problem4;
|
||||||
@@ -30,7 +32,7 @@ public class ProblemSelector{
|
|||||||
//Holds the valid problem numbers
|
//Holds the valid problem numbers
|
||||||
protected static final List<Integer> PROBLEM_NUMBERS = List.of(
|
protected static final List<Integer> PROBLEM_NUMBERS = List.of(
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
10, 11, 12, 13, 14, 15, 16
|
10, 11, 12, 13, 14, 15, 16, 17, 18
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
@@ -59,6 +61,8 @@ public class ProblemSelector{
|
|||||||
case 14 : day = new Problem14(); break;
|
case 14 : day = new Problem14(); break;
|
||||||
case 15 : day = new Problem15(); break;
|
case 15 : day = new Problem15(); break;
|
||||||
case 16 : day = new Problem16(); break;
|
case 16 : day = new Problem16(); break;
|
||||||
|
case 17 : day = new Problem17(); break;
|
||||||
|
case 18 : day = new Problem18(); break;
|
||||||
default: throw new InvalidParameterException();
|
default: throw new InvalidParameterException();
|
||||||
}
|
}
|
||||||
return day;
|
return day;
|
||||||
|
|||||||
119
src/main/java/com/mattrixwv/adventOfCode24/days/Problem17.java
Normal file
119
src/main/java/com/mattrixwv/adventOfCode24/days/Problem17.java
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package com.mattrixwv.adventOfCode24.days;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import com.mattrixwv.Stopwatch;
|
||||||
|
|
||||||
|
public class Problem17 extends Problem{
|
||||||
|
private static final String inputFileName = "days/Problem17.txt";
|
||||||
|
private String rawInput;
|
||||||
|
private List<Integer> prettyInput;
|
||||||
|
|
||||||
|
|
||||||
|
public Problem17(){
|
||||||
|
super();
|
||||||
|
description = "Find the filesystem checksum after compacting the files.";
|
||||||
|
result = "Unresolved";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String runSolution(){
|
||||||
|
Stopwatch timer = new Stopwatch();
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
|
||||||
|
//Read the file
|
||||||
|
/* */
|
||||||
|
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||||
|
scanner.useDelimiter("\n");
|
||||||
|
rawInput = scanner.next();
|
||||||
|
}
|
||||||
|
/* */
|
||||||
|
/*
|
||||||
|
rawInput = "2333133121414131402";
|
||||||
|
/* */
|
||||||
|
|
||||||
|
//Change the raw into into pretty input
|
||||||
|
generatePrettyInput();
|
||||||
|
|
||||||
|
//printPrettyInput();
|
||||||
|
|
||||||
|
//Compact the files
|
||||||
|
compactInput();
|
||||||
|
|
||||||
|
//printPrettyInput();
|
||||||
|
|
||||||
|
//Generate sum
|
||||||
|
long sum = generateCheckSum();
|
||||||
|
|
||||||
|
|
||||||
|
//Save the results
|
||||||
|
timer.stop();
|
||||||
|
result = "The filesystem checksum is " + sum + ".\nIt took " + timer.toString() + " to run this algorithm.";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generatePrettyInput(){
|
||||||
|
prettyInput = new ArrayList<>();
|
||||||
|
int fileNumber = 0;
|
||||||
|
boolean isFile = true;
|
||||||
|
|
||||||
|
for(int loc = 0;loc < rawInput.length();++loc){
|
||||||
|
//Get the next number
|
||||||
|
int num = Integer.parseInt(rawInput.charAt(loc) + "");
|
||||||
|
//If this number represents a file size then add the file number to the pretty input
|
||||||
|
if(isFile){
|
||||||
|
for(int cnt = 0;cnt < num;++cnt){
|
||||||
|
prettyInput.add(fileNumber);
|
||||||
|
}
|
||||||
|
++fileNumber;
|
||||||
|
}
|
||||||
|
//If this number represents free space then add -1 to the pretty input
|
||||||
|
else{
|
||||||
|
for(int cnt = 0;cnt < num;++cnt){
|
||||||
|
prettyInput.add(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
isFile = !isFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void compactInput(){
|
||||||
|
int forwardCnt = 0;
|
||||||
|
int backwardCnt = prettyInput.size() - 1;
|
||||||
|
while(forwardCnt < backwardCnt){
|
||||||
|
if(prettyInput.get(forwardCnt) != -1){
|
||||||
|
++forwardCnt;
|
||||||
|
}
|
||||||
|
else if(prettyInput.get(backwardCnt) == -1){
|
||||||
|
--backwardCnt;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
Integer temp = prettyInput.get(backwardCnt);
|
||||||
|
prettyInput.set(backwardCnt, -1);
|
||||||
|
prettyInput.set(forwardCnt, temp);
|
||||||
|
--backwardCnt;
|
||||||
|
++forwardCnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private long generateCheckSum(){
|
||||||
|
long sum = 0;
|
||||||
|
for(int loc = 0;loc < prettyInput.size();++loc){
|
||||||
|
if(prettyInput.get(loc) != -1){
|
||||||
|
sum += (loc * prettyInput.get(loc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Find the filesystem checksum after compacting the files.
|
||||||
|
The filesystem checksum is 6370402949053.
|
||||||
|
It took 23.980 milliseconds to run this algorithm.
|
||||||
|
*/
|
||||||
140
src/main/java/com/mattrixwv/adventOfCode24/days/Problem18.java
Normal file
140
src/main/java/com/mattrixwv/adventOfCode24/days/Problem18.java
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
package com.mattrixwv.adventOfCode24.days;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import com.mattrixwv.Stopwatch;
|
||||||
|
|
||||||
|
|
||||||
|
public class Problem18 extends Problem{
|
||||||
|
private static final String inputFileName = "days/Problem17.txt";
|
||||||
|
private String rawInput;
|
||||||
|
private List<Integer> prettyInput;
|
||||||
|
|
||||||
|
|
||||||
|
public Problem18(){
|
||||||
|
super();
|
||||||
|
description = "Find the filesystem checksum after compacting the files.";
|
||||||
|
result = "Unresolved";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String runSolution(){
|
||||||
|
Stopwatch timer = new Stopwatch();
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
|
||||||
|
//Read the file
|
||||||
|
/* */
|
||||||
|
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||||
|
scanner.useDelimiter("\n");
|
||||||
|
rawInput = scanner.next();
|
||||||
|
}
|
||||||
|
/* */
|
||||||
|
/*
|
||||||
|
rawInput = "2333133121414131402";
|
||||||
|
/* */
|
||||||
|
|
||||||
|
//Change the raw into into pretty input
|
||||||
|
generatePrettyInput();
|
||||||
|
|
||||||
|
//Compact the files
|
||||||
|
compactInput();
|
||||||
|
|
||||||
|
//Generate sum
|
||||||
|
long sum = generateCheckSum();
|
||||||
|
|
||||||
|
|
||||||
|
//Save the results
|
||||||
|
timer.stop();
|
||||||
|
result = "The filesystem checksum is " + sum + ".\nIt took " + timer.toString() + " to run this algorithm.";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void generatePrettyInput(){
|
||||||
|
prettyInput = new ArrayList<>();
|
||||||
|
int fileNumber = 0;
|
||||||
|
boolean isFile = true;
|
||||||
|
|
||||||
|
for(int loc = 0;loc < rawInput.length();++loc){
|
||||||
|
//Get the next number
|
||||||
|
int num = Integer.parseInt(rawInput.charAt(loc) + "");
|
||||||
|
//If this number represents a file size then add the file number to the pretty input
|
||||||
|
if(isFile){
|
||||||
|
for(int cnt = 0;cnt < num;++cnt){
|
||||||
|
prettyInput.add(fileNumber);
|
||||||
|
}
|
||||||
|
++fileNumber;
|
||||||
|
}
|
||||||
|
//If this number represents free space then add -1 to the pretty input
|
||||||
|
else{
|
||||||
|
for(int cnt = 0;cnt < num;++cnt){
|
||||||
|
prettyInput.add(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
isFile = !isFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void compactInput(){
|
||||||
|
//Get the last file id
|
||||||
|
int currentFileId = 0;
|
||||||
|
for(int cnt = prettyInput.size() - 1;(currentFileId == 0) && (cnt >= 0);--cnt){
|
||||||
|
if(prettyInput.get(cnt) != -1){
|
||||||
|
currentFileId = prettyInput.get(cnt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while(currentFileId >= 0){
|
||||||
|
//Get the last file size
|
||||||
|
int startIndex = prettyInput.indexOf(currentFileId);
|
||||||
|
int size = 1 + prettyInput.lastIndexOf(currentFileId) - startIndex;
|
||||||
|
int indexOfBlankSpace = findBlankSpace(size, startIndex);
|
||||||
|
//Check if any blank spaces are large enough to fit the file
|
||||||
|
if(indexOfBlankSpace >= 0){
|
||||||
|
//Move the entire file to that space and blank out the file's current location
|
||||||
|
for(int cnt = 0;cnt < size;++cnt){
|
||||||
|
prettyInput.set(indexOfBlankSpace + cnt, currentFileId); //Move file
|
||||||
|
prettyInput.set(startIndex + cnt, -1); //Blank file's old location
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--currentFileId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int findBlankSpace(int size, int finalIndex){
|
||||||
|
int currentBlankSize = 0;
|
||||||
|
for(int cnt = 0;(cnt < finalIndex) && (cnt < prettyInput.size());++cnt){
|
||||||
|
if(prettyInput.get(cnt) == -1){
|
||||||
|
++currentBlankSize;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
currentBlankSize = 0;
|
||||||
|
}
|
||||||
|
if(currentBlankSize >= size){
|
||||||
|
return 1 + cnt - size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private long generateCheckSum(){
|
||||||
|
long sum = 0;
|
||||||
|
for(int loc = 0;loc < prettyInput.size();++loc){
|
||||||
|
if(prettyInput.get(loc) != -1){
|
||||||
|
sum += (loc * prettyInput.get(loc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Find the filesystem checksum after compacting the files.
|
||||||
|
The filesystem checksum is 6398096697992.
|
||||||
|
It took 617.157 milliseconds to run this algorithm.
|
||||||
|
*/
|
||||||
1
src/main/resources/days/Problem17.txt
Normal file
1
src/main/resources/days/Problem17.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user