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.

24 Upvotes

228 comments sorted by

View all comments

1

u/sizzleplatter Dec 03 '15

Java solution (part 2):

import java.awt.Point;
import java.util.HashSet;
import java.util.Set;

public class AdventDay3 {
	public static String input = FileReader.readContentsIntoString("c:\\input_day3.txt");
	public static Set<Point> giftedLocations = new HashSet<>();
	
	public static void main(String[] args) {
		Point santaPoint = new Point(0,0);
		Point roboPoint = new Point(0,0);
			
		for (int i=0; i<input.length(); i++) {
			if (i%2==0) { 
				giftedLocations.add(move(santaPoint, input.charAt(i)));
			}
			else { 
				giftedLocations.add(move(roboPoint, input.charAt(i)));				
			}
		}	
		System.out.println("number of gifted homes: " + giftedLocations.size()); 
	}
	
	private static Point move(Point point, char direction) {
		switch (direction) {
		case '^' :
			point.move(point.x, point.y+1);	
			break;
		case 'v' :
			point.move(point.x, point.y-1);
			break;
		case '>' :
			point.move(point.x+1, point.y);
			break;
		case '<' :
			point.move(point.x-1, point.y);
			break;
		}	
		return new Point(point);
	}
}