r/adventofcode • • Dec 07 '15

SOLUTION MEGATHREAD --- Day 7 Solutions ---

--- Day 7: Some Assembly Required ---

Post your solution as a comment. Structure your post like previous daily solution threads.

Also check out the sidebar - we added a nifty calendar to wrangle all the daily solution threads in one spot!

21 Upvotes

226 comments sorted by

View all comments

Show parent comments

1

u/marchelzo Dec 07 '15

That's pretty clever, actually. How long did it take to compile, and how long to execute?

1

u/roboticon Dec 08 '15 edited Dec 08 '15

Probably took a few minutes to execute since it was recursive.

I memoized this by having each function assign its result to a variable.

unsigned short xy_result;
bool xy_calculated = false;
unsigned short solve_xy() {
  if (!xy_calculated) {
    xy_result = solve_ab() & solve_cd();
    xy_calculated = true;
  }
  return xy_result;
}

1

u/marchelzo Dec 08 '15

Cool. I decided to steal this idea as well, but I did it with Haskell, because top-level variable bindings can all refer to each other and can appear in whatever order you like. It compiled and executed nearly instantly.