diff options
Diffstat (limited to 'src/2017/day10/aoc.cpp')
-rw-r--r-- | src/2017/day10/aoc.cpp | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/src/2017/day10/aoc.cpp b/src/2017/day10/aoc.cpp index b6b617f..0d0d5a4 100644 --- a/src/2017/day10/aoc.cpp +++ b/src/2017/day10/aoc.cpp @@ -1,4 +1,6 @@ #include "aoc.h" +#include <deque> +#include <set> namespace aoc2017 { @@ -102,7 +104,19 @@ static void reduce(uint8_t dense[16], uint8_t sparse[256]) { } } -int knot_hash(line_view file, int total) { +static void binary_grid(char hexdigit[33], char* grid) { + const char* b09[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001"}; + const char* baf[] = {"1010", "1011", "1100", "1101", "1110", "1111"}; + + for (int i = 0; i < 32; i++) { + char c = hexdigit[i]; + memcpy(grid + 4 * i, c >= '0' && c <= '9' ? b09[c - '0'] : baf[c - 'a'], 4); + } + + // printf("%s\n", binary); +} + +int knot_hash(line_view file, int total, char* grid = nullptr) { const char trail[] = {17, 31, 73, 47, 23}; char* input = (char*)malloc(file.length - 1 + 5); memcpy(input, file.line, file.length - 1); @@ -125,8 +139,10 @@ int knot_hash(line_view file, int total) { for (int i = 0; i < 16; i++) { sprintf(hexdigit + i * 2, "%02x", dense[i]); } - // printf("%s\n", hexdigit); + if (grid != nullptr) { + binary_grid(hexdigit, grid); + } return 0; } @@ -134,4 +150,63 @@ std::pair<int64_t, int64_t> day10(line_view file) { knot_hash(file, 64); return {knot_hash_once(file), 0}; } + +struct pos { + int r; + int c; + + friend bool operator==(pos p1, pos p2) { return p1.r == p2.r && p1.c == p2.c; } + friend bool operator<(pos p1, pos p2) { return p1.r < p2.r ? true : p1.r > p2.r ? false : p1.c < p2.c; } +}; + +void floodfill(int x, pos p, char grid[128][128], int fill[128][128]) { + std::deque<pos> q; + q.push_back(p); + + auto is_valid = [](pos p) -> bool { return p.r >= 0 && p.r < 128 && p.c >= 0 && p.c < 128; }; + while (!q.empty()) { + auto s = q.size(); + while (s-- > 0) { + auto p0 = q.front(); + q.pop_front(); + + fill[p0.r][p0.c] = x; + + pos ns[] = {{p0.r, p0.c + 1}, {p0.r, p0.c - 1}, {p0.r - 1, p0.c}, {p0.r + 1, p0.c}}; + for (auto& n : ns) { + if (is_valid(n) && grid[n.r][n.c] == '1' && fill[n.r][n.c] == 0) { + q.push_back(n); + } + } + } + } +} + +std::pair<int64_t, int64_t> day14(line_view) { + char buf[30]; + const char* key = "oundnydw"; + // const char* key = "flqrgnkx"; + char grid[128][128]; + for (int i = 0; i < 128; i++) { + memset(buf, 0, 30); + sprintf(buf, "%s-%d\n", key, i); + knot_hash(buf, 64, grid[i]); + } + + int t0{0}; + int fill[128][128] = {{0}}; + + int t1{0}; + for (int r = 0; r < 128; r++) { + for (int c = 0; c < 128; c++) { + char x = grid[r][c]; + t0 += x - '0'; + if (x == '1' && fill[r][c] == 0) { + floodfill(++t1, {r, c}, grid, fill); + } + } + } + + return {t0, t1}; +} } // namespace aoc2017 |