aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-26 11:20:18 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-26 11:20:18 +0800
commitce1b89ccb424e920447115478ae3f7fcc024608a (patch)
tree3a3f8b568d6d35a5daea4d46fc402689aa008873
parentc4bb3048a264c04fcd4ae14c0a615c8ff32695c8 (diff)
downloadadvent-of-code-ce1b89ccb424e920447115478ae3f7fcc024608a.tar.gz
advent-of-code-ce1b89ccb424e920447115478ae3f7fcc024608a.zip
2016 day20
-rw-r--r--src/2016/day20/README.md4
-rw-r--r--src/2016/day20/aoc.cpp83
-rw-r--r--src/2016/day20/input1
-rw-r--r--src/2016/day21/README.md29
-rw-r--r--src/2016/day21/input100
-rw-r--r--test/test_2016.cpp4
6 files changed, 217 insertions, 4 deletions
diff --git a/src/2016/day20/README.md b/src/2016/day20/README.md
index 3ba39ac..202f11a 100644
--- a/src/2016/day20/README.md
+++ b/src/2016/day20/README.md
@@ -14,3 +14,7 @@ The blacklist specifies ranges of IPs (inclusive of both the start and end value
Given the list of blocked IPs you retrieved from the firewall (your puzzle input), what is the lowest-valued IP that is not blocked?
+--- Part Two ---
+
+How many IPs are allowed by the blacklist?
+
diff --git a/src/2016/day20/aoc.cpp b/src/2016/day20/aoc.cpp
index 2a438aa..16e3e0a 100644
--- a/src/2016/day20/aoc.cpp
+++ b/src/2016/day20/aoc.cpp
@@ -1,6 +1,87 @@
#include "aoc.h"
+#include <algorithm>
namespace aoc2016 {
-std::pair<int64_t, int64_t> day20(line_view) { return {0, 0}; }
+static void get_number(const char** pp, uint32_t* d) {
+ const char* p = *pp;
+ while (*p >= '0' && *p <= '9') {
+ *d = *d * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+}
+
+struct ipdigit {
+ uint32_t ds[2] = {0};
+
+ void print() const noexcept { printf("%u-%u\n", ds[0], ds[1]); }
+ ipdigit(line_view lv) {
+ const char* p = lv.line;
+ int i{0};
+ while (p < lv.line + lv.length) {
+ if (*p >= '0' && *p <= '9') {
+ get_number(&p, ds + i);
+ i++;
+ }
+ p++;
+ }
+ }
+
+ friend bool operator<(ipdigit ip1, ipdigit ip2) {
+ return ip1.ds[0] < ip2.ds[0] ? true : ip1.ds[0] > ip2.ds[0] ? false : ip1.ds[1] < ip2.ds[1];
+ }
+};
+
+static bool merge(ipdigit& ip, const ipdigit& ipx) {
+ if (ip.ds[1] >= ipx.ds[1]) {
+ return true;
+ }
+ if (ip.ds[1] < ipx.ds[1] && ip.ds[1] + 1 >= ipx.ds[0]) {
+ ip.ds[1] = ipx.ds[1];
+ return true;
+ }
+ return false;
+}
+
+static std::vector<ipdigit> merge(const std::vector<ipdigit>& ips) {
+ std::vector<ipdigit> ds;
+ ipdigit ip = ips[0];
+ for (size_t i = 1; i < ips.size(); i++) {
+ if (!merge(ip, ips[i])) {
+ ds.push_back(ip);
+ ip = ips[i];
+ }
+ }
+ ds.push_back(ip);
+ return ds;
+}
+
+static size_t count(std::vector<ipdigit>& ips) {
+ size_t c{0};
+ for (size_t i = 1; i < ips.size(); i++) {
+ c += ips[i].ds[0] - ips[i - 1].ds[1] - 1;
+ }
+ uint32_t max{UINT32_MAX};
+ auto& last = ips[ips.size() - 1];
+ c += max - last.ds[1];
+ return c;
+}
+
+std::pair<int64_t, int64_t> day20(line_view file) {
+ std::vector<ipdigit> ips;
+ per_line(file, [&ips](line_view lv) {
+ ips.emplace_back(lv);
+ return true;
+ });
+
+ std::sort(ips.begin(), ips.end());
+ auto ds = merge(ips);
+ // printf("%zu\n", count(ds));
+ // for (auto ip : ds) {
+ // ip.print();
+ // }
+
+ return {0, count(ds)};
+}
} // namespace aoc2016
diff --git a/src/2016/day20/input b/src/2016/day20/input
index 273be89..577cd04 100644
--- a/src/2016/day20/input
+++ b/src/2016/day20/input
@@ -956,4 +956,3 @@
2497969870-2535505355
1671431570-1689108789
151189360-154214893
-
diff --git a/src/2016/day21/README.md b/src/2016/day21/README.md
index e69de29..9d6aa4c 100644
--- a/src/2016/day21/README.md
+++ b/src/2016/day21/README.md
@@ -0,0 +1,29 @@
+--- Day 21: Scrambled Letters and Hash ---
+
+The computer system you're breaking into uses a weird scrambling function to store its passwords. It shouldn't be much trouble to create your own scrambled password so you can add it to the system; you just have to implement the scrambler.
+
+The scrambling function is a series of operations (the exact list is provided in your puzzle input). Starting with the password to be scrambled, apply each operation in succession to the string. The individual operations behave as follows:
+
+ swap position X with position Y means that the letters at indexes X and Y (counting from 0) should be swapped.
+ swap letter X with letter Y means that the letters X and Y should be swapped (regardless of where they appear in the string).
+ rotate left/right X steps means that the whole string should be rotated; for example, one right rotation would turn abcd into dabc.
+ rotate based on position of letter X means that the whole string should be rotated to the right based on the index of letter X (counting from 0) as determined before this instruction does any rotations. Once the index is determined, rotate the string to the right one time, plus a number of times equal to that index, plus one additional time if the index was at least 4.
+ reverse positions X through Y means that the span of letters at indexes X through Y (including the letters at X and Y) should be reversed in order.
+ move position X to position Y means that the letter which is at index X should be removed from the string, then inserted such that it ends up at index Y.
+
+For example, suppose you start with abcde and perform the following operations:
+
+ swap position 4 with position 0 swaps the first and last letters, producing the input for the next step, ebcda.
+ swap letter d with letter b swaps the positions of d and b: edcba.
+ reverse positions 0 through 4 causes the entire string to be reversed, producing abcde.
+ rotate left 1 step shifts all letters left one position, causing the first letter to wrap to the end of the string: bcdea.
+ move position 1 to position 4 removes the letter at position 1 (c), then inserts it at position 4 (the end of the string): bdeac.
+ move position 3 to position 0 removes the letter at position 3 (a), then inserts it at position 0 (the front of the string): abdec.
+ rotate based on position of letter b finds the index of letter b (1), then rotates the string right once plus a number of times equal to that index (2): ecabd.
+ rotate based on position of letter d finds the index of letter d (4), then rotates the string right once, plus a number of times equal to that index, plus an additional time because the index was at least 4, for a total of 6 right rotations: decab.
+
+After these steps, the resulting scrambled password is decab.
+
+Now, you just need to generate a new scrambled password and you can access the system. Given the list of scrambling operations in your puzzle input, what is the result of scrambling abcdefgh?
+
+
diff --git a/src/2016/day21/input b/src/2016/day21/input
index e69de29..249bd2c 100644
--- a/src/2016/day21/input
+++ b/src/2016/day21/input
@@ -0,0 +1,100 @@
+swap position 5 with position 6
+reverse positions 1 through 6
+rotate right 7 steps
+rotate based on position of letter c
+rotate right 7 steps
+reverse positions 0 through 4
+swap letter f with letter h
+reverse positions 1 through 2
+move position 1 to position 0
+rotate based on position of letter f
+move position 6 to position 3
+reverse positions 3 through 6
+rotate based on position of letter c
+rotate based on position of letter b
+move position 2 to position 4
+swap letter b with letter d
+move position 1 to position 6
+move position 7 to position 1
+swap letter f with letter c
+move position 2 to position 3
+swap position 1 with position 7
+reverse positions 3 through 5
+swap position 1 with position 4
+move position 4 to position 7
+rotate right 4 steps
+reverse positions 3 through 6
+move position 0 to position 6
+swap position 3 with position 5
+swap letter e with letter h
+rotate based on position of letter c
+swap position 4 with position 7
+reverse positions 0 through 5
+rotate right 5 steps
+rotate left 0 steps
+rotate based on position of letter f
+swap letter e with letter b
+rotate right 2 steps
+rotate based on position of letter c
+swap letter a with letter e
+rotate left 4 steps
+rotate left 0 steps
+move position 6 to position 7
+rotate right 2 steps
+rotate left 6 steps
+rotate based on position of letter d
+swap letter a with letter b
+move position 5 to position 4
+reverse positions 0 through 7
+rotate left 3 steps
+rotate based on position of letter e
+rotate based on position of letter h
+swap position 4 with position 6
+reverse positions 4 through 5
+reverse positions 5 through 7
+rotate left 3 steps
+move position 7 to position 2
+move position 3 to position 4
+swap letter b with letter d
+reverse positions 3 through 4
+swap letter e with letter a
+rotate left 4 steps
+swap position 3 with position 4
+swap position 7 with position 5
+rotate right 1 step
+rotate based on position of letter g
+reverse positions 0 through 3
+swap letter g with letter b
+rotate based on position of letter b
+swap letter a with letter c
+swap position 0 with position 2
+reverse positions 1 through 3
+rotate left 7 steps
+swap letter f with letter a
+move position 5 to position 0
+reverse positions 1 through 5
+rotate based on position of letter d
+rotate based on position of letter c
+rotate left 2 steps
+swap letter b with letter a
+swap letter f with letter c
+swap letter h with letter f
+rotate based on position of letter b
+rotate left 3 steps
+swap letter b with letter h
+reverse positions 1 through 7
+rotate based on position of letter h
+swap position 1 with position 5
+rotate left 1 step
+rotate based on position of letter h
+reverse positions 0 through 1
+swap position 5 with position 7
+reverse positions 0 through 2
+reverse positions 1 through 3
+move position 1 to position 4
+reverse positions 1 through 3
+rotate left 1 step
+swap position 4 with position 1
+move position 1 to position 3
+rotate right 2 steps
+move position 0 to position 5
diff --git a/test/test_2016.cpp b/test/test_2016.cpp
index 14e2188..d2dec77 100644
--- a/test/test_2016.cpp
+++ b/test/test_2016.cpp
@@ -178,11 +178,11 @@ TEST_CASE("An Elephant Named Joseph", "[2016]") {
}
-TEST_CASE("", "[2016]") {
+TEST_CASE("Firewall Rules", "[2016]") {
line_view lv = load_file("../src/2016/day20/input");
auto p = aoc2016::day20(lv);
REQUIRE(0 == p.first);
- REQUIRE(0 == p.second);
+ REQUIRE(104 == p.second);
}