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.

25 Upvotes

228 comments sorted by

View all comments

Show parent comment

0

u/jsims87 Dec 03 '15

Running into a similar issue with mine. Looks like your Set isn't hashing correctly; you're getting multiple entries in your Set for (presumably) the same Object

1

u/[deleted] Dec 03 '15

Every time you translate the Point, it changes hashcode (it is based off the X and Y coordinates, as it should be).

It SHOULD create multiple entries.

-2

u/tempyreddity Dec 03 '15

Thanks for the response. Do you know what could be causing this? I've never heard of an issue with the java standard libraries before.

2

u/nutrecht Dec 03 '15

You're making a mistake. Whenever you use objects that are used in a HashSet / Map you can't modify them after you added because the hash-set / map won't be able to find them anymore. This has nothing to do with Java: this is how a hash-set / map works in pretty much every language.

So instead of updating the values in your Point you need to add a new Point every time.

1

u/desrtfx Dec 03 '15

There is no issue.

You just need to add a new Point to the HashSet every time you move Santa around. Trying to add the same Point instance will not work.

I have made a similar solution using HashSet and it works flawlessly. Another user also has made a similar solution and his also works without problems.

You can check the working solutions over at /r/javahelp in this thread