aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-06 12:27:42 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-06 12:27:42 +0800
commit2811b1bed5cde2d9260d5964e54dc8d3d2b87d49 (patch)
tree14c24f0efbcd50d9ab5b7bcf01acdb5c7d3c8746 /src
parentb369fbbe155a79fba2437f822f9507c53a8c35e4 (diff)
downloadadvent-of-code-2811b1bed5cde2d9260d5964e54dc8d3d2b87d49.tar.gz
advent-of-code-2811b1bed5cde2d9260d5964e54dc8d3d2b87d49.zip
2019 day10 part1
Diffstat (limited to 'src')
-rw-r--r--src/2019/day10/README.md59
-rw-r--r--src/2019/day10/aoc.cpp24
-rw-r--r--src/2019/day10/aoc.h97
-rw-r--r--src/2019/day10/input010
-rw-r--r--src/2019/day10/input110
-rw-r--r--src/2019/day10/input210
-rw-r--r--src/2019/day10/input320
-rw-r--r--src/common.cpp7
-rw-r--r--src/common.h1
9 files changed, 237 insertions, 1 deletions
diff --git a/src/2019/day10/README.md b/src/2019/day10/README.md
index 0af8fbd..f050cff 100644
--- a/src/2019/day10/README.md
+++ b/src/2019/day10/README.md
@@ -94,3 +94,62 @@ Best is 11,13 with 210 other asteroids detected:
#.#.#.#####.####.###
###.##.####.##.#..##
Find the best location for a new monitoring station. How many other asteroids can be detected from that location?
+
+--- Part Two ---
+Once you give them the coordinates, the Elves quickly deploy an Instant Monitoring Station to the location and discover the worst: there are simply too many asteroids.
+
+The only solution is complete vaporization by giant laser.
+
+Fortunately, in addition to an asteroid scanner, the new monitoring station also comes equipped with a giant rotating laser perfect for vaporizing asteroids. The laser starts by pointing up and always rotates clockwise, vaporizing any asteroid it hits.
+
+If multiple asteroids are exactly in line with the station, the laser only has enough power to vaporize one of them before continuing its rotation. In other words, the same asteroids that can be detected can be vaporized, but if vaporizing one asteroid makes another one detectable, the newly-detected asteroid won't be vaporized until the laser has returned to the same position by rotating a full 360 degrees.
+
+For example, consider the following map, where the asteroid with the new monitoring station (and laser) is marked X:
+
+.#....#####...#..
+##...##.#####..##
+##...#...#.#####.
+..#.....X...###..
+..#.#.....#....##
+The first nine asteroids to get vaporized, in order, would be:
+
+.#....###24...#..
+##...##.13#67..9#
+##...#...5.8####.
+..#.....X...###..
+..#.#.....#....##
+Note that some asteroids (the ones behind the asteroids marked 1, 5, and 7) won't have a chance to be vaporized until the next full rotation. The laser continues rotating; the next nine to be vaporized are:
+
+.#....###.....#..
+##...##...#.....#
+##...#......1234.
+..#.....X...5##..
+..#.9.....8....76
+The next nine to be vaporized are then:
+
+.8....###.....#..
+56...9#...#.....#
+34...7...........
+..2.....X....##..
+..1..............
+Finally, the laser completes its first full rotation (1 through 3), a second rotation (4 through 8), and vaporizes the last asteroid (9) partway through its third rotation:
+
+......234.....6..
+......1...5.....7
+.................
+........X....89..
+.................
+In the large example above (the one with the best monitoring station location at 11,13):
+
+The 1st asteroid to be vaporized is at 11,12.
+The 2nd asteroid to be vaporized is at 12,1.
+The 3rd asteroid to be vaporized is at 12,2.
+The 10th asteroid to be vaporized is at 12,8.
+The 20th asteroid to be vaporized is at 16,0.
+The 50th asteroid to be vaporized is at 16,9.
+The 100th asteroid to be vaporized is at 10,16.
+The 199th asteroid to be vaporized is at 9,6.
+The 200th asteroid to be vaporized is at 8,2.
+The 201st asteroid to be vaporized is at 10,9.
+The 299th and final asteroid to be vaporized is at 11,1.
+The Elves are placing bets on which will be the 200th asteroid to be vaporized. Win the bet by determining which asteroid that will be; what do you get if you multiply its X coordinate by 100 and then add its Y coordinate? (For example, 8,2 becomes 802.)
diff --git a/src/2019/day10/aoc.cpp b/src/2019/day10/aoc.cpp
index 4d200f4..fbcb049 100644
--- a/src/2019/day10/aoc.cpp
+++ b/src/2019/day10/aoc.cpp
@@ -4,7 +4,29 @@
namespace aoc2019 {
std::pair<int, int> day10(line_view file) {
- return {0, 0};
+ belt b;
+ int r{0};
+ per_line(file, [&b, &r](line_view lv) {
+ b.load(lv, r++);
+ return true;
+ });
+
+ // b.print();
+
+ int observe{0};
+ int max{0};
+
+ b.iterate([&observe, &max, &b](belt::pos p){
+ if (b.get(p) == '#') {
+ observe = 0;
+ b.count(p, &observe);
+ if (observe > max) {
+ max = observe;
+ }
+ }
+ });
+
+ return {max, 0};
}
} // namespace aoc2019
diff --git a/src/2019/day10/aoc.h b/src/2019/day10/aoc.h
index 9951322..d3cdb76 100644
--- a/src/2019/day10/aoc.h
+++ b/src/2019/day10/aoc.h
@@ -1,7 +1,104 @@
#pragma once
#include "common.h"
+#include <math.h>
namespace aoc2019 {
+struct belt {
+ static constexpr int grid = 34;
+
+ struct distance {
+ int dx;
+ int dy;
+ };
+
+ struct pos {
+ int x;
+ int y;
+
+ friend bool operator==(const pos &p1, const pos &p2) {
+ return p1.x == p2.x && p1.y == p2.y;
+ }
+ };
+
+ char map[grid * grid] = {0};
+
+ belt(const belt& b) {
+ memcpy(map, b.map, grid * grid);
+ }
+
+ belt() {}
+
+ char& get(int x, int y) {
+ return map[y * grid + x];
+ }
+
+ char& get(pos p) {
+ return get(p.x, p.y);
+ }
+
+ void load(line_view lv, int row) {
+ const char* p = lv.line;
+ for (int i = 0; i < grid; i++) {
+ get(i, row) = *(p + i);
+ }
+ }
+
+ distance diff(pos px, pos p) const noexcept {
+ auto d = distance{px.x - p.x, px.y - p.y};
+ int g = gcd(std::abs(d.dx), std::abs(d.dy));
+ return distance {d.dx / g, d.dy / g};
+ }
+
+ bool valid (pos p) const noexcept {
+ return p.x >= 0 && p.x < grid && p.y >= 0 && p.y < grid;
+ }
+
+ pos next(pos p, distance d) const noexcept {
+ return pos{p.x + d.dx, p.y + d.dy};
+ }
+
+ bool blocked(pos p, pos m) {
+ auto d = diff(p, m);
+ auto n = next(m, d);
+ while (valid(n) && !(n == p)) {
+ if (get(n) == '#') {
+ return true;
+ }
+ n = next(n, d);
+ }
+ return false;
+ }
+
+ template<typename F, typename... Args>
+ void iterate(F&& f, Args&&... args) {
+ for(int y = 0; y < grid; y++) {
+ for (int x = 0; x < grid; x++) {
+ f(pos{x, y}, std::forward(args)...);
+ }
+ }
+ }
+
+ void print() {
+ iterate([this](pos p){
+ printf("%c", get(p));
+ if (p.x == grid - 1) {
+ printf("\n");
+ }
+ });
+ printf("\n");
+ }
+
+ void count(pos m, int* c) {
+ iterate([this, &m, &c](pos p){
+ if (get(p) == '#' && !(p == m)) {
+ if (!blocked(p, m)) {
+ *c += 1;
+ }
+ }
+ });
+ }
+};
+
std::pair<int, int> day10(line_view);
}
diff --git a/src/2019/day10/input0 b/src/2019/day10/input0
new file mode 100644
index 0000000..987698f
--- /dev/null
+++ b/src/2019/day10/input0
@@ -0,0 +1,10 @@
+......#.#.
+#..#.#....
+..#######.
+.#.#.###..
+.#..#.....
+..#....#.#
+#..#....#.
+.##.#..###
+##...#..#.
+.#....####
diff --git a/src/2019/day10/input1 b/src/2019/day10/input1
new file mode 100644
index 0000000..e28e424
--- /dev/null
+++ b/src/2019/day10/input1
@@ -0,0 +1,10 @@
+#.#...#.#.
+.###....#.
+.#....#...
+##.#.#.#.#
+....#.#.#.
+.##..###.#
+..#...##..
+..##....##
+......#...
+.####.###.
diff --git a/src/2019/day10/input2 b/src/2019/day10/input2
new file mode 100644
index 0000000..af5b6e9
--- /dev/null
+++ b/src/2019/day10/input2
@@ -0,0 +1,10 @@
+.#..#..###
+####.###.#
+....###.#.
+..###.##.#
+##.##.#.#.
+....###..#
+..#.#..#.#
+#..#.#.###
+.##...##.#
+.....#.#..
diff --git a/src/2019/day10/input3 b/src/2019/day10/input3
new file mode 100644
index 0000000..33437ba
--- /dev/null
+++ b/src/2019/day10/input3
@@ -0,0 +1,20 @@
+.#..##.###...#######
+##.############..##.
+.#.######.########.#
+.###.#######.####.#.
+#####.##.#.##.###.##
+..#####..#.#########
+####################
+#.####....###.#.#.##
+##.#################
+#####.##.###..####..
+..######..##.#######
+####.##.####...##..#
+.#####..#.######.###
+##...#.##########...
+#.##########.#######
+.####.#.###.###.#.##
+....##.##.###..#####
+.#.#.###########.###
+#.#.#.#####.####.###
+###.##.####.##.#..##
diff --git a/src/common.cpp b/src/common.cpp
index 1e7984d..956456f 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -51,3 +51,10 @@ bool is_repeated(const char* p1, const char* p2) {
}
return true;
}
+
+int gcd(int a, int b)
+{
+ if (a == 0)
+ return b;
+ return gcd(b % a, a);
+}
diff --git a/src/common.h b/src/common.h
index 8e65a07..9581d82 100644
--- a/src/common.h
+++ b/src/common.h
@@ -153,3 +153,4 @@ struct ascii_count {
};
bool is_repeated(const char* p1, const char* p2);
+int gcd(int, int);