diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2015/day10/README.md | 24 | ||||
-rw-r--r-- | src/2015/day10/aoc.cpp | 62 | ||||
-rw-r--r-- | src/2015/day10/aoc.h | 7 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 |
4 files changed, 94 insertions, 0 deletions
diff --git a/src/2015/day10/README.md b/src/2015/day10/README.md new file mode 100644 index 0000000..c1d7988 --- /dev/null +++ b/src/2015/day10/README.md @@ -0,0 +1,24 @@ +--- 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? + +Your puzzle input is 3113322113. + +--- 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? + diff --git a/src/2015/day10/aoc.cpp b/src/2015/day10/aoc.cpp new file mode 100644 index 0000000..ccddb37 --- /dev/null +++ b/src/2015/day10/aoc.cpp @@ -0,0 +1,62 @@ +#include "aoc.h" +#include <stdlib.h> +#include <string> + +namespace aoc2015 { + +int len(int i) { + int x{0}; + while (i > 0) { + i = i / 10; + x++; + } + return x; +} + +void itoa(int i, char* s) { + if (i < 10) { + *s = '0' + i; + } else { + int x = i % 10; + *s = '0' + x; + itoa(i / 10, s--); + } +} + +void record(const char* p1, const char* p2, std::string& x) { + char c = *p1; + int d = p2 - p1; + int l = len(d); + char* s = (char*)malloc(l + 1); + s[l] = '\0'; + itoa(d, s + l - 1); + x.append(s); + x.append(1, c); + free(s); +} + +std::string look_and_say(const char* s) { + std::string x; + const char* p1 = s; + const char* p2 = s; + while (*p2 != '\0') { + if (*p2 != *p1) { + record(p1, p2, x); + p1 = p2; + } + p2++; + } + record(p1, p2, x); + return x; +} + +size_t day10(const char* s, int repeat) { + std::string x(s); + for (int i = 0; i < repeat; i++) { + // printf("%s\n", x.c_str()); + x = look_and_say(x.c_str()); + } + return x.size(); +} + +} // namespace aoc2015 diff --git a/src/2015/day10/aoc.h b/src/2015/day10/aoc.h new file mode 100644 index 0000000..194662b --- /dev/null +++ b/src/2015/day10/aoc.h @@ -0,0 +1,7 @@ +#pragma once +#include "common.h" + +namespace aoc2015 { + +size_t day10(const char*, int); +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6fbe717..2d9c403 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ set(SOLUTION_FILES "2015/day7/aoc.cpp" "2015/day8/aoc.cpp" "2015/day9/aoc.cpp" + "2015/day10/aoc.cpp" ) add_library(solution SHARED ${SOLUTION_FILES}) |