r/adventofcode • • Dec 09 '16

SOLUTION MEGATHREAD --- 2016 Day 9 Solutions ---

--- Day 9: Explosives in Cyberspace ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


RETICULATING SPLINES IS MANDATORY [?]

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!

11 Upvotes

155 comments sorted by

View all comments

Show parent comment

1

u/rhardih Dec 10 '16

There's a simpler way and kind of a trick to this puzzle. No recursion or actual decompression of the string is necessary. One iterative multiplication will do. Here's a linear solution:

int main(int argc, char const *argv[])
{
  char c;
  int index = 0, a, b;
  long i, pos, length = 0;

  fseek(stdin, 0, SEEK_END);
  long input_size = ftell(stdin);
  int multipliers[input_size], current_multiplier;
  for (i = 0; i < input_size; i++) multipliers[i] = 1;

  rewind(stdin);

  while(scanf("%c", &c) != EOF) {
    pos = ftell(stdin);
    current_multiplier = multipliers[pos - 1];

    if (c == '(') {
      scanf("%dx%d)", &a, &b);
      pos = ftell(stdin);

      for (i = pos; i < pos + a && i < input_size; i++)
      {
        multipliers[i] = current_multiplier * b;
      }
    } else if (c == '\n') {
      // skip whitespace
    } else {
      length += current_multiplier;
    }
  }

  printf("Decompressed length of file: %lu\n", length);

  return 0;
}

Explained in comment further down.

1

u/askalski Dec 10 '16

My 'full decompression' implementation was meant mainly to be funny, but also to demonstrate that such a solution was indeed feasible for the input provided.

I'm glad you shared this version, though. One thing I want to point about about it, unless I'm mistaken, this looks like it actually runs in O(n2) time because of the nested loop over the multipliers array (which is linear in the size of the input.)

One way to improve the time complexity would be to keep the current_multiplier variable, but replace the multipliers array with a min-priority-queue of (end_pos, divisor).