Complete day 8
This commit is contained in:
123
src/main/java/com/mattrixwv/adventOfCode24/days/Problem15.java
Normal file
123
src/main/java/com/mattrixwv/adventOfCode24/days/Problem15.java
Normal file
@@ -0,0 +1,123 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
import com.mattrixwv.Triple;
|
||||
|
||||
|
||||
public class Problem15 extends Problem{
|
||||
private static final String inputFileName = "days/Problem15.txt";
|
||||
private List<String> map;
|
||||
private Set<Triple<Integer, Integer, Character>> antiNodes;
|
||||
|
||||
|
||||
public Problem15(){
|
||||
super();
|
||||
description = "Find the number of unique anti-nodes in the bounds of the map.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
|
||||
//Read the file
|
||||
map = new ArrayList<>();
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter("\n");
|
||||
while(scanner.hasNext()){
|
||||
String inputLine = scanner.next();
|
||||
if(inputLine.isBlank()){
|
||||
continue;
|
||||
}
|
||||
else{
|
||||
map.add(inputLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
findAntiNodes();
|
||||
|
||||
|
||||
//Save the results
|
||||
timer.stop();
|
||||
result = "There are " + antiNodes.size() + " anti-nodes in the boundaries of the map.\nIt took " + timer.toString() + " to run this algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
private void findAntiNodes(){
|
||||
antiNodes = new HashSet<>();
|
||||
//Step through the map and find all nodes
|
||||
Map<Character, List<Triple<Integer, Integer, Character>>> nodes = new HashMap<>();
|
||||
nodes.put('.', new ArrayList<>());
|
||||
for(String mapRow : map){
|
||||
for(char ch : mapRow.toCharArray()){
|
||||
if(!nodes.containsKey(ch)){
|
||||
nodes.put(ch, findMatchingElements(ch));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Step through the list of nodes and find all anti-nodes
|
||||
for(Character key : nodes.keySet()){
|
||||
List<Triple<Integer, Integer, Character>> nodeList = nodes.get(key);
|
||||
for(int nodeLoc1 = 0;nodeLoc1 < nodeList.size();++nodeLoc1){
|
||||
for(int nodeLoc2 = nodeLoc1 + 1;nodeLoc2 < nodeList.size();++nodeLoc2){
|
||||
Triple<Integer, Integer, Character> node1 = nodeList.get(nodeLoc1);
|
||||
Triple<Integer, Integer, Character> node2 = nodeList.get(nodeLoc2);
|
||||
int rowDifference = node1.getA() - node2.getA();
|
||||
int colDifference = node1.getB() - node2.getB();
|
||||
//Forwards node
|
||||
int antiNode1Row = node1.getA() + rowDifference;
|
||||
int antiNode1Col = node1.getB() + colDifference;
|
||||
if(isInBounds(antiNode1Row, antiNode1Col)){
|
||||
antiNodes.add(new Triple<>(antiNode1Row, antiNode1Col, 'A'));
|
||||
}
|
||||
//Backwards node
|
||||
int antiNode2Row = node2.getA() - rowDifference;
|
||||
int antiNode2Col = node2.getB() - colDifference;
|
||||
if(isInBounds(antiNode2Row, antiNode2Col)){
|
||||
antiNodes.add(new Triple<>(antiNode2Row, antiNode2Col, 'A'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Triple<Integer, Integer, Character>> findMatchingElements(char element){
|
||||
List<Triple<Integer, Integer, Character>> elements = new ArrayList<>();
|
||||
//Step through each row in the map searching for the given character
|
||||
for(int row = 0;row < map.size();++row){
|
||||
char[] mapRow = map.get(row).toCharArray();
|
||||
for(int col = 0;col < mapRow.length;++col){
|
||||
if(mapRow[col] == element){
|
||||
elements.add(new Triple<Integer, Integer, Character>(row, col, element));
|
||||
}
|
||||
}
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
||||
private boolean isInBounds(int row, int col){
|
||||
return
|
||||
(row >= 0) && (row < map.size()) &&
|
||||
(col >= 0) && (col < map.get(row).length());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Find the number of unique anti-nodes in the bounds of the map.
|
||||
There are 409 anti-nodes in the boundaries of the map.
|
||||
It took 6.557 milliseconds to run this algorithm.
|
||||
*/
|
||||
130
src/main/java/com/mattrixwv/adventOfCode24/days/Problem16.java
Normal file
130
src/main/java/com/mattrixwv/adventOfCode24/days/Problem16.java
Normal file
@@ -0,0 +1,130 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
import com.mattrixwv.Triple;
|
||||
|
||||
|
||||
public class Problem16 extends Problem{
|
||||
private static final String inputFileName = "days/Problem15.txt";
|
||||
private List<String> map;
|
||||
private Set<Triple<Integer, Integer, Character>> antiNodes;
|
||||
|
||||
|
||||
public Problem16(){
|
||||
super();
|
||||
description = "Find the number of unique anti-nodes in the bounds of the map.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
|
||||
//Read the file
|
||||
map = new ArrayList<>();
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter("\n");
|
||||
while(scanner.hasNext()){
|
||||
String inputLine = scanner.next();
|
||||
if(inputLine.isBlank()){
|
||||
continue;
|
||||
}
|
||||
else{
|
||||
map.add(inputLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
findAntiNodes();
|
||||
|
||||
|
||||
//Save the results
|
||||
timer.stop();
|
||||
result = "There are " + antiNodes.size() + " anti-nodes in the boundaries of the map.\nIt took " + timer.toString() + " to run this algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
private void findAntiNodes(){
|
||||
antiNodes = new HashSet<>();
|
||||
//Step through the map and find all nodes
|
||||
Map<Character, List<Triple<Integer, Integer, Character>>> nodes = new HashMap<>();
|
||||
nodes.put('.', new ArrayList<>());
|
||||
for(String mapRow : map){
|
||||
for(char ch : mapRow.toCharArray()){
|
||||
if(!nodes.containsKey(ch)){
|
||||
nodes.put(ch, findMatchingElements(ch));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Step through the list of nodes and find all anti-nodes
|
||||
for(Character key : nodes.keySet()){
|
||||
List<Triple<Integer, Integer, Character>> nodeList = nodes.get(key);
|
||||
for(int nodeLoc1 = 0;nodeLoc1 < nodeList.size();++nodeLoc1){
|
||||
for(int nodeLoc2 = nodeLoc1 + 1;nodeLoc2 < nodeList.size();++nodeLoc2){
|
||||
Triple<Integer, Integer, Character> node1 = nodeList.get(nodeLoc1);
|
||||
Triple<Integer, Integer, Character> node2 = nodeList.get(nodeLoc2);
|
||||
//Since there are at least 2 elements add each as a resonance point
|
||||
antiNodes.add(new Triple<>(node1.getA(), node1.getB(), 'A'));
|
||||
antiNodes.add(new Triple<>(node2.getA(), node2.getB(), 'A'));
|
||||
|
||||
int rowDifference = node1.getA() - node2.getA();
|
||||
int colDifference = node1.getB() - node2.getB();
|
||||
//Forwards node
|
||||
int antiNode1Row = node1.getA() + rowDifference;
|
||||
int antiNode1Col = node1.getB() + colDifference;
|
||||
while(isInBounds(antiNode1Row, antiNode1Col)){
|
||||
antiNodes.add(new Triple<>(antiNode1Row, antiNode1Col, 'A'));
|
||||
antiNode1Row += rowDifference;
|
||||
antiNode1Col += colDifference;
|
||||
}
|
||||
//Backwards node
|
||||
int antiNode2Row = node2.getA() - rowDifference;
|
||||
int antiNode2Col = node2.getB() - colDifference;
|
||||
while(isInBounds(antiNode2Row, antiNode2Col)){
|
||||
antiNodes.add(new Triple<>(antiNode2Row, antiNode2Col, 'A'));
|
||||
antiNode2Row -= rowDifference;
|
||||
antiNode2Col -= colDifference;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Triple<Integer, Integer, Character>> findMatchingElements(char element){
|
||||
List<Triple<Integer, Integer, Character>> elements = new ArrayList<>();
|
||||
//Step through each row in the map searching for the given character
|
||||
for(int row = 0;row < map.size();++row){
|
||||
char[] mapRow = map.get(row).toCharArray();
|
||||
for(int col = 0;col < mapRow.length;++col){
|
||||
if(mapRow[col] == element){
|
||||
elements.add(new Triple<Integer, Integer, Character>(row, col, element));
|
||||
}
|
||||
}
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
||||
private boolean isInBounds(int row, int col){
|
||||
return
|
||||
(row >= 0) && (row < map.size()) &&
|
||||
(col >= 0) && (col < map.get(row).length());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Find the number of unique anti-nodes in the bounds of the map.
|
||||
There are 1308 anti-nodes in the boundaries of the map.
|
||||
It took 8.836 milliseconds to run this algorithm.
|
||||
*/
|
||||
Reference in New Issue
Block a user