import java.util.*;
import java.io.*;
public class Mowing{
public static class Point{
int x , y;
Point( int x , int y){
this.x = x;
this.y = y;
}
public int hashCode() {
return Objects.hash(x,y);
}
public boolean equals(Object other) {
Point temp = (Point)other;
return temp.x==x && temp.y ==y;
}
public String toString() {
return String.format("%d %d", x,y);
}
}
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(new FileReader("mowing.in"));
PrintWriter out = new PrintWriter(new FileWriter("mowing.out"));
int num = Integer.valueOf(in.nextLine());
int max = Integer.MAX_VALUE;
HashMap<Point , Integer> grid = new HashMap<>();
int x = 0, y= 0,time = 0;
grid.put(new Point(x,y), 0);
for(int i = 0;i<num;i++) {
String direction = in.next();
int steps = Integer.valueOf(in.nextLine().trim());
for(int j = 0;j<steps;j++) {
if(direction.equals("N"))y++;
if(direction.equals("E"))x++;
if(direction.equals("S"))y--;
if(direction.equals("W"))x--;
time++;
if(grid.containsKey(new Point(x,y))) {
max = Math.min(max, time-grid.get(new Point(x,y)));
}
grid.put(new Point(x,y),time);
}
}
out.println(max);
System.out.println(grid);
out.close();
}
}
This fails a few test cases and I am not sure why. PLease let me know if you find anything!