Guest User

Untitled

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