aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-03-15 17:02:03 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-03-15 17:02:03 +0800
commit682316596c43d31da35539d12e63caf1cd39155e (patch)
tree64d7b8ccb5a3936fb5b8ee4f466eb56f720299a6
parentab0d13314e489ad03c226f794c0f22d0c0059549 (diff)
downloadadvent-of-code-682316596c43d31da35539d12e63caf1cd39155e.tar.gz
advent-of-code-682316596c43d31da35539d12e63caf1cd39155e.zip
2017 day24 part1HEADmain
-rw-r--r--src/2017/day24/README.md40
-rw-r--r--src/2017/day24/aoc.cpp13
-rw-r--r--src/2017/day24/aoc.h29
-rw-r--r--src/2017/day24/input56
-rw-r--r--src/2017/day24/input08
-rw-r--r--test/test_2017.cpp4
6 files changed, 146 insertions, 4 deletions
diff --git a/src/2017/day24/README.md b/src/2017/day24/README.md
index e69de29..1f18c33 100644
--- a/src/2017/day24/README.md
+++ b/src/2017/day24/README.md
@@ -0,0 +1,40 @@
+--- Day 24: Electromagnetic Moat ---
+The CPU itself is a large, black building surrounded by a bottomless pit. Enormous metal tubes extend outward from the side of the building at regular intervals and descend down into the void. There's no way to cross, but you need to get inside.
+
+No way, of course, other than building a bridge out of the magnetic components strewn about nearby.
+
+Each component has two ports, one on each end. The ports come in all different types, and only matching types can be connected. You take an inventory of the components by their port types (your puzzle input). Each port is identified by the number of pins it uses; more pins mean a stronger connection for your bridge. A 3/7 component, for example, has a type-3 port on one side, and a type-7 port on the other.
+
+Your side of the pit is metallic; a perfect surface to connect a magnetic, zero-pin port. Because of this, the first port you use must be of type 0. It doesn't matter what type of port you end with; your goal is just to make the bridge as strong as possible.
+
+The strength of a bridge is the sum of the port types in each component. For example, if your bridge is made of components 0/3, 3/7, and 7/4, your bridge has a strength of 0+3 + 3+7 + 7+4 = 24.
+
+For example, suppose you had the following components:
+
+0/2
+2/2
+2/3
+3/4
+3/5
+0/1
+10/1
+9/10
+With them, you could make the following valid bridges:
+
+0/1
+0/1--10/1
+0/1--10/1--9/10
+0/2
+0/2--2/3
+0/2--2/3--3/4
+0/2--2/3--3/5
+0/2--2/2
+0/2--2/2--2/3
+0/2--2/2--2/3--3/4
+0/2--2/2--2/3--3/5
+(Note how, as shown by 10/1, order of ports within a component doesn't matter. However, you may only use each port on a component once.)
+
+Of these bridges, the strongest one is 0/1--10/1--9/10; it has a strength of 0+1 + 1+10 + 10+9 = 31.
+
+What is the strength of the strongest bridge you can make with the components you have available?
+
diff --git a/src/2017/day24/aoc.cpp b/src/2017/day24/aoc.cpp
index b4e87c1..8216137 100644
--- a/src/2017/day24/aoc.cpp
+++ b/src/2017/day24/aoc.cpp
@@ -2,5 +2,16 @@
namespace aoc2017 {
-std::pair<int64_t, int64_t> day24(line_view) { return {0, 0}; }
+std::pair<int64_t, int64_t> day24(line_view file) {
+ std::vector<component> cs;
+ per_line(file, [&cs](line_view lv) {
+ cs.emplace_back(lv);
+ return true;
+ });
+
+ // for (auto& c: cs) {
+ // c.print();
+ // }
+ return {0, 0};
+}
} // namespace aoc2017
diff --git a/src/2017/day24/aoc.h b/src/2017/day24/aoc.h
index d102bfa..9643b78 100644
--- a/src/2017/day24/aoc.h
+++ b/src/2017/day24/aoc.h
@@ -3,5 +3,32 @@
#include <vector>
namespace aoc2017 {
+
+struct component {
+ int pins[2] = {0};
+
+ void get_number(const char** pp, int* d) {
+ const char* p = *pp;
+ while (*p >= '0' && *p <= '9') {
+ *d = *d * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+ }
+
+ void print() const noexcept { printf("%d/%d\n", pins[0], pins[1]); }
+
+ component(line_view lv) {
+ int i{0};
+ const char* p = lv.line;
+ while (p < lv.line + lv.length) {
+ if (*p >= '0' && *p <= '9') {
+ get_number(&p, &pins[i++]);
+ }
+ p++;
+ }
+ }
+};
+
std::pair<int64_t, int64_t> day24(line_view);
-}
+} // namespace aoc2017
diff --git a/src/2017/day24/input b/src/2017/day24/input
index e69de29..441ee31 100644
--- a/src/2017/day24/input
+++ b/src/2017/day24/input
@@ -0,0 +1,56 @@
+32/31
+2/2
+0/43
+45/15
+33/24
+20/20
+14/42
+2/35
+50/27
+2/17
+5/45
+3/14
+26/1
+33/38
+29/6
+50/32
+9/48
+36/34
+33/50
+37/35
+12/12
+26/13
+19/4
+5/5
+14/46
+17/29
+45/43
+5/0
+18/18
+41/22
+50/3
+4/4
+17/1
+40/7
+19/0
+33/7
+22/48
+9/14
+50/43
+26/29
+19/33
+46/31
+3/16
+29/46
+16/0
+34/17
+31/7
+5/27
+7/4
+49/49
+14/21
+50/9
+14/44
+29/29
+13/38
+31/11
diff --git a/src/2017/day24/input0 b/src/2017/day24/input0
index e69de29..f2c8f2a 100644
--- a/src/2017/day24/input0
+++ b/src/2017/day24/input0
@@ -0,0 +1,8 @@
+0/2
+2/2
+2/3
+3/4
+3/5
+0/1
+10/1
+9/10
diff --git a/test/test_2017.cpp b/test/test_2017.cpp
index 2311016..5ccb526 100644
--- a/test/test_2017.cpp
+++ b/test/test_2017.cpp
@@ -221,12 +221,12 @@ TEST_CASE("Sporifica Virus", "[2017]") {
TEST_CASE("Coprocessor Conflagration", "[2017]") {
line_view lv = load_file("../src/2017/day23/input");
auto p = aoc2017::day23(lv);
- // REQUIRE(4225 == p.first);
+ REQUIRE(4225 == p.first);
REQUIRE(0 == p.second);
}
-TEST_CASE("", "[2017]") {
+TEST_CASE("Electromagnetic Moat", "[2017]") {
line_view lv = load_file("../src/2017/day24/input");
auto p = aoc2017::day24(lv);
REQUIRE(0 == p.first);