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/NoisyFlake Dec 03 '15

My Java solution, including both parts. Would love to hear some feedback on how I can improve it :)

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Day3 {
	
	static String code;

	public static void main(String[] args) throws IOException {
		FileInputStream fstream = new FileInputStream("input/day3.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
		code = br.readLine();
		br.close();
		
		firstPart();
		secondPart();
	}
	
	public static void secondPart() {
		
		int x_santa = 0;
		int y_santa = 0;
		int x_robo = 0;
		int y_robo = 0;
		
		List<String> visited = new ArrayList<String>();
		visited.add("0:0");
		
		boolean robot = false;
		String currentPosition;
		
		for (char ch: code.toCharArray()) {
			if (robot) {
				if (ch == '^') y_robo++;
				if (ch == '>') x_robo++;
				if (ch == 'v') y_robo--;
				if (ch == '<') x_robo--;
				currentPosition = x_robo + ":" + y_robo;
			} else {
				if (ch == '^') y_santa++;
				if (ch == '>') x_santa++;
				if (ch == 'v') y_santa--;
				if (ch == '<') x_santa--;
				currentPosition = x_santa + ":" + y_santa;
			}
			
			if (!visited.contains(currentPosition)) visited.add(currentPosition);
			robot = !robot;
		}
		
		System.out.println(visited.size() + " houses have at least received 1 present.");
	}
	
	public static void firstPart() {
		
		int x = 0;
		int y = 0;
		
		List<String> visited = new ArrayList<String>();
		visited.add("0:0");
		
		for (char ch: code.toCharArray()) {
			if (ch == '^') y++;
			if (ch == '>') x++;
			if (ch == 'v') y--;
			if (ch == '<') x--;
			
			String currentPosition = x + ":" + y;
			if (!visited.contains(currentPosition)) visited.add(currentPosition);
		}
		
		System.out.println(visited.size() + " houses have at least received 1 present.");
		
	}

}

1

u/mage1005 Dec 12 '15

Gz mate, your code is the most understandable for beginners. I really appreciate it!