diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-06 17:50:08 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-06 17:50:08 +0800 |
commit | d5382bf6436dbb040fb4aa664135d8c1cb0fc3eb (patch) | |
tree | 579da864ba334d1ef942291157335010723bc30f /src/2018/day3/aoc.h | |
parent | 844da9170858556950aa54d4843de332734e228f (diff) | |
download | advent-of-code-d5382bf6436dbb040fb4aa664135d8c1cb0fc3eb.tar.gz advent-of-code-d5382bf6436dbb040fb4aa664135d8c1cb0fc3eb.zip |
2018 day3 part1
Diffstat (limited to 'src/2018/day3/aoc.h')
-rw-r--r-- | src/2018/day3/aoc.h | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/src/2018/day3/aoc.h b/src/2018/day3/aoc.h index 4bfdbf3..24fad47 100644 --- a/src/2018/day3/aoc.h +++ b/src/2018/day3/aoc.h @@ -1,6 +1,64 @@ #pragma once #include "common.h" +#include <vector> namespace aoc2018 { -} +struct claim { + int id; + int x; + int y; + int width; + int height; +}; + +struct fabric { + // 0 ... 999 + uint16_t claims[1000 * 1000] = {0}; + + void apply(const claim& c) { + for (int x = c.x; x < c.x + c.width; x++) { + for (int y = c.y; y < c.y + c.height; y++) { + claims[y * 1000 + x] += 1; + } + } + } + + int count(int x) { + int total{0}; + for (auto c : claims) { + if (c > x) { + total += 1; + } + } + return total; + } + + void get_number(const char** pp, int* is[], int i) { + *is[i] = 0; + const char* p = *pp; + while (*p >= '0' && *p <= '9') { + *is[i] = *is[i] * 10 + *p - '0'; + p++; + } + *pp = p; + } + + void parse(line_view lv) { + claim c; + int* is[] = {&c.id, &c.x, &c.y, &c.width, &c.height}; + int i{0}; + const char* p = lv.line; + while (p < lv.line + lv.length) { + if (*p >= '0' && *p <= '9') { + get_number(&p, is, i++); + } + p++; + } + apply(c); + } +}; + +int day3(line_view, int); + +} // namespace aoc2018 |