Guest User

Untitled

a guest
Dec 3rd, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.HashSet;
  3. import java.util.Map;
  4. import java.util.Set;
  5. import java.awt.Point;
  6.  
  7. /**
  8.  * Created by tim on 12/2/2015.
  9.  */
  10. public class Advent3 {
  11.  
  12.     static enum Direction {
  13.         North(-1,0), South(1,0), East(0,1), West(0,-1);
  14.         private final int dx;
  15.         private final int dy;
  16.  
  17.         private Direction(int dx, int dy) {
  18.             this.dx = dx;
  19.             this.dy = dy;
  20.         }
  21.     }
  22.  
  23.     static Map<Character, Direction> map = new HashMap<>();
  24.  
  25.     static {
  26.         map.put('^', Direction.North);
  27.         map.put('>', Direction.East);
  28.         map.put('<', Direction.West);
  29.         map.put('v', Direction.South);
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.         String data = "^^<<v<<v><v^^<><> ...etc... >^>><<v>";
  34.  
  35.         //int[][] grid = new int[10000][10000];
  36.         Set<Point> set = new HashSet<>();
  37.  
  38.         int[] x = new int[2];
  39.         int[] y = new int[2];
  40.  
  41.         int turn = 1;
  42.         set.add(new Point(x[0], y[0]));
  43.         int sum = 1;
  44.  
  45.         Direction currentDir;
  46.  
  47.         for (char each : data.toCharArray()) {
  48.             turn ^= 1;
  49.             currentDir = map.get(each);
  50.             x[turn] += currentDir.dx;
  51.             y[turn] += currentDir.dy;
  52.             set.add(new Point(x[turn],y[turn]));
  53.  
  54.         }
  55.         System.out.println(set.size());
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment