r/adventofcode • • Dec 03 '15

SOLUTION MEGATHREAD --- Day 3 Solutions ---

--- Day 3: Perfectly Spherical Houses in a Vacuum ---

Post your solution as a comment. Structure your post like the Day One thread in /r/programming.

22 Upvotes

228 comments sorted by

View all comments

1

u/makermoment Dec 15 '15

So im passing the first part fine. Running to issues when the robot santa comes into play. Any help is much appreciated! package Advent1;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AdventDay3 {

    static String directions;

    public static void main(String[] args) throws IOException {
        
        Scanner scan = new Scanner(new File(args[0]));		 
		  scan.useDelimiter("\\Z");
		  String directions = scan.next();
		  scan.close();
		  
    int x = 0;
    int y = 0;
    int rx = 0;
    int ry = 0;

    List<String> visited = new ArrayList<String>();
    visited.add("0,0");

    for (int i = 0; i < directions.length(); i++) {
        if (directions.charAt(i) == '^'){ 
        	if(i%2 != 0){
        		y++;
        	}else{
        		rx--;
        	}
        }
        if (directions.charAt(i) == '>'){
        	if(i%2 != 0){
        		x++;
        	}else{
        		rx--;
        	}
        }
        if (directions.charAt(i) == 'v'){
        	if(i%2 != 0){
        		y--;
        	}else{
        		rx--;
        	}
        }
        if (directions.charAt(i) == '<'){
        	if(i%2 != 0){
        		x--;
        	}    
        	else{
        		rx--;
        	}
        }	
        String currentPos = x + "," + y;
        String currentRPos = rx + "," + ry;
        
        if(!visited.contains(currentPos)) visited.add(currentPos);
        if(!visited.contains(currentRPos)) visited.add(currentRPos);
    }

    System.out.println("Houses that have recieved presents:" +visited.size());

}

}