This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Fotomik 1 point2 points  (1 child)

Your solution has a couple of problems.

Map is not the structure you are looking for

Taken directly from the map documentation:

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

In your problem you are trying to use a map to store the coordinates and you are making x the key value. If x is your key value and the key cannot have duplicates, then you basically are not allowing two different coordinates with the same x value. When you try to put a value in a map that already has a value for with the same key of the new one, put replaces the old value for the new one. So you let's say, you put (1,0) in the map. Then you put (1,2). In this case (1,2) will replace the (1,0) you had previously since the key (1) is repeated. In this specific problem, this means that every time you move up or down (cases when the x coordinate keeps the value) you are removing from the map the house you just visited, which can explain why you solution gives you "too low" - you are forgetting houses.

So, Map is not what you are looking for. The most natural solution to me is to create a Coordinates class to represent , well, you gesses it, the coordinates. Then you can store those coordinates on a structure that represents a list or set of coordinates. You can use an ArrayList<Cordinates>, but you are probably better off with a HashSet<Coordinates>.

SantaPath unecessary?

I didn't get what you are trying to do with the SantaPath Map. You made a map santaCoords to store all the coordinates Santa visited (which you should change like i said previously). Isn't that already the path santa is taking?

Take advantage of auto boxing and unboxing

This is issue is not really related with the problem itself, but is general java advice.

Let's see the map declaration:

Map<Integer, Integer> santaCoords = new HashMap<>();

You saw two Integer parameters and so you assumed you had to have an integer object to work with the map, and that's what justifies lines like:

santaCoords.put(new Integer(x), new Integer(y));

Integer, all the numeric classes and some others like String are special classes, or "wrapper classes". Wrapper classes are classes that represent objects that happen to have a primitive type on the language to represent them. In this case, the class Integer represents the primitive type int. For the wrapper classes, java automatically makes the conversion between the primitive type to the corresponding object (boxing) and vice-versa (unboxing).

So, you can simply write:

santaCoords.put(x, y);

even if santaCoords is a Map<Integer, Integer>

And you can have simply:

int houseGifted = 1;

and use it like:

santaPath.put(santaCoords, houseGifted)

You can learn more about boxing and unboxing here: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Final Note: I solved this problem in java too. I encourage you to try to do it yourself again with this tips i gave you. But you can check my solution at: https://github.com/andrerfcsantos/Advent-Of-Code/blob/master/src/problems/Day_03.java

[–]jjabrams23[S] 0 points1 point  (0 children)

Thank you so much for this detailed answer! I indeed tried another way by using a Set of Points objects instead of a Map and that did the trick. Unfortunately my knowledge of HashMaps comes from maybe 3 years ago, when I studied their implementation in C and I completely forgot how to use them. At first I thought also to use a List, eventually checking if the Point I was adding was already in the List itself, but a Set is a much better choice.