From 4c7b60e037a73a49616b8e3ce03adb931ee4f707 Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Thu, 10 Dec 2015 13:24:01 +0000 Subject: Day 10. --- day10/Makefile | 1 + day10/day10.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ day10/day10.input | 1 + day10/day10.readme | 21 +++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 120000 day10/Makefile create mode 100644 day10/day10.c create mode 100644 day10/day10.input create mode 100644 day10/day10.readme diff --git a/day10/Makefile b/day10/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/day10/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/day10/day10.c b/day10/day10.c new file mode 100644 index 0000000..3286198 --- /dev/null +++ b/day10/day10.c @@ -0,0 +1,55 @@ +#include +#include + +int encode(char* dest, char* src) { + int d = 0, s = 0, num; + char c; + + while(src[s] != '\0') { + c = src[s]; + num = 0; + + while(src[s] == c) { + num++; + s++; + } + + d += sprintf(dest + d, "%d", num); + dest[d++] = c; + } + dest[d] = '\0'; + + return d; +} + +int main() { + char *this, *next; + int i, len; + + this = (char*) malloc(11 * sizeof(char)); + next = (char*) malloc(21 * sizeof(char)); + + scanf("%s", this); + + for(i = 0; i < 20; i++) { + len = encode(next, this); + this = realloc(this, (2*len)+1); + len = encode(this, next); + next = realloc(next, (2*len)+1); + } + + printf("First final length: %d\n", len); + + for(i = 0; i < 5; i++) { + len = encode(next, this); + this = realloc(this, (2*len)+1); + len = encode(this, next); + next = realloc(next, (2*len)+1); + } + + printf("Second final length: %d\n", len); + + free(this); + free(next); + return 0; +} diff --git a/day10/day10.input b/day10/day10.input new file mode 100644 index 0000000..ec6ebd7 --- /dev/null +++ b/day10/day10.input @@ -0,0 +1 @@ +1113222113 diff --git a/day10/day10.readme b/day10/day10.readme new file mode 100644 index 0000000..98ba9f8 --- /dev/null +++ b/day10/day10.readme @@ -0,0 +1,21 @@ +--- Day 10: Elves Look, Elves Say --- + +Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s). + +Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed by the digit itself (1). + +For example: + + - 1 becomes 11 (1 copy of digit 1). + - 11 becomes 21 (2 copies of digit 1). + - 21 becomes 1211 (one 2 followed by one 1). + - 1211 becomes 111221 (one 1, one 2, and two 1s). + - 111221 becomes 312211 (three 1s, two 2s, and one 1). + +Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result? + +--- Part Two --- + +Neat, right? You might also enjoy hearing John Conway talking about this sequence (that's Conway of Conway's Game of Life fame). + +Now, starting again with the digits in your puzzle input, apply this process 50 times. What is the length of the new result? -- cgit v1.2.1