Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package advent.of.code;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- public class Day3 {
- private String content;
- private char[] array;
- private Santa regularSanta;
- private Santa roboSanta;
- private int startPosX = 500;
- private int startPosY = 500;
- private int grid[][];
- public static void main(String[] args) {
- Day3 day3 = new Day3();
- day3.loadContent();
- day3.initSantas();
- day3.part1();
- day3.initSantas();
- day3.part2();
- }
- private void initSantas() {
- this.grid = new int[1000][1000];
- regularSanta = new Santa(startPosX, startPosY);
- roboSanta = new Santa(startPosX, startPosY);
- }
- private void loadContent() {
- try {
- content = new String(Files.readAllBytes(Paths.get("C:\\AdventOfCode\\input3.txt")));
- array = content.toCharArray();
- System.out.println("array size = " + array.length);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void part1() {
- for(char c : array) {
- regularSanta.move(c);
- }
- System.out.println("part1 total = " + checkHowManyYouVisitedYouFatOaf());
- }
- private void part2() {
- int counter = 0;
- for(char c : array) {
- if(counter % 2 == 0) {
- regularSanta.move(c);
- } else {
- roboSanta.move(c);
- }
- counter++;
- }
- System.out.println("part2 total = " + checkHowManyYouVisitedYouFatOaf());
- }
- private class Santa {
- public int x;
- public int y;
- public Santa(int x, int y) {
- this.x = x;
- this.y = y;
- visitHouse();
- }
- public void move(char direction) {
- if(direction == '^') {
- y += 1;
- } else if(direction == 'v') {
- y -= 1;
- } else if(direction == '<') {
- x -= 1;
- } else if(direction == '>') {
- x += 1;
- }
- visitHouse();
- }
- public void visitHouse() {
- grid[x][y] += 1;
- }
- }
- public int checkHowManyYouVisitedYouFatOaf() {
- int total = 0;
- for(int i=0; i<grid.length; i++) {
- for(int j=0; j<grid[i].length; j++) {
- if(grid[i][j] > 0) {
- total++;
- }
- }
- }
- return total;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment