Petra

Posted on

# Advent of Code - Day 5 : Puzzle 2

## Puzzle

--- Part Two ---

Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider diagonal lines.

Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:

An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3.
An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9.
Considering all lines from the above example would now produce the following diagram:

1.1....11.
.111...2..
..2.1.111.
...1.2.2..
.112313211
...1.2....
..1...1...
.1.....1..
1.......1.
222111....
You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2 or larger - now a total of 12 points.

Consider all of the lines. At how many points do at least two lines overlap?*

## My solution

``````package main;

import java.io.File;
import java.util.*;

public class Puzzle {
public static void main(String[] args) throws Exception {
File input = new File("/Users/files/input.txt");
Scanner scanner = new Scanner(input);

List<String> coordinates = new ArrayList<>();

while (scanner.hasNextLine()) {
}

int minX = 10000000;
int maxX = 0;

int minY = 10000000;
int maxY = 0;

for (int i = 0; i < coordinates.size(); i++) {

int firstXValue =
Integer.parseInt(coordinates.get(i).substring(0, coordinates.get(i).indexOf(",")));
int secondXValue =
Integer.parseInt(
coordinates
.get(i)
.substring(
coordinates.get(i).lastIndexOf(" ") + 1,
coordinates.get(i).lastIndexOf(",")));

int firstYValue =
Integer.parseInt(
coordinates
.get(i)
.substring(coordinates.get(i).indexOf(",") + 1, coordinates.get(i).indexOf(" ")));
int secondYValue =
Integer.parseInt(coordinates.get(i).substring(coordinates.get(i).lastIndexOf(",") + 1));

// calculate X values
if (firstXValue > maxX) maxX = firstXValue;
else if (firstXValue < minX) minX = firstXValue;
else if (secondXValue < minX) minX = firstXValue;
else if (secondXValue > maxX) maxX = firstXValue;

// calculate Y values
if (firstYValue > maxY) maxY = firstYValue;
else if (firstYValue < minY) minY = firstYValue;
else if (secondYValue < minX) minX = secondYValue;
else if (secondYValue > maxX) maxX = secondYValue;
}

int[][] table = new int[maxX + 1][maxY + 1];

// remove elements
for (int l = 0; l < coordinates.size(); l++) {
// if X values equal
int firstXValue =
Integer.parseInt(coordinates.get(l).substring(0, coordinates.get(l).indexOf(",")));
int secondXValue =
Integer.parseInt(
coordinates
.get(l)
.substring(
coordinates.get(l).lastIndexOf(" ") + 1,
coordinates.get(l).lastIndexOf(",")));

int firstYValue =
Integer.parseInt(
coordinates
.get(l)
.substring(coordinates.get(l).indexOf(",") + 1, coordinates.get(l).indexOf(" ")));
int secondYValue =
Integer.parseInt(coordinates.get(l).substring(coordinates.get(l).lastIndexOf(",") + 1));

if (firstXValue == secondXValue) {
int startingY = 0;
int endingY = 0;

if (firstYValue < secondYValue) {

startingY = firstYValue;

endingY = secondYValue;
} else {
endingY = firstYValue;
startingY = secondYValue;
}

for (int k = startingY; k < endingY + 1; k++) {

for (int n = minX; n < table[k].length; n++) {
if (n == secondXValue) {
table[k][n]++;
}
}
}
}

// if Y values equal
if (firstYValue == secondYValue) {

int startingX = 0;
int endingX = 0;

if (firstXValue < secondXValue) {
startingX = firstXValue;
endingX = secondXValue;
} else {
endingX = firstXValue;
startingX = secondXValue;
}

for (int n = startingX; n < endingX + 1; n++) {
table[secondYValue][n]++;
}
}
// diagonal values
else if (firstXValue != secondXValue && firstYValue != secondYValue) {
int startingX = 0;
int endingX = 0;

int startingY = 0;
int endingY = 0;

if (firstXValue < secondXValue) {
startingX = firstXValue;
endingX = secondXValue;
startingY = firstYValue;
endingY = secondYValue;
} else if (firstXValue > secondXValue) {

endingX = firstXValue;
startingX = secondXValue;
endingY = firstYValue;
startingY = secondYValue;
}

if (startingY < endingY) {

outer:
for (int a = startingY; a < endingY + 1; a++) {
for (int b = startingX; b < endingX + 1; b++) {

table[a][b]++;
startingX++;
continue outer;
}
}
} else if (startingY > endingY) {
outer:
for (int a = startingY; a > endingY - 1; a--) {
for (int b = startingX; b < endingX + 1; b++) {

table[a][b]++;
startingX++;
continue outer;
}
}
}
}
}

int total = 0;

for (int[] ints : table) {
for (int anInt : ints) {
if (anInt > 1) total++;
}
}
System.out.println(total);
}
}

``````