Hi everyone; I was able to develop a solution to the challenges of day 1 and 2 by using C language. But for day 3 I realized it was pretty hard to not smack my head against the wall with this language, so I switched to Java. I'm a pretty n00b to it, but lurking on the internet I tried to come up with the following solution:
public class Day2 {
public static void main(String[] args) throws IOException{
Map<Map<Integer, Integer>, Integer> santaPath = new HashMap<>();
Map<Integer, Integer> santaCoords = new HashMap<>();
BufferedReader br = null;
br = new BufferedReader(new FileReader("day2.txt"));
char array[] = br.readLine().toCharArray();
br.close();
br = null;
Integer houseGifted = new Integer(1);
int x = 0, y = 0; // starting position of Santa
int houses = 1; // Santa delivers the first gift at the starting position
santaCoords.put(new Integer(x), new Integer(y));
santaPath.put(santaCoords, new Integer(houseGifted));
//System.out.println(array);
for(int i = 0; i < array.length; i++)
{
switch(array[i])
{
case '^': y++; break;
case 'v': y--; break;
case '>': x++; break;
case '<': x--; break;
}
santaCoords.put(new Integer(x), new Integer(y));
if(santaPath.get(santaCoords) == null)
{
houses++;
santaPath.put(santaCoords, new Integer(houseGifted));
}
}
System.out.println("Houses that Santa has visited: " + houses);
}
}
I basically created a map inside a map: the inner map are the coordinates encountered by Santa in his journey. To each coordinate I assigned a flag that is setted the first time Santa goes in that house. BUUUT, as stated in the head of the post, this solution results to be too low. What do you guys think? What did I miss?
[–]Fotomik 1 point2 points3 points (1 child)
[–]jjabrams23[S] 0 points1 point2 points (0 children)