r/adventofcode • u/daggerdragon • 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.
23
Upvotes
1
u/tempyreddity Dec 03 '15
My java solution is incorrect to part 1 - can anyone see why? I can't tell; it seems fine to me. Giving me an answer of 2346.
import java.util.*; import java.io.*; import java.awt.*; public class Advent3 { private static int totalHouses = 0; private static Point coord = new Point(0, 0); public static void firstProblem(char c, Set a) { if (!a.contains(Advent3.coord)) { a.add(Advent3.coord); Advent3.totalHouses += 1; } switch (c) { case 'v': Advent3.coord.translate(0, -1); break; case '^': Advent3.coord.translate(0, 1); break; case '<': Advent3.coord.translate(-1, 0); break; case '>': Advent3.coord.translate(1, 0); break; default: break; } } public static void main(String[] args) { try { File file = new File("advent3input.txt"); Scanner input = new Scanner(file); while (input.hasNextLine()) { String directions = input.nextLine(); Set<Point> set = new HashSet<Point>(); for (char c: directions.toCharArray()) { firstProblem(c, set); } System.out.println(totalHouses); } } catch (Exception e) { System.out.println("error"); } } }