r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -๐ŸŽ„- 2018 Day 6 Solutions -๐ŸŽ„-

--- Day 6: Chronal Coordinates ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

Show parent comment

1

u/hobLs Dec 06 '18

Yeah, I also did a BFS flood fill for part 1. It's also pretty easy to get part 2 from that though, so I stuck with it.

1

u/Smylers Dec 07 '18

Oooh, is your code available? Because Partย 2 involves positions' distances to multiple co-ordinates, I couldn't see how a flood-fill would help.

1

u/hobLs Dec 07 '18

You know, I donโ€™t think it does help! My part 2 code simply figured out the midpoint between all of the groups by averaging them (this turns out to be very near to the middle of the grid), and then I add this (x, y) to a queue and start a normal breadth first search outwards from there.

BUT, hereโ€™s the nasty part, I then end up calculating the distance between each cell and all center points to make sure weโ€™re within the right distance. In my head I thought the shape might end up being weird for some reason, but I think itโ€™s just a circle. Itโ€™s a ton of abs() calls, which luckily are quite fast, so it doesnโ€™t actually take that much time, but itโ€™s many many calls.

The code runs in O(nk) time where n is number of input points and k is number of points you end up checking during the fill, which in this case is probably just worse than O(n2 ). I think it would be helpful if you needed to fill a funny shape for some reason though.

(Iโ€™m also not sure this is technically a flood fill. Maybe itโ€™s just a weird application of a graph technique to a geometry problem.)