Guest User

java-uber-alles

a guest
Dec 3rd, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package advent.of.code;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6.  
  7. public class Day3 {
  8.    
  9.     private String content;
  10.     private char[] array;
  11.     private Santa regularSanta;
  12.     private Santa roboSanta;
  13.     private int startPosX = 500;
  14.     private int startPosY = 500;
  15.     private int grid[][];
  16.  
  17.     public static void main(String[] args) {
  18.         Day3 day3 = new Day3();
  19.         day3.loadContent();
  20.         day3.initSantas();
  21.         day3.part1();
  22.         day3.initSantas();
  23.         day3.part2();
  24.     }
  25.    
  26.     private void initSantas() {
  27.         this.grid = new int[1000][1000];
  28.         regularSanta = new Santa(startPosX, startPosY);
  29.         roboSanta = new Santa(startPosX, startPosY);
  30.     }
  31.  
  32.     private void loadContent() {
  33.         try {
  34.             content = new String(Files.readAllBytes(Paths.get("C:\\AdventOfCode\\input3.txt")));
  35.             array = content.toCharArray();
  36.             System.out.println("array size = " + array.length);
  37.         } catch (IOException e) {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41.  
  42.     private void part1() {
  43.         for(char c : array) {
  44.             regularSanta.move(c);
  45.         }
  46.         System.out.println("part1 total = " + checkHowManyYouVisitedYouFatOaf());
  47.     }
  48.    
  49.     private void part2() {
  50.         int counter = 0;
  51.         for(char c : array) {
  52.             if(counter % 2 == 0) {
  53.                 regularSanta.move(c);
  54.             } else {
  55.                 roboSanta.move(c);
  56.             }
  57.             counter++;
  58.         }
  59.        
  60.         System.out.println("part2 total = " + checkHowManyYouVisitedYouFatOaf());
  61.     }
  62.    
  63.     private class Santa {
  64.         public int x;
  65.         public int y;
  66.        
  67.         public Santa(int x, int y) {
  68.             this.x = x;
  69.             this.y = y;
  70.             visitHouse();
  71.         }
  72.        
  73.         public void move(char direction) {
  74.             if(direction == '^') {
  75.                 y += 1;
  76.             } else if(direction == 'v') {
  77.                 y -= 1;
  78.             } else if(direction == '<') {
  79.                 x -= 1;
  80.             } else if(direction == '>') {
  81.                 x += 1;
  82.             }
  83.             visitHouse();
  84.         }
  85.        
  86.         public void visitHouse() {
  87.             grid[x][y] += 1;
  88.         }
  89.     }
  90.    
  91.     public int checkHowManyYouVisitedYouFatOaf() {
  92.         int total = 0;
  93.        
  94.         for(int i=0; i<grid.length; i++) {
  95.             for(int j=0; j<grid[i].length; j++) {
  96.                 if(grid[i][j] > 0) {
  97.                     total++;
  98.                 }
  99.             }
  100.         }
  101.        
  102.         return total;
  103.     }
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment