Last active
September 3, 2023 08:20
-
-
Save moomdate/99a0ba8879b8c1e8ba2a419534a224e8 to your computer and use it in GitHub Desktop.
wtf test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
Stack<Vector> l2r = findHighestPosition(rowOfTree, findDirection.LEFT2RIGHT); | |
stack.addAll(l2r); | |
Stack<Vector> r2l = findHighestPosition(rowOfTree, findDirection.RIGHT2LEFT); | |
stack.addAll(r2l); | |
Stack<Vector> t2b = findHighestPosition(rowOfTree, findDirection.TOP2DOWN); | |
stack.addAll(t2b); | |
Stack<Vector> t2d = findHighestPosition(rowOfTree, findDirection.DOWN2TOP); | |
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 (d == findDirection.LEFT2RIGHT) { | |
for (int row = 0; row < list.size(); row++) { | |
List<String> cols = list.get(row); | |
int firstTree = Integer.parseInt(cols.get(0)); | |
stack.push(new Vector(row, 0)); | |
for (int col = 0; col < cols.size(); col++) { | |
// System.out.println(list.get(row).get(col)); | |
if (firstTree < Integer.parseInt(cols.get(col))) { | |
firstTree = Integer.parseInt(cols.get(col)); | |
stack.push(new Vector(row, col)); | |
} | |
} | |
} | |
} else if (d == findDirection.RIGHT2LEFT) { | |
for (int row = 0; row < list.size(); row++) { | |
List<String> cols = list.get(row); | |
int firstTree = Integer.parseInt(cols.get(cols.size() - 1)); | |
stack.push(new Vector(row, cols.size() - 1)); | |
for (int col = cols.size() - 1; col >= 0; col--) { | |
// System.out.println(list.get(row).get(col)); | |
if (firstTree < Integer.parseInt(cols.get(col))) { | |
firstTree = Integer.parseInt(cols.get(col)); | |
stack.push(new Vector(row, col)); | |
} | |
} | |
} | |
} | |
else if (d == findDirection.TOP2DOWN) { | |
for (int col1 = 0; col1 < list.get(0).size(); col1++) { | |
int firstTree = Integer.parseInt(list.get(0).get(col1)); | |
stack.push(new Vector(0, col1)); | |
for (int row1 = 0; row1 < list.size(); row1++) { | |
// System.out.println(list.get(row1).get(col1)); | |
if (firstTree < Integer.parseInt(list.get(row1).get(col1))) { | |
firstTree = Integer.parseInt(list.get(row1).get(col1)); | |
stack.push(new Vector(row1, col1)); | |
} | |
} | |
} | |
} | |
else if (d == findDirection.DOWN2TOP) { | |
for (int col1 = 0; col1 < list.get(0).size(); col1++) { | |
int firstTree = Integer.parseInt(list.get(list.size() - 1).get(col1)); | |
stack.push(new Vector(list.size() - 1, col1)); | |
for (int row1 = list.size() - 1; row1 >= 0; row1--) { | |
// System.out.println(list.get(row1).get(col1)); | |
if (firstTree < Integer.parseInt(list.get(row1).get(col1))) { | |
firstTree = Integer.parseInt(list.get(row1).get(col1)); | |
stack.push(new Vector(row1, col1)); | |
} | |
} | |
} | |
} | |
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