diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-21 19:28:26 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-21 19:28:26 +0800 |
commit | 6905858d6fe4ab21c419992ad4bb0bbc002692d0 (patch) | |
tree | 6d2a99824604f08c7f0623797cd16bfa4173e1d6 /src/2015 | |
parent | bd126e2b3361ef472bcf819374638eb0d42c1ea2 (diff) | |
download | advent-of-code-6905858d6fe4ab21c419992ad4bb0bbc002692d0.tar.gz advent-of-code-6905858d6fe4ab21c419992ad4bb0bbc002692d0.zip |
day18 part1
Diffstat (limited to 'src/2015')
-rw-r--r-- | src/2015/day18/README.md | 54 | ||||
-rw-r--r-- | src/2015/day18/aoc.cpp | 7 | ||||
-rw-r--r-- | src/2015/day18/aoc.h | 99 |
3 files changed, 159 insertions, 1 deletions
diff --git a/src/2015/day18/README.md b/src/2015/day18/README.md index 1858130..f915fbd 100644 --- a/src/2015/day18/README.md +++ b/src/2015/day18/README.md @@ -70,5 +70,59 @@ After 4 steps, this example has four lights on. In your grid of 100x100 lights, given your initial configuration, how many lights are on after 100 steps? +--- Part Two --- +You flip the instructions over; Santa goes on to point out that this is all just an implementation of Conway's Game of Life. At least, it was, until you notice that something's wrong with the grid of lights you bought: four lights, one in each corner, are stuck on and can't be turned off. The example above will actually run like this: + +Initial state: +##.#.# +...##. +#....# +..#... +#.#..# +####.# + +After 1 step: +#.##.# +####.# +...##. +...... +#...#. +#.#### + +After 2 steps: +#..#.# +#....# +.#.##. +...##. +.#..## +##.### + +After 3 steps: +#...## +####.# +..##.# +...... +##.... +####.# + +After 4 steps: +#.#### +#....# +...#.. +.##... +#..... +#.#..# + +After 5 steps: +##.### +.##..# +.##... +.##... +#.#... +##...# + +After 5 steps, this example now has 17 lights on. + +In your grid of 100x100 lights, given your initial configuration, but with the four corners always in the on state, how many lights are on after 100 steps? diff --git a/src/2015/day18/aoc.cpp b/src/2015/day18/aoc.cpp index 9e8d822..0980b50 100644 --- a/src/2015/day18/aoc.cpp +++ b/src/2015/day18/aoc.cpp @@ -1,4 +1,11 @@ #include "aoc.h" namespace aoc2015 { +int day18(line_view file, int d) { + yard y; + y.parse(file); + y.turns(d); + return y.count(); } + +} // namespace aoc2015 diff --git a/src/2015/day18/aoc.h b/src/2015/day18/aoc.h index 7eb843c..874a67f 100644 --- a/src/2015/day18/aoc.h +++ b/src/2015/day18/aoc.h @@ -4,4 +4,101 @@ namespace aoc2015 { -} +struct neolight { + constexpr static size_t size = 100 * 100; + uint8_t bs[size] = {0}; + + int count() const noexcept { + int d{0}; + for (auto i : bs) { + if (i > 0) { + d += 1; + } + } + return d; + } + + void turn(int x, int y, int v) { bs[y * 100 + x] = v; } + + bool on(int x, int y) const noexcept { + if (x < 0 || y < 0 || x > 99 || y > 99) { + return false; + } + return bs[y * 100 + x] > 0; + } + + int count(int x, int y) const noexcept { + int d{0}; + for (int i = -1; i < 2; i++) { + for (int j = -1; j < 2; j++) { + if (!(i == 0 && j == 0)) { + d += on(x + i, y + j) ? 1 : 0; + } + } + } + return d; + } +}; + +struct yard { + neolight* lights = new neolight; + + void transform(int x, int y, neolight* g, neolight* n) { + if (g->on(x, y)) { + int v = 0; + int neighbors = g->count(x, y); + if (neighbors == 2 || neighbors == 3) { + v = 1; + } + n->turn(x, y, v); + } else { + int v = 0; + int neighbors = g->count(x, y); + if (neighbors == 3) { + v = 1; + } + n->turn(x, y, v); + } + } + + neolight* next(neolight* g) { + neolight* n = new neolight; + for (int x = 0; x < 100; x++) { + for (int y = 0; y < 100; y++) { + transform(x, y, g, n); + } + } + return n; + } + + void turns(int i) { + while (i-- > 0) { + lights = next(lights); + } + } + + int count() { return lights != nullptr ? lights->count() : 0; } + + void parse(line_view file) { + int x = 0; + int y = 0; + const char *p = file.line; + while (p != file.line + file.length) { + if (*p == '#') { + lights->turn(x++, y, 1); + } + if (*p == '.') { + lights->turn(x++, y, 0); + } + if (*p == '\n') { + x = 0; + y += 1; + } + p++; + } + } +}; + +int day18(line_view, int); + +} // namespace aoc2015 |