Skip to content

Instantly share code, notes, and snippets.

@moomdate
Created September 3, 2023 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moomdate/1120af9d2c1685cfcec56d31868705bf to your computer and use it in GitHub Desktop.
Save moomdate/1120af9d2c1685cfcec56d31868705bf to your computer and use it in GitHub Desktop.
test.java
package org.example;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws IOException {
// Scanner scanner = new Scanner(System.in);
//
// System.out.print("Enter a input file path: ");
//
// String userInput = scanner.nextLine();
//
// scanner.close();
String content = readUsingApacheCommonsIO("text.txt");
List<List<String>> rowOfTree = Stream.of(content.split("\n"))
.map(t -> Stream.of(t.split("")).collect(Collectors.toList()))
.collect(Collectors.toList());
Stack<Vector> stack = new Stack<Vector>();
// for (int row = 0; row < rowOfTree.size(); row++) {
// for (int col = 0; col < rowOfTree.get(row).size(); col++) {
// if (row == 0 || row == rowOfTree.size() - 1) { //start, end
// stack.push(new Vector(row, col));
// }
// if (col == 0 || col == rowOfTree.get(row).size() - 1) {
// stack.add(new Vector(row, col));
// }
// }
// }
Stack<Vector> t2b = findHighestPosition(rowOfTree, findDirection.DOWN2TOP);
stack.addAll(t2b);
for (Vector vector : stack) {
System.out.println("v," + vector.getRow() + ", " + vector.getCol());
}
//
// Stack<Vector> l2r = findHighestPosition(rowOfTree, findDirection.LEFT2RIGHT);
// stack.addAll(l2r);
//
// Stack<Vector> r2l = findHighestPosition(rowOfTree, findDirection.RIGHT2LEFT);
// stack.addAll(r2l);
//
// Stack<Vector> t2d = findHighestPosition(rowOfTree, findDirection.TOP2DOWN);
// stack.addAll(t2d);
List<Vector> setOfTree = new ArrayList<>();
for (Vector vector : stack) {
boolean match = setOfTree.stream().anyMatch(a -> a.getCol() == vector.getCol() && a.getRow() == vector.getRow());
if (!match)
setOfTree.add(vector);
}
System.out.println(setOfTree.size());
}
public static Stack<Vector> findHighestPosition(List<List<String>> list, findDirection d) {
Stack<Vector> stack = new Stack<Vector>();
if (findDirection.LEFT2RIGHT.equals(d)) {
for (int row = 0; row < list.size(); row++) {
List<String> cols = list.get(row);
for (int col = 0; col < cols.size(); col++) {
System.out.println("out:{}" + row + " , " + col);
if (col < cols.size() - 1 && Integer.parseInt(cols.get(col)) < Integer.parseInt(cols.get(col + 1))) {
stack.push(new Vector(row, col));
stack.push(new Vector(row, col + 1));
} else {
break;
}
}
}
}
if (findDirection.RIGHT2LEFT.equals(d)) {
for (int row = 0; row < list.size(); row++) {
List<String> cols = list.get(row);
for (int col = cols.size() - 1; col >= 0; col--) {
System.out.println("out:{}" + row + " , " + col);
if (col > 0 && Integer.parseInt(cols.get(col)) < Integer.parseInt(cols.get(col - 1))) {
stack.push(new Vector(row, col));
stack.push(new Vector(row, col - 1));
} else {
break;
}
}
}
}
if (findDirection.TOP2DOWN.equals(d)) {
for (int row = 0; row < list.size(); row++) {
for (int col = 0; col < list.get(row).size(); col++) {
String height = list.get(col).get(row);
if (row < list.get(col).size() - 1 && Integer.parseInt(height) < Integer.parseInt(list.get(col).get(row + 1))) {
stack.push(new Vector(col, row));
stack.push(new Vector(col + 1, row));
} else {
break;
}
}
}
}
if (findDirection.DOWN2TOP.equals(d)) {
for (int row = list.size() - 1; row >= 0; row--) {
for (int col = list.get(row).size() - 1; col >= 0; col--) {
String height = list.get(row).get(col);
if (row > 0 && Integer.parseInt(height) >= Integer.parseInt(list.get(row - 1).get(col))) {
break;
}
if (row > 0 && Integer.parseInt(height) < Integer.parseInt(list.get(row - 1).get(col))) {
System.out.println("cc" + height + ", " + list.get(row - 1).get(col));
stack.push(new Vector(row, col));
stack.push(new Vector(row - 1, col));
} else {
break;
}
}
}
}
return stack;
}
public enum findDirection {
LEFT2RIGHT,
RIGHT2LEFT,
TOP2DOWN,
DOWN2TOP
}
private static String readUsingApacheCommonsIO(String fileName) throws IOException {
return new String(Files.readAllBytes(Paths.get(fileName)));
}
public static class Vector {
private final int row;
private final int col;
Vector(int row, int col) {
this.row = row;
this.col = col;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment