코드 2021의 출현 - 5일차
21541 단어 javaadventofcode
퍼즐
--- 5일차: 열수 벤처 ---
당신은 해저에 열수 분출구 필드를 발견했습니다! 이 통풍구는 지속적으로 크고 불투명한 구름을 생성하므로 가능하면 피하는 것이 가장 좋습니다.
그들은 일렬로 형성되는 경향이 있습니다. 잠수함은 사용자가 검토할 수 있도록 근처의 통풍구(퍼즐 입력) 목록을 유용하게 생성합니다. 예를 들어:
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2
통풍구의 각 라인은 x1,y1 -> x2,y2 형식의 선분으로 제공됩니다. 여기서 x1,y1은 선분의 한쪽 끝 좌표이고 x2,y2는 다른 쪽 끝의 좌표입니다. 이러한 선분에는 양쪽 끝에 있는 점이 포함됩니다. 다시 말해:
1,1 -> 1,3과 같은 항목은 포인트 1,1, 1,2 및 1,3을 포함합니다.
9,7 -> 7,7과 같은 항목은 9,7, 8,7 및 7,7을 포함합니다.
지금은 수평선과 수직선만 고려하십시오: x1 = x2 또는 y1 = y2인 선.
따라서 위 목록의 수평선과 수직선은 다음 다이어그램을 생성합니다.
.......1..
..1....1..
..1....1..
.......1..
.112111211
...........
...........
...........
...........
222111....
이 다이어그램에서 왼쪽 위 모서리는 0,0이고 오른쪽 아래 모서리는 9,9입니다. 각 위치는 해당 점 또는 를 덮는 선의 수로 표시됩니다. 선이 그 지점을 덮지 않는 경우. 예를 들어, 1의 왼쪽 상단 쌍은 2,2 -> 2,1에서 옵니다. 맨 아래 행은 겹치는 선 0,9 -> 5,9 및 0,9 -> 2,9에 의해 형성됩니다.
가장 위험한 지역을 피하려면 최소한 두 개의 선이 겹치는 지점의 수를 결정해야 합니다. 위의 예에서 이것은 2개 이상(총 5개 점)이 있는 다이어그램의 아무 곳에나 있습니다.
수평선과 수직선만 고려하십시오. 최소한 두 개의 선이 겹치는 지점은 몇 개입니까?*
내 솔루션
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()) {
coordinates.add(scanner.nextLine());
}
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]++;
}
}
}
int total = 0;
for (int[] ints : table) {
for (int anInt : ints) {
if (anInt > 1) total++;
}
}
System.out.println(total);
}
}
*출처: https://adventofcode.com/2021/day/5
Reference
이 문제에 관하여(코드 2021의 출현 - 5일차), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/petraboldi/advent-of-code-2021-day-5-5g6p텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)