From 8c69b5d33a35e8ad7530b57a19431ebdf45e5e4c Mon Sep 17 00:00:00 2001 From: Junyan Zhang Date: Sun, 26 Nov 2023 22:53:56 +0800 Subject: [PATCH 1/2] Create readme.md --- zhangjunyan2580-cpp/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 zhangjunyan2580-cpp/readme.md diff --git a/zhangjunyan2580-cpp/readme.md b/zhangjunyan2580-cpp/readme.md new file mode 100644 index 00000000..151430ef --- /dev/null +++ b/zhangjunyan2580-cpp/readme.md @@ -0,0 +1 @@ +solved From 1f13bf905f0602870621ebf27e9f68328cdcc99a Mon Sep 17 00:00:00 2001 From: Junyan Zhang Date: Sun, 26 Nov 2023 22:56:04 +0800 Subject: [PATCH 2/2] Add all days --- day10_1.cpp | 29 ++++++ day10_2.cpp | 47 ++++++++++ day11_1.cpp | 41 +++++++++ day11_2.cpp | 44 +++++++++ day12_1.cpp | 32 +++++++ day12_2.cpp | 38 ++++++++ day13_1.cpp | 18 ++++ day13_2.cpp | 24 +++++ day14_1.cpp | 61 +++++++++++++ day14_2.cpp | 81 +++++++++++++++++ day15_1.cpp | 17 ++++ day15_2.cpp | 20 +++++ day16_1.cpp | 33 +++++++ day16_2.cpp | 45 ++++++++++ day17_1.cpp | 16 ++++ day17_2.cpp | 60 +++++++++++++ day18_1.cpp | 52 +++++++++++ day18_2.cpp | 70 +++++++++++++++ day19_1.cpp | 33 +++++++ day19_2.cpp | 32 +++++++ day1_1.cpp | 15 ++++ day1_2.cpp | 16 ++++ day20_1.cpp | 44 +++++++++ day20_2.cpp | 91 +++++++++++++++++++ day21_1.cpp | 138 +++++++++++++++++++++++++++++ day21_2.cpp | 138 +++++++++++++++++++++++++++++ day22_1.cpp | 30 +++++++ day22_2.cpp | 32 +++++++ day23_1.cpp | 45 ++++++++++ day23_2.cpp | 6 ++ day24_1.cpp | 26 ++++++ day24_2.cpp | 26 ++++++ day25_1.cpp | 41 +++++++++ day2_1.cpp | 20 +++++ day2_2.cpp | 18 ++++ day3_1.cpp | 16 ++++ day3_2.cpp | 29 ++++++ day4_1.cpp | 18 ++++ day4_2.cpp | 19 ++++ day5_1.cpp | 11 +++ day5_2.cpp | 11 +++ day6_1.cpp | 31 +++++++ day6_2.cpp | 32 +++++++ day7_1.cpp | 37 ++++++++ day7_2.cpp | 52 +++++++++++ day8_1.cpp | 35 ++++++++ day9_1.cpp | 24 +++++ day9_2.cpp | 25 ++++++ pystrlib.hpp | 245 +++++++++++++++++++++++++++++++++++++++++++++++++++ 49 files changed, 2064 insertions(+) create mode 100644 day10_1.cpp create mode 100644 day10_2.cpp create mode 100644 day11_1.cpp create mode 100644 day11_2.cpp create mode 100644 day12_1.cpp create mode 100644 day12_2.cpp create mode 100644 day13_1.cpp create mode 100644 day13_2.cpp create mode 100644 day14_1.cpp create mode 100644 day14_2.cpp create mode 100644 day15_1.cpp create mode 100644 day15_2.cpp create mode 100644 day16_1.cpp create mode 100644 day16_2.cpp create mode 100644 day17_1.cpp create mode 100644 day17_2.cpp create mode 100644 day18_1.cpp create mode 100644 day18_2.cpp create mode 100644 day19_1.cpp create mode 100644 day19_2.cpp create mode 100644 day1_1.cpp create mode 100644 day1_2.cpp create mode 100644 day20_1.cpp create mode 100644 day20_2.cpp create mode 100644 day21_1.cpp create mode 100644 day21_2.cpp create mode 100644 day22_1.cpp create mode 100644 day22_2.cpp create mode 100644 day23_1.cpp create mode 100644 day23_2.cpp create mode 100644 day24_1.cpp create mode 100644 day24_2.cpp create mode 100644 day25_1.cpp create mode 100644 day2_1.cpp create mode 100644 day2_2.cpp create mode 100644 day3_1.cpp create mode 100644 day3_2.cpp create mode 100644 day4_1.cpp create mode 100644 day4_2.cpp create mode 100644 day5_1.cpp create mode 100644 day5_2.cpp create mode 100644 day6_1.cpp create mode 100644 day6_2.cpp create mode 100644 day7_1.cpp create mode 100644 day7_2.cpp create mode 100644 day8_1.cpp create mode 100644 day9_1.cpp create mode 100644 day9_2.cpp create mode 100644 pystrlib.hpp diff --git a/day10_1.cpp b/day10_1.cpp new file mode 100644 index 00000000..6eecb843 --- /dev/null +++ b/day10_1.cpp @@ -0,0 +1,29 @@ +#include +#include "pystrlib.hpp" +#include + +std::vector list; +int n = 256, skip_size, position; + +inline void rotate_left(int r) { + r %= n; if (r < 0) r += n; + list.insert(list.end(), list.begin(), list.begin() + r); + list.erase(list.begin(), list.begin() + r); +} + +int main() { + freopen("day10.txt", "r", stdin); + for (int i = 0; i < n; ++i) list.push_back(i); + { + std::string line; + std::cin >> line; + for (const std::string &q : lib::split(line, ",")) { + int len = std::stoi(q); + std::reverse(list.begin(), list.begin() + len); + rotate_left(len + skip_size); position += len + skip_size++; + } + } + rotate_left(-position); + std::cout << list[0] * list[1] << std::endl; + return 0; +} \ No newline at end of file diff --git a/day10_2.cpp b/day10_2.cpp new file mode 100644 index 00000000..9666de7e --- /dev/null +++ b/day10_2.cpp @@ -0,0 +1,47 @@ +#include +#include "pystrlib.hpp" +#include +#include + +std::vector list, len; +int n = 256, skip_size, position; + +inline void rotate_left(int r) { + r %= n; if (r < 0) r += n; + list.insert(list.end(), list.begin(), list.begin() + r); + list.erase(list.begin(), list.begin() + r); +} + +void round() { + for (int i : len) { + std::reverse(list.begin(), list.begin() + i); + rotate_left(i + skip_size); position += i + skip_size++; + } +} + +int main() { + freopen("day10.txt", "r", stdin); + for (int i = 0; i < n; ++i) list.push_back(i); + { + std::string line; + std::getline(std::cin, line); + for (char q : line) + len.push_back(q); + len.push_back(17); + len.push_back(31); + len.push_back(73); + len.push_back(47); + len.push_back(23); + } + for (int i = 0; i < 64; ++i) + round(); + rotate_left(-position); + for (int i = 0; i < 256; i += 16) { + int s = 0; + for (int j = i; j < i + 16; ++j) + s ^= list[j]; + std::cout << std::hex << std::setw(2) << std::setfill('0') << s; + } + std::cout << std::endl; + return 0; +} \ No newline at end of file diff --git a/day11_1.cpp b/day11_1.cpp new file mode 100644 index 00000000..8ecad70b --- /dev/null +++ b/day11_1.cpp @@ -0,0 +1,41 @@ +// se: (1, 0) +// ne: (0, 1) +// n: (-1, 1) +// nw: (-1, 0) +// sw: (0, -1) +// s: (1, -1) + +// \| +// -s- +// |\ + +#include +#include "pystrlib.hpp" +#include + +constexpr int dx[6] = { 1, 0, -1, -1, 0, 1 }, dy[6] = { 0, 1, 1, 0, -1, -1 }; +int x, y; + +inline int get_d(const std::string &str) { + if (str == "se") return 0; + if (str == "ne") return 1; + if (str == "n") return 2; + if (str == "nw") return 3; + if (str == "sw") return 4; + if (str == "s") return 5; + return -1; +} + +int main() { + freopen("day11.txt", "r", stdin); + { + std::string line; std::cin >> line; + std::vector steps = lib::split(line, ","); + for (const std::string &str : steps) { + int d = get_d(str); + x += dx[d]; y += dy[d]; + } + } + std::cout << ((x * y >= 0) ? (std::abs(x) + std::abs(y)) : (std::max(std::abs(x), std::abs(y)))) << std::endl; + return 0; +} \ No newline at end of file diff --git a/day11_2.cpp b/day11_2.cpp new file mode 100644 index 00000000..43640ad5 --- /dev/null +++ b/day11_2.cpp @@ -0,0 +1,44 @@ +// se: (1, 0) +// ne: (0, 1) +// n: (-1, 1) +// nw: (-1, 0) +// sw: (0, -1) +// s: (1, -1) + +// \| +// -s- +// |\ + +#include +#include "pystrlib.hpp" +#include + +constexpr int dx[6] = { 1, 0, -1, -1, 0, 1 }, dy[6] = { 0, 1, 1, 0, -1, -1 }; +int x, y; + +inline int get_d(const std::string &str) { + if (str == "se") return 0; + if (str == "ne") return 1; + if (str == "n") return 2; + if (str == "nw") return 3; + if (str == "sw") return 4; + if (str == "s") return 5; + return -1; +} + +int dist(int x, int y) { return (x * y >= 0) ? (std::abs(x) + std::abs(y)) : (std::max(std::abs(x), std::abs(y))); } + +int main() { + freopen("day11.txt", "r", stdin); + int ans = 0; + { + std::string line; std::cin >> line; + std::vector steps = lib::split(line, ","); + for (const std::string &str : steps) { + int d = get_d(str); + x += dx[d]; y += dy[d]; ans = std::max(ans, dist(x, y)); + } + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day12_1.cpp b/day12_1.cpp new file mode 100644 index 00000000..7dc891f6 --- /dev/null +++ b/day12_1.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include "pystrlib.hpp" + +struct edge { int to, nxt; } g[20005]; +int head[2000], cnt; + +inline void add_edge(int f, int t) { g[++cnt] = (edge) { t, head[f] }; head[f] = cnt; } + +std::queue Q; +int V[2000], n; + +int main() { + freopen("day12.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector to = lib::split(std::get<2>(lib::partition(line, " <-> ")), ", "); + for (const std::string &str : to) add_edge(n, std::stoi(str)); + ++n; + } + } + Q.push(0); V[0] = 1; + while (!Q.empty()) { + int u = Q.front(); Q.pop(); + for (int i = head[u]; i; i = g[i].nxt) + if (!V[g[i].to]) V[g[i].to] = 1, Q.push(g[i].to); + } + std::cout << std::accumulate(V, V + n, 0) << std::endl; + return 0; +} \ No newline at end of file diff --git a/day12_2.cpp b/day12_2.cpp new file mode 100644 index 00000000..5c4d8116 --- /dev/null +++ b/day12_2.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include "pystrlib.hpp" + +struct edge { int to, nxt; } g[20005]; +int head[2000], cnt; + +inline void add_edge(int f, int t) { g[++cnt] = (edge) { t, head[f] }; head[f] = cnt; } + +std::queue Q; +int V[2000], n; + +void bfs(int s) { + Q.push(s); V[s] = 1; + while (!Q.empty()) { + int u = Q.front(); Q.pop(); + for (int i = head[u]; i; i = g[i].nxt) + if (!V[g[i].to]) V[g[i].to] = 1, Q.push(g[i].to); + } +} + +int main() { + freopen("day12.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector to = lib::split(std::get<2>(lib::partition(line, " <-> ")), ", "); + for (const std::string &str : to) add_edge(n, std::stoi(str)); + ++n; + } + } + int ans = 0; + for (int i = 0; i < n; ++i) + if (!V[i]) ++ans, bfs(i); + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day13_1.cpp b/day13_1.cpp new file mode 100644 index 00000000..95ee8980 --- /dev/null +++ b/day13_1.cpp @@ -0,0 +1,18 @@ +#include +#include "pystrlib.hpp" +#include + +int main() { + freopen("day13.txt", "r", stdin); + int ans = 0; + { + std::string line; + while (std::getline(std::cin, line)) { + auto [D, _, R] = lib::partition(line, ": "); + int d = std::stoi(D), r = std::stoi(R); + !(d % ((r - 1) << 1)) && (ans += d * r); + } + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day13_2.cpp b/day13_2.cpp new file mode 100644 index 00000000..64b1dec3 --- /dev/null +++ b/day13_2.cpp @@ -0,0 +1,24 @@ +#include +#include "pystrlib.hpp" +#include + +std::vector< std::pair > wall; + +bool pass(int x) { + for (const auto &[d, r] : wall) + if (!((x + d) % r)) return false; + return true; +} + +int main() { + freopen("day13.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + auto [D, _, R] = lib::partition(line, ": "); + wall.emplace_back(std::stoi(D), (std::stoi(R) - 1) << 1); + } + } + for (int i = 0; ; ++i) + if (pass(i)) { std::cout << i << std::endl; return 0; } +} \ No newline at end of file diff --git a/day14_1.cpp b/day14_1.cpp new file mode 100644 index 00000000..8c81a3fb --- /dev/null +++ b/day14_1.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +namespace hash { + +std::vector list, len; +int n = 256, skip_size, position; + +inline void rotate_left(int r) { + r %= n; if (r < 0) r += n; + list.insert(list.end(), list.begin(), list.begin() + r); + list.erase(list.begin(), list.begin() + r); +} + +void round() { + for (int i : len) { + std::reverse(list.begin(), list.begin() + i); + rotate_left(i + skip_size); position += i + skip_size++; + } +} + +std::bitset<128> hash(const std::string &str) { + list.clear(); len.clear(); skip_size = 0; position = 0; + for (int i = 0; i < n; ++i) list.push_back(i); + { + for (char q : str) + len.push_back(q); + len.push_back(17); + len.push_back(31); + len.push_back(73); + len.push_back(47); + len.push_back(23); + } + for (int i = 0; i < 64; ++i) + round(); + rotate_left(-position); + std::bitset<128> final; + for (int i = 0; i < 256; i += 16) { + int s = 0, p = i >> 1; + for (int j = i; j < i + 16; ++j) + s ^= list[j]; + for (int j = p + 7; j >= p; --j) + final[j] = s & 1, s >>= 1; + } + return final; +} + +} + +std::string key; +int ans; + +int main() { + std::cin >> key; key += "-"; + for (int i = 0; i < 128; ++i) + ans += hash::hash(key + std::to_string(i)).count(); + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day14_2.cpp b/day14_2.cpp new file mode 100644 index 00000000..5cacf09b --- /dev/null +++ b/day14_2.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +namespace hash { + +std::vector list, len; +int n = 256, skip_size, position; + +inline void rotate_left(int r) { + r %= n; if (r < 0) r += n; + list.insert(list.end(), list.begin(), list.begin() + r); + list.erase(list.begin(), list.begin() + r); +} + +void round() { + for (int i : len) { + std::reverse(list.begin(), list.begin() + i); + rotate_left(i + skip_size); position += i + skip_size++; + } +} + +std::bitset<128> hash(const std::string &str) { + list.clear(); len.clear(); skip_size = 0; position = 0; + for (int i = 0; i < n; ++i) list.push_back(i); + { + for (char q : str) + len.push_back(q); + len.push_back(17); + len.push_back(31); + len.push_back(73); + len.push_back(47); + len.push_back(23); + } + for (int i = 0; i < 64; ++i) + round(); + rotate_left(-position); + std::bitset<128> final; + for (int i = 0; i < 256; i += 16) { + int s = 0, p = i >> 1; + for (int j = i; j < i + 16; ++j) + s ^= list[j]; + for (int j = p + 7; j >= p; --j) + final[j] = s & 1, s >>= 1; + } + return final; +} + +} + +std::string key; +int ans; + +std::bitset<128> map[128]; +std::queue< std::pair > Q; + +constexpr int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }; + +void bfs(int x, int y) { + Q.emplace(x, y); map[x][y] = 0; + while (!Q.empty()) { + auto [x, y] = Q.front(); Q.pop(); + for (int i = 0; i < 4; ++i) { + int nx = x + dx[i], ny = y + dy[i]; + if (nx < 0 || nx >= 128 || ny < 0 || ny >= 128 || !map[nx][ny]) continue; + map[nx][ny] = 0; Q.emplace(nx, ny); + } + } +} + +int main() { + std::cin >> key; key += "-"; + for (int i = 0; i < 128; ++i) map[i] = hash::hash(key + std::to_string(i)); + for (int i = 0; i < 128; ++i) + for (int j = 0; j < 128; ++j) + if (map[i][j]) ++ans, bfs(i, j); + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day15_1.cpp b/day15_1.cpp new file mode 100644 index 00000000..fe8cdc4e --- /dev/null +++ b/day15_1.cpp @@ -0,0 +1,17 @@ +#include +#include + +std::minstd_rand0 A; +std::minstd_rand B; + +int n = 40000000, ans; + +int main() { + int a, b; + std::cin >> a >> b; + A.seed(a); B.seed(b); + for (; n; --n) + if ((A() & 65535) == (B() & 65535)) ++ans; + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day15_2.cpp b/day15_2.cpp new file mode 100644 index 00000000..328a1c69 --- /dev/null +++ b/day15_2.cpp @@ -0,0 +1,20 @@ +#include +#include + +std::minstd_rand0 A; +std::minstd_rand B; + +int n = 5000000, ans; + +int main() { + int a, b; + std::cin >> a >> b; + A.seed(a); B.seed(b); + for (; n; --n) { + int a = A(); while (a & 3) a = A(); + int b = B(); while (b & 7) b = B(); + if ((a & 65535) == (b & 65535)) ++ans; + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day16_1.cpp b/day16_1.cpp new file mode 100644 index 00000000..5ebe8133 --- /dev/null +++ b/day16_1.cpp @@ -0,0 +1,33 @@ +#include +#include "pystrlib.hpp" +#include + +std::string str; + +int n = 16; + +inline void rotate_left(int r) { + r %= n; (r < 0) && (r += n); (r >= n) && (r -= n); + str = str.substr(n - r) + str.substr(0, n - r); +} + +inline void swap(int a, int b) { std::swap(str[a], str[b]); } +inline int index(char ch) { return str.find(ch); } + +int main() { + freopen("day16.txt", "r", stdin); + for (int i = 0; i < n; ++i) str += 'a' + i; + { + std::string line; + std::cin >> line; + std::vector op = lib::split(line, ","); + for (const std::string &str : op) + if (str[0] == 's') rotate_left(std::stoi(str.substr(1))); + else if (str[0] == 'x') { + auto [x, _, y] = lib::partition(str.substr(1), "/"); + swap(std::stoi(x), std::stoi(y)); + } else if (str[0] == 'p') swap(index(str[1]), index(str[3])); + } + std::cout << str << std::endl; + return 0; +} \ No newline at end of file diff --git a/day16_2.cpp b/day16_2.cpp new file mode 100644 index 00000000..7d745db1 --- /dev/null +++ b/day16_2.cpp @@ -0,0 +1,45 @@ +#include +#include "pystrlib.hpp" +#include + +std::string str; + +int n = 16; + +inline void rotate_left(int r) { + r %= n; (r < 0) && (r += n); (r >= n) && (r -= n); + str = str.substr(n - r) + str.substr(0, n - r); +} + +inline void swap(int a, int b) { std::swap(str[a], str[b]); } +inline int index(char ch) { return str.find(ch); } + +int to[16], vis[16], from[16]; +std::vector op; + +void do_round() { + for (const std::string &str : op) + if (str[0] == 's') rotate_left(std::stoi(str.substr(1))); + else if (str[0] == 'x') { + auto [x, _, y] = lib::partition(str.substr(1), "/"); + swap(std::stoi(x), std::stoi(y)); + } else if (str[0] == 'p') swap(index(str[1]), index(str[3])); +} + +int main() { + freopen("day16.txt", "r", stdin); + for (int i = 0; i < n; ++i) str += 'a' + i; + { + std::string line; + std::cin >> line; + op = std::move(lib::split(line, ",")); + } + std::vector states; + states.push_back(str); + do do_round(), states.push_back(str); while (states.back() != states.front()); + states.pop_back(); + std::cout << states.size() << ' ' << states[1000000000 % states.size()] << std::endl; + return 0; +} + +// Do some research diff --git a/day17_1.cpp b/day17_1.cpp new file mode 100644 index 00000000..0b82d47d --- /dev/null +++ b/day17_1.cpp @@ -0,0 +1,16 @@ +#include + +int len; + +struct Node { int nxt, val; } lst[3000]; +int pos, cnt; + +int main() { + std::cin >> len; + for (int i = 1; i <= 2017; ++i) { + for (int j = 0; j < len; ++j) pos = lst[pos].nxt; + lst[++cnt].val = i; lst[cnt].nxt = lst[pos].nxt; lst[pos].nxt = cnt; pos = cnt; + } + std::cout << lst[lst[pos].nxt].val << std::endl; + return 0; +} \ No newline at end of file diff --git a/day17_2.cpp b/day17_2.cpp new file mode 100644 index 00000000..8e72d3d5 --- /dev/null +++ b/day17_2.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +struct Node { + int ls, rs, val, siz; + unsigned key; +} tr[50000005]; +int root, cnt; +std::mt19937 RND; + +inline void push_up(int k) { tr[k].siz = tr[tr[k].ls].siz + tr[tr[k].rs].siz + 1; } +inline int new_node(int v) { tr[++cnt] = (Node) { 0, 0, v, 1, RND()}; return cnt; } + +void split(int k, int sz, int &x, int &y) { + if (!k) { x = y = 0; return; } + if (tr[tr[k].ls].siz < sz) x = k, split(tr[k].rs, sz - tr[tr[k].ls].siz - 1, tr[k].rs, y); + else y = k, split(tr[k].ls, sz, x, tr[k].ls); + push_up(k); +} +int merge(int x, int y) { + if (!x || !y) return x | y; + if (tr[x].key < tr[y].key) { tr[x].rs = merge(tr[x].rs, y); push_up(x); return x; } + else { tr[y].ls = merge(x, tr[y].ls); push_up(y); return y; } +} + +void insert(int pos, int val) { + int x, y; + split(root, pos, x, y); + root = merge(merge(x, new_node(val)), y); +} + +void traverse(int k, std::vector &r) { + if (tr[k].ls) traverse(tr[k].ls, r); + r.push_back(tr[k].val); + if (tr[k].rs) traverse(tr[k].rs, r); +} +std::vector to_vec() { + std::vector r; + traverse(root, r); + return r; +} + +int len, pos; + +int main() { + std::cin >> len; + insert(0, 0); pos = 0; + for (int i = 1; i <= 50000000; ++i) { + pos = (pos + len) % tr[root].siz; + insert(++pos, i); + } + std::vector num = to_vec(); + for (int i = 0; i <= 50000000; ++i) + if (num[i] == 0) { + std::cout << num[(i + 1) % 50000001] << std::endl; + return 0; + } + return 0; +} \ No newline at end of file diff --git a/day18_1.cpp b/day18_1.cpp new file mode 100644 index 00000000..ff9a9b18 --- /dev/null +++ b/day18_1.cpp @@ -0,0 +1,52 @@ +#include +#include "pystrlib.hpp" + +long long reg[26]; + +struct Operand { + Operand() {} + Operand(const std::string &s) { std::isalpha(s[0]) ? (type = 1, value = s[0] - 'a') : (type = 0, value = std::stoi(s)); } + int type, value; + operator long long () const { return type ? reg[value] : value; } + long long operator=(long long v) { return type ? (reg[value] = v) : value; } +} op1[100], op2[100]; + +int code[100], n, pt, ls; + +#define SND 1 +#define SET 2 +#define ADD 3 +#define MUL 4 +#define MOD 5 +#define RCV 6 +#define JGZ 7 + +int main() { + freopen("day18.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, " "); + if (vec[0] == "snd") code[n] = SND, op1[n] = Operand(vec[1]); + else if (vec[0] == "set") code[n] = SET, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "add") code[n] = ADD, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "mul") code[n] = MUL, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "mod") code[n] = MOD, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "rcv") code[n] = RCV, op1[n] = Operand(vec[1]); + else if (vec[0] == "jgz") code[n] = JGZ, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + ++n; + } + } + while (pt < n) { + switch (code[pt]) { + case SND: ls = op1[pt]; ++pt; break; + case SET: op1[pt] = (long long) op2[pt]; ++pt; break; + case ADD: op1[pt] = op1[pt] + op2[pt]; ++pt; break; + case MUL: op1[pt] = op1[pt] * op2[pt]; ++pt; break; + case MOD: op1[pt] = op1[pt] % op2[pt]; ++pt; break; + case RCV: if (op1[pt]) { std::cout << ls; return 0; } ++pt; break; + case JGZ: if (op1[pt] > 0) pt += op2[pt]; else ++pt; break; + } + } + return 0; +} \ No newline at end of file diff --git a/day18_2.cpp b/day18_2.cpp new file mode 100644 index 00000000..2226c1cc --- /dev/null +++ b/day18_2.cpp @@ -0,0 +1,70 @@ +#include +#include "pystrlib.hpp" +#include + +struct Operand { + Operand() {} + Operand(const std::string &s) { std::isalpha(s[0]) ? (type = 1, value = s[0] - 'a') : (type = 0, value = std::stoi(s)); } + int type, value; +} op1[100], op2[100]; + +int code[100], n; + +#define SND 1 +#define SET 2 +#define ADD 3 +#define MUL 4 +#define MOD 5 +#define RCV 6 +#define JGZ 7 + +#define _INT_TYPE long long + +struct program { + _INT_TYPE reg[26]; + _INT_TYPE value(const Operand &op) const { return op.type ? reg[op.value] : op.value; } + _INT_TYPE & ref(const Operand &op) { return reg[op.value]; } + int pt, ts; + program *p; + std::queue<_INT_TYPE> Q; + bool run_until_block() { + while (0 <= pt && pt < n) { + switch (code[pt]) { + case SND: p->Q.push(value(op1[pt])); ++ts; ++pt; break; + case SET: ref(op1[pt]) = value(op2[pt]); ++pt; break; + case ADD: ref(op1[pt]) += value(op2[pt]); ++pt; break; + case MUL: ref(op1[pt]) *= value(op2[pt]); ++pt; break; + case MOD: ref(op1[pt]) %= value(op2[pt]); ++pt; break; + case RCV: if (Q.empty()) return false; ref(op1[pt]) = Q.front(); Q.pop(); ++pt; break; + case JGZ: if (value(op1[pt]) > 0) pt += value(op2[pt]); else ++pt; break; + } + } + return true; + } + bool wait() { return pt < 0 || pt >= n || (code[pt] == RCV && Q.empty()); } +} p[2]; + +int main() { + freopen("day18.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, " "); + if (vec[0] == "snd") code[n] = SND, op1[n] = Operand(vec[1]); + else if (vec[0] == "set") code[n] = SET, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "add") code[n] = ADD, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "mul") code[n] = MUL, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "mod") code[n] = MOD, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "rcv") code[n] = RCV, op1[n] = Operand(vec[1]); + else if (vec[0] == "jgz") code[n] = JGZ, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + ++n; + } + } + p[0].reg[15] = 0; p[1].reg[15] = 1; + p[0].p = p + 1; p[1].p = p; + while (1) { + if (p[0].run_until_block()) { std::cout << p[1].ts << std::endl; return 0; } + p[1].run_until_block(); + if (p[0].wait() && p[1].wait()) { std::cout << p[1].ts << std::endl; return 0; } + } +} \ No newline at end of file diff --git a/day19_1.cpp b/day19_1.cpp new file mode 100644 index 00000000..fc2bec6f --- /dev/null +++ b/day19_1.cpp @@ -0,0 +1,33 @@ +#include +#include + +char map[300][300], ch[30]; +int n, m, x, y, dir, c; + +constexpr int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 }; +// RDLU + +inline bool in_range(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; } + +int main() { + freopen("day19.txt", "r", stdin); + while (std::cin.getline(map[n], 299)) ++n; + m = std::strlen(map[0]); + for (int i = 0; i < m; ++i) + if (map[0][i] != ' ') { dir = 1; x = 0; y = i; break; } + while (1) { + x += dx[dir]; y += dy[dir]; + if (!in_range(x, y) || map[x][y] == ' ') break; + if (std::isalpha(map[x][y])) ch[c++] = map[x][y]; + if (map[x][y] == '+') { + if (dir & 1) + if (in_range(x + dx[0], y + dy[0]) && map[x + dx[0]][y + dy[0]] != ' ') dir = 0; + else dir = 2; + else + if (in_range(x + dx[1], y + dy[1]) && map[x + dx[1]][y + dy[1]] != ' ') dir = 1; + else dir = 3; + } + } + std::cout << ch << std::endl; + return 0; +} \ No newline at end of file diff --git a/day19_2.cpp b/day19_2.cpp new file mode 100644 index 00000000..49b898d3 --- /dev/null +++ b/day19_2.cpp @@ -0,0 +1,32 @@ +#include +#include + +char map[300][300]; +int n, m, x, y, dir, c; + +constexpr int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 }; +// RDLU + +inline bool in_range(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; } + +int main() { + freopen("day19.txt", "r", stdin); + while (std::cin.getline(map[n], 299)) ++n; + m = std::strlen(map[0]); + for (int i = 0; i < m; ++i) + if (map[0][i] != ' ') { dir = 1; x = 0; y = i; break; } + while (1) { + x += dx[dir]; y += dy[dir]; ++c; + if (!in_range(x, y) || map[x][y] == ' ') break; + if (map[x][y] == '+') { + if (dir & 1) + if (in_range(x + dx[0], y + dy[0]) && map[x + dx[0]][y + dy[0]] != ' ') dir = 0; + else dir = 2; + else + if (in_range(x + dx[1], y + dy[1]) && map[x + dx[1]][y + dy[1]] != ' ') dir = 1; + else dir = 3; + } + } + std::cout << c << std::endl; + return 0; +} \ No newline at end of file diff --git a/day1_1.cpp b/day1_1.cpp new file mode 100644 index 00000000..e2cb5d6a --- /dev/null +++ b/day1_1.cpp @@ -0,0 +1,15 @@ +#include +#include + +char str[5000]; +int n, ans; + +int main() { + std::cin >> str; + n = std::strlen(str); + str[n] = str[0]; + for (int i = 0; i < n; ++i) + if (str[i] == str[i + 1]) ans += str[i] - '0'; + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day1_2.cpp b/day1_2.cpp new file mode 100644 index 00000000..f8bbc9fa --- /dev/null +++ b/day1_2.cpp @@ -0,0 +1,16 @@ +#include +#include + +char str[5000]; +int n, ans; + +int main() { + std::cin >> str; + n = std::strlen(str); + for (int i = 0; i < n; ++i) { + str[i + n] = str[i]; + if (str[i] == str[i + (n >> 1)]) ans += str[i] - '0'; + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day20_1.cpp b/day20_1.cpp new file mode 100644 index 00000000..0f07e34a --- /dev/null +++ b/day20_1.cpp @@ -0,0 +1,44 @@ +#include +#include "pystrlib.hpp" + +struct QuadraticFunc { + int a, b, c; + QuadraticFunc operator+(const QuadraticFunc &rhs) const { return (QuadraticFunc) { a + rhs.a, b + rhs.b, c + rhs.c }; } + QuadraticFunc operator*(int rhs) const { return (QuadraticFunc) { a * rhs, b * rhs, c * rhs }; } + bool operator<(const QuadraticFunc &rhs) const { return (a == rhs.a) ? ((b == rhs.b) ? (c < rhs.c) : (b < rhs.b)) : (a < rhs.a); } +}; + +struct Component { + int a, v, x; + QuadraticFunc get_dist_func() const { return ((QuadraticFunc) { a, 2 * v + a, 2 * x }) * ((a > 0 || (a == 0 && v >= 0)) ? 1 : -1); } +}; + +struct Particle { + Component x, y, z; + QuadraticFunc get_dist_func() const { return x.get_dist_func() + y.get_dist_func() + z.get_dist_func(); } +}; + +std::vector particles; +std::vector pos; + +int main() { + freopen("day20.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, ", "); + std::vector vp = lib::split(vec[0].substr(3, vec[0].size() - 4), ","), + vv = lib::split(vec[1].substr(3, vec[1].size() - 4), ","), + va = lib::split(vec[2].substr(3, vec[2].size() - 4), ","); + particles.push_back((Particle) { + (Component) { std::stoi(va[0]), std::stoi(vv[0]), std::stoi(vp[0]) }, + (Component) { std::stoi(va[1]), std::stoi(vv[1]), std::stoi(vp[1]) }, + (Component) { std::stoi(va[2]), std::stoi(vv[2]), std::stoi(vp[2]) } + }); + } + } + for (unsigned i = 0; i < particles.size(); ++i) pos.push_back(i); + std::sort(pos.begin(), pos.end(), [](int p1, int p2) { return particles[p1].get_dist_func() < particles[p2].get_dist_func(); }); + std::cout << pos[0] << std::endl; + return 0; +} \ No newline at end of file diff --git a/day20_2.cpp b/day20_2.cpp new file mode 100644 index 00000000..27544b97 --- /dev/null +++ b/day20_2.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include "pystrlib.hpp" + +struct QuadraticFunc { + int a, b, c; + QuadraticFunc operator-(const QuadraticFunc &rhs) const { return (QuadraticFunc) { a - rhs.a, b - rhs.b, c - rhs.c }; } + std::pair< bool, std::vector > get_root() { + if (a != 0) { + int delta = b * b - 4 * a * c, sq = std::sqrt(delta); + if (sq * sq != delta) return std::make_pair(false, std::vector()); + std::vector ans; + int v = -b + sq; + if (v % (2 * a) == 0) ans.push_back(v / (2 * a)); + v = -b - sq; + if (v % (2 * a) == 0) ans.push_back(v / (2 * a)); + return std::make_pair(false, ans); + } else if (b != 0) { + if (c % b == 0) return std::make_pair(false, std::vector{ -c / b }); + return std::make_pair(false, std::vector()); + } else return std::make_pair(c == 0, std::vector()); + } +}; + +struct Component { + int a, v, x; + QuadraticFunc get_pos_func() const { return ((QuadraticFunc) { a, 2 * v + a, 2 * x }); } +}; + +struct Particle { + Component x, y, z; +}; + +std::vector particles; +int n; + +std::map< int, std::vector< std::pair > > M; +std::vector collided; + +inline bool contains(const std::pair< bool, std::vector > &vec, int v) { + if (vec.first) return true; + for (int i : vec.second) if (v == i) return true; + return false; +} + +int main() { + freopen("day20.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, ", "); + std::vector vp = lib::split(vec[0].substr(3, vec[0].size() - 4), ","), + vv = lib::split(vec[1].substr(3, vec[1].size() - 4), ","), + va = lib::split(vec[2].substr(3, vec[2].size() - 4), ","); + particles.push_back((Particle) { + (Component) { std::stoi(va[0]), std::stoi(vv[0]), std::stoi(vp[0]) }, + (Component) { std::stoi(va[1]), std::stoi(vv[1]), std::stoi(vp[1]) }, + (Component) { std::stoi(va[2]), std::stoi(vv[2]), std::stoi(vp[2]) } + }); + } + } + n = particles.size(); + collided.resize(n, 0); + for (int i = 0; i < n; ++i) + for (int j = i + 1; j < n; ++j) { + auto xt = (particles[i].x.get_pos_func() - particles[j].x.get_pos_func()).get_root(), + yt = (particles[i].y.get_pos_func() - particles[j].y.get_pos_func()).get_root(), + zt = (particles[i].z.get_pos_func() - particles[j].z.get_pos_func()).get_root(); + bool found = false; int collide; + if (!yt.first) std::swap(xt, yt); + else if (!zt.first) std::swap(xt, zt); + if (xt.first) found = true, collide = 0; + else + for (int v : xt.second) + if (v >= 0 && contains(yt, v) && contains(zt, v) && (!found || (found && v < collide))) + found = true, collide = v; + if (found) { + if (!M.count(collide)) M[collide] = std::vector< std::pair >(); + M[collide].emplace_back(i, j); + } + } + for (const auto &[x, y] : M) { + for (auto [i, j] : y) + if (collided[i] != 2 && collided[j] != 2) + collided[i] = collided[j] = 1; + std::replace(collided.begin(), collided.end(), 1, 2); + } + std::cout << std::count(collided.begin(), collided.end(), 0) << std::endl; + return 0; +} \ No newline at end of file diff --git a/day21_1.cpp b/day21_1.cpp new file mode 100644 index 00000000..a08fb3e8 --- /dev/null +++ b/day21_1.cpp @@ -0,0 +1,138 @@ +#include +#include +#include "pystrlib.hpp" + +struct Pattern { + int siz; + char ch[6][6]; + Pattern(int _siz) : siz(_siz) { std::memset(ch, 0, sizeof(ch)); } + Pattern rotate() const { + Pattern ans(siz); + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + ans.ch[j][siz - 1 - i] = ch[i][j]; + return ans; + } + Pattern flip() const { + Pattern ans(siz); + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + ans.ch[i][siz - 1 - j] = ch[i][j]; + return ans; + } + bool operator==(const Pattern &rhs) const { + if (siz != rhs.siz) return false; + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + if (ch[i][j] != rhs.ch[i][j]) return false; + return true; + } + bool match(const Pattern &match) const { + Pattern tmp = *this; + for (int i = 0; i < 4; ++i) { + if (tmp == match) return true; + tmp = tmp.rotate(); + } + tmp = tmp.flip(); + for (int i = 0; i < 4; ++i) { + if (tmp == match) return true; + tmp = tmp.rotate(); + } + return false; + } + Pattern sub_pattern(int x, int y, int siz) const { + Pattern ans(siz); + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + ans.ch[i][j] = ch[x + i][y + j]; + return ans; + } + void copy_into(const Pattern &pattern, int x, int y) { + for (int i = 0; i < pattern.siz; ++i) + for (int j = 0; j < pattern.siz; ++j) + ch[x + i][y + j] = pattern.ch[i][j]; + } + int lit() const { + int ans = 0; + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + ans += ch[i][j] == '#'; + return ans; + } + friend std::ostream& operator<<(std::ostream &cout, const Pattern &pattern); +}; + +std::vector patterns; +int go[20000][9], lit[20000], cnt[20000], gcnt[20000]; + +Pattern read_pattern(const std::string &str) { + std::vector v = lib::split(str, "/"); + Pattern pattern(v.size()); + for (int i = 0; i < pattern.siz; ++i) + for (int j = 0; j < pattern.siz; ++j) + pattern.ch[i][j] = v[i][j]; + return pattern; +} + +int get_order(const Pattern &pattern) { + for (unsigned i = 0; i < patterns.size(); ++i) + if (pattern.match(patterns[i])) return i; + patterns.push_back(pattern); return patterns.size() - 1; +} + +std::ostream& operator<<(std::ostream& cout, const Pattern &pattern) { + for (int i = 0; i < pattern.siz; ++i) + cout << pattern.ch[i] << std::endl; + return cout; +} + +inline void fill_into(char ch[6][6], int x, int y, char ch2[4][4]) { + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + ch[x + i][y + j] = ch2[i][j]; +} + +int main() { + freopen("day21.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + auto [f, _, t] = lib::partition(line, " => "); + Pattern fp = read_pattern(f), tp = read_pattern(t); + go[get_order(fp)][0] = get_order(tp); + } + } + for (unsigned i = 0; i < patterns.size(); ++i) + if (patterns[i].siz == 4) { + Pattern pattern(6); + pattern.copy_into(patterns[go[get_order(patterns[i].sub_pattern(0, 0, 2))][0]], 0, 0); + pattern.copy_into(patterns[go[get_order(patterns[i].sub_pattern(0, 2, 2))][0]], 0, 3); + pattern.copy_into(patterns[go[get_order(patterns[i].sub_pattern(2, 0, 2))][0]], 3, 0); + pattern.copy_into(patterns[go[get_order(patterns[i].sub_pattern(2, 2, 2))][0]], 3, 3); + go[i][0] = get_order(pattern.sub_pattern(0, 0, 2)); + go[i][1] = get_order(pattern.sub_pattern(0, 2, 2)); + go[i][2] = get_order(pattern.sub_pattern(0, 4, 2)); + go[i][3] = get_order(pattern.sub_pattern(2, 0, 2)); + go[i][4] = get_order(pattern.sub_pattern(2, 2, 2)); + go[i][5] = get_order(pattern.sub_pattern(2, 4, 2)); + go[i][6] = get_order(pattern.sub_pattern(4, 0, 2)); + go[i][7] = get_order(pattern.sub_pattern(4, 2, 2)); + go[i][8] = get_order(pattern.sub_pattern(4, 4, 2)); + } + for (unsigned i = 0; i < patterns.size(); ++i) lit[i] = patterns[i].lit(); + cnt[get_order(read_pattern(".#./..#/###"))] = 1; + for (int i = 0; i < 5; ++i) { + std::memset(gcnt, 0, sizeof(gcnt)); + for (unsigned j = 0; j < patterns.size(); ++j) + if (patterns[j].siz < 4) gcnt[go[j][0]] += cnt[j]; + else + for (int k = 0; k < 9; ++k) + gcnt[go[j][k]] += cnt[j]; + std::memcpy(cnt, gcnt, sizeof(cnt)); + } + int ans = 0; + for (unsigned i = 0; i < patterns.size(); ++i) + ans += lit[i] * cnt[i]; + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day21_2.cpp b/day21_2.cpp new file mode 100644 index 00000000..6046964f --- /dev/null +++ b/day21_2.cpp @@ -0,0 +1,138 @@ +#include +#include +#include "pystrlib.hpp" + +struct Pattern { + int siz; + char ch[6][6]; + Pattern(int _siz) : siz(_siz) { std::memset(ch, 0, sizeof(ch)); } + Pattern rotate() const { + Pattern ans(siz); + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + ans.ch[j][siz - 1 - i] = ch[i][j]; + return ans; + } + Pattern flip() const { + Pattern ans(siz); + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + ans.ch[i][siz - 1 - j] = ch[i][j]; + return ans; + } + bool operator==(const Pattern &rhs) const { + if (siz != rhs.siz) return false; + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + if (ch[i][j] != rhs.ch[i][j]) return false; + return true; + } + bool match(const Pattern &match) const { + Pattern tmp = *this; + for (int i = 0; i < 4; ++i) { + if (tmp == match) return true; + tmp = tmp.rotate(); + } + tmp = tmp.flip(); + for (int i = 0; i < 4; ++i) { + if (tmp == match) return true; + tmp = tmp.rotate(); + } + return false; + } + Pattern sub_pattern(int x, int y, int siz) const { + Pattern ans(siz); + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + ans.ch[i][j] = ch[x + i][y + j]; + return ans; + } + void copy_into(const Pattern &pattern, int x, int y) { + for (int i = 0; i < pattern.siz; ++i) + for (int j = 0; j < pattern.siz; ++j) + ch[x + i][y + j] = pattern.ch[i][j]; + } + int lit() const { + int ans = 0; + for (int i = 0; i < siz; ++i) + for (int j = 0; j < siz; ++j) + ans += ch[i][j] == '#'; + return ans; + } + friend std::ostream& operator<<(std::ostream &cout, const Pattern &pattern); +}; + +std::vector patterns; +int go[20000][9], lit[20000], cnt[20000], gcnt[20000]; + +Pattern read_pattern(const std::string &str) { + std::vector v = lib::split(str, "/"); + Pattern pattern(v.size()); + for (int i = 0; i < pattern.siz; ++i) + for (int j = 0; j < pattern.siz; ++j) + pattern.ch[i][j] = v[i][j]; + return pattern; +} + +int get_order(const Pattern &pattern) { + for (unsigned i = 0; i < patterns.size(); ++i) + if (pattern.match(patterns[i])) return i; + patterns.push_back(pattern); return patterns.size() - 1; +} + +std::ostream& operator<<(std::ostream& cout, const Pattern &pattern) { + for (int i = 0; i < pattern.siz; ++i) + cout << pattern.ch[i] << std::endl; + return cout; +} + +inline void fill_into(char ch[6][6], int x, int y, char ch2[4][4]) { + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + ch[x + i][y + j] = ch2[i][j]; +} + +int main() { + freopen("day21.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + auto [f, _, t] = lib::partition(line, " => "); + Pattern fp = read_pattern(f), tp = read_pattern(t); + go[get_order(fp)][0] = get_order(tp); + } + } + for (unsigned i = 0; i < patterns.size(); ++i) + if (patterns[i].siz == 4) { + Pattern pattern(6); + pattern.copy_into(patterns[go[get_order(patterns[i].sub_pattern(0, 0, 2))][0]], 0, 0); + pattern.copy_into(patterns[go[get_order(patterns[i].sub_pattern(0, 2, 2))][0]], 0, 3); + pattern.copy_into(patterns[go[get_order(patterns[i].sub_pattern(2, 0, 2))][0]], 3, 0); + pattern.copy_into(patterns[go[get_order(patterns[i].sub_pattern(2, 2, 2))][0]], 3, 3); + go[i][0] = get_order(pattern.sub_pattern(0, 0, 2)); + go[i][1] = get_order(pattern.sub_pattern(0, 2, 2)); + go[i][2] = get_order(pattern.sub_pattern(0, 4, 2)); + go[i][3] = get_order(pattern.sub_pattern(2, 0, 2)); + go[i][4] = get_order(pattern.sub_pattern(2, 2, 2)); + go[i][5] = get_order(pattern.sub_pattern(2, 4, 2)); + go[i][6] = get_order(pattern.sub_pattern(4, 0, 2)); + go[i][7] = get_order(pattern.sub_pattern(4, 2, 2)); + go[i][8] = get_order(pattern.sub_pattern(4, 4, 2)); + } + for (unsigned i = 0; i < patterns.size(); ++i) lit[i] = patterns[i].lit(); + cnt[get_order(read_pattern(".#./..#/###"))] = 1; + for (int i = 0; i < 18; ++i) { + std::memset(gcnt, 0, sizeof(gcnt)); + for (unsigned j = 0; j < patterns.size(); ++j) + if (patterns[j].siz < 4) gcnt[go[j][0]] += cnt[j]; + else + for (int k = 0; k < 9; ++k) + gcnt[go[j][k]] += cnt[j]; + std::memcpy(cnt, gcnt, sizeof(cnt)); + } + int ans = 0; + for (unsigned i = 0; i < patterns.size(); ++i) + ans += lit[i] * cnt[i]; + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day22_1.cpp b/day22_1.cpp new file mode 100644 index 00000000..ee64680d --- /dev/null +++ b/day22_1.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +std::set< std::pair > S; + +constexpr int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 }; + +int dir, x, y, n, m; + +char map[100][100]; + +int main() { + freopen("day22.txt", "r", stdin); + while (std::cin >> map[n]) ++n; + m = std::strlen(map[0]); + for (int i = 0; i < n; ++i) + for (int j = 0; j < m; ++j) + if (map[i][j] == '#') S.emplace(i, j); + x = n >> 1; y = m >> 1; dir = 0; + int ans = 0; + for (int i = 0; i < 10000; ++i) { + auto infected = S.find(std::make_pair(x, y)); + if (infected != S.end()) dir = (dir + 1) % 4, S.erase(infected); + else dir = (dir + 3) % 4, S.emplace(x, y), ++ans; + x += dx[dir], y += dy[dir]; + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day22_2.cpp b/day22_2.cpp new file mode 100644 index 00000000..5ac07a91 --- /dev/null +++ b/day22_2.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +std::map< std::pair, int > S; + +constexpr int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 }, dl[4] = { 3, 0, 1, 2 }; + +int dir, x, y, n, m; + +char map[100][100]; + +int main() { + freopen("day22.txt", "r", stdin); + while (std::cin >> map[n]) ++n; + m = std::strlen(map[0]); + for (int i = 0; i < n; ++i) + for (int j = 0; j < m; ++j) + if (map[i][j] == '#') S[std::make_pair(i, j)] = 2; + else S[std::make_pair(i, j)] = 0; + x = n >> 1; y = m >> 1; dir = 0; + int ans = 0; + for (int i = 0; i < 10000000; ++i) { + auto infected = S.find(std::make_pair(x, y)); + int state = infected == S.end() ? 0 : infected->second; + S[std::make_pair(x, y)] = (state + 1) % 4; + dir = (dir + dl[state]) % 4; x += dx[dir]; y += dy[dir]; + if (state == 1) ++ans; + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day23_1.cpp b/day23_1.cpp new file mode 100644 index 00000000..8b817b2b --- /dev/null +++ b/day23_1.cpp @@ -0,0 +1,45 @@ +#include +#include "pystrlib.hpp" + +long long reg[8]; + +struct Operand { + Operand() {} + Operand(const std::string &s) { std::isalpha(s[0]) ? (type = 1, value = s[0] - 'a') : (type = 0, value = std::stoi(s)); } + int type, value; + operator long long () const { return type ? reg[value] : value; } + long long operator=(long long v) { return type ? (reg[value] = v) : value; } +} op1[100], op2[100]; + +int code[100], n, pt, ls; + +#define SET 1 +#define SUB 2 +#define MUL 3 +#define JNZ 4 + +int main() { + freopen("day23.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, " "); + if (vec[0] == "set") code[n] = SET, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "sub") code[n] = SUB, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "mul") code[n] = MUL, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + else if (vec[0] == "jnz") code[n] = JNZ, op1[n] = Operand(vec[1]), op2[n] = Operand(vec[2]); + ++n; + } + } + int ans = 0; + while (pt < n) { + switch (code[pt]) { + case SET: op1[pt] = (long long) op2[pt]; ++pt; break; + case SUB: op1[pt] = op1[pt] - op2[pt]; ++pt; break; + case MUL: op1[pt] = op1[pt] * op2[pt]; ++ans; ++pt; break; + case JNZ: if (op1[pt]) pt += op2[pt]; else ++pt; break; + } + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day23_2.cpp b/day23_2.cpp new file mode 100644 index 00000000..d60a5350 --- /dev/null +++ b/day23_2.cpp @@ -0,0 +1,6 @@ +b = 81 + +h = 0 +b = 81 * 100 + 100000 +c = b + 17000 +For[i = b, i <= c, i += 17, If[!PrimeQ[i], h += 1]] diff --git a/day24_1.cpp b/day24_1.cpp new file mode 100644 index 00000000..7f51a1a0 --- /dev/null +++ b/day24_1.cpp @@ -0,0 +1,26 @@ +#include +#include "pystrlib.hpp" + +int p1[100], p2[100], n, u[100], ans; + +void dfs(int s, int v) { + if (s > ans) ans = s; + for (int i = 0; i < n; ++i) { + if (p1[i] == v && !u[i]) u[i] = 1, dfs(s + p1[i] + p2[i], p2[i]), u[i] = 0; + if (p2[i] == v && !u[i]) u[i] = 1, dfs(s + p1[i] + p2[i], p1[i]), u[i] = 0; + } +} + +int main() { + freopen("day24.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + auto [f, _, t] = lib::partition(line, "/"); + p1[n] = std::stoi(f); p2[n++] = std::stoi(t); + } + } + dfs(0, 0); + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day24_2.cpp b/day24_2.cpp new file mode 100644 index 00000000..5b6e586f --- /dev/null +++ b/day24_2.cpp @@ -0,0 +1,26 @@ +#include +#include "pystrlib.hpp" + +int p1[100], p2[100], n, u[100], ans, len; + +void dfs(int s, int v, int c) { + if ((c == len && s > ans) || c > len) ans = s, len = c; + for (int i = 0; i < n; ++i) { + if (p1[i] == v && !u[i]) u[i] = 1, dfs(s + p1[i] + p2[i], p2[i], c + 1), u[i] = 0; + if (p2[i] == v && !u[i]) u[i] = 1, dfs(s + p1[i] + p2[i], p1[i], c + 1), u[i] = 0; + } +} + +int main() { + freopen("day24.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + auto [f, _, t] = lib::partition(line, "/"); + p1[n] = std::stoi(f); p2[n++] = std::stoi(t); + } + } + dfs(0, 0, 0); + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day25_1.cpp b/day25_1.cpp new file mode 100644 index 00000000..a4a92648 --- /dev/null +++ b/day25_1.cpp @@ -0,0 +1,41 @@ +#include +#include "pystrlib.hpp" +#include + +int s, v_t[10], v_f[10], s_t[10], s_f[10], p_t[10], p_f[10], p, st; +std::unordered_set tape; + +inline void set(int p, int v) { if (v) tape.insert(p); else tape.erase(p); } + +int main() { + freopen("day25.txt", "r", stdin); + { + std::string line; + std::getline(std::cin, line); + p = 0; s = line[line.size() - 2] - 'A'; + std::getline(std::cin, line); + std::vector sp = lib::split(line, " "); + st = std::stoi(sp[5]); + std::getline(std::cin, line); + + int i = 0; + while (std::getline(std::cin, line)) { + std::getline(std::cin, line); + std::getline(std::cin, line); v_f[i] = line[line.size() - 2] & 1; + std::getline(std::cin, line); p_f[i] = line[line.size() - 3] == 'f' ? -1 : 1; + std::getline(std::cin, line); s_f[i] = line[line.size() - 2] - 'A'; + std::getline(std::cin, line); + std::getline(std::cin, line); v_t[i] = line[line.size() - 2] & 1; + std::getline(std::cin, line); p_t[i] = line[line.size() - 3] == 'f' ? -1 : 1; + std::getline(std::cin, line); s_t[i] = line[line.size() - 2] - 'A'; + std::getline(std::cin, line); + ++i; + } + } + for (int i = 0; i < st; ++i) { + if (tape.count(p)) set(p, v_t[s]), p += p_t[s], s = s_t[s]; + else set(p, v_f[s]), p += p_f[s], s = s_f[s]; + } + std::cout << tape.size() << std::endl; + return 0; +} \ No newline at end of file diff --git a/day2_1.cpp b/day2_1.cpp new file mode 100644 index 00000000..429ace4c --- /dev/null +++ b/day2_1.cpp @@ -0,0 +1,20 @@ +#include +#include "pystrlib.hpp" + +int ans; + +int main() { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, "\t"); + int maxv = std::stoi(vec[0]), minv = maxv; + for (const std::string &str : vec) { + int v = std::stoi(str); + if (v < minv) minv = v; + if (v > maxv) maxv = v; + } + ans += maxv - minv; + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day2_2.cpp b/day2_2.cpp new file mode 100644 index 00000000..fbeedaf4 --- /dev/null +++ b/day2_2.cpp @@ -0,0 +1,18 @@ +#include +#include "pystrlib.hpp" + +int ans; + +int main() { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, "\t"); + std::vector a; + for (const std::string &str : vec) a.push_back(std::stoi(str)); + for (unsigned i = 0; i < a.size(); ++i) + for (unsigned j = 0; j < a.size(); ++j) + if (i != j && a[i] % a[j] == 0) ans += a[i] / a[j]; + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day3_1.cpp b/day3_1.cpp new file mode 100644 index 00000000..fdaf8913 --- /dev/null +++ b/day3_1.cpp @@ -0,0 +1,16 @@ +#include +#include + +int n, x, y; + +int main() { + std::cin >> n; + int s = ((int) std::ceil(std::sqrt(n))) | 1; + int os = s & -2, v = s * s - n, hs = os >> 1; + if (v < os) x = hs - v, y = -hs; + else if (v < 2 * os) x = -hs, y = -hs + (v - os); + else if (v < 3 * os) x = -hs + (v - 2 * os), y = hs; + else if (v < 4 * os) x = hs, y = hs - (v - 3 * os); + std::cout << std::abs(x) + std::abs(y) << std::endl; + return 0; +} \ No newline at end of file diff --git a/day3_2.cpp b/day3_2.cpp new file mode 100644 index 00000000..a9fce898 --- /dev/null +++ b/day3_2.cpp @@ -0,0 +1,29 @@ +#include + +int a[505][505], x = 250, y = 250, n; + +inline int sum(int x, int y) { return a[x - 1][y - 1] + a[x - 1][y] + a[x - 1][y + 1] + a[x][y - 1] + a[x][y] + a[x][y + 1] + a[x + 1][y - 1] + a[x + 1][y] + a[x + 1][y + 1]; } + +int main() { + std::cin >> n; + a[x][y] = 1; + for (int len = 2; ; len += 2) { + ++x; a[x][y] = sum(x, y); + for (int i = 1; i < len; ++i) { + ++y; a[x][y] = sum(x, y); + if (a[x][y] > n) { std::cout << a[x][y] << std::endl; return 0; } + } + for (int i = 0; i < len; ++i) { + --x; a[x][y] = sum(x, y); + if (a[x][y] > n) { std::cout << a[x][y] << std::endl; return 0; } + } + for (int i = 0; i < len; ++i) { + --y; a[x][y] = sum(x, y); + if (a[x][y] > n) { std::cout << a[x][y] << std::endl; return 0; } + } + for (int i = 0; i < len; ++i) { + ++x; a[x][y] = sum(x, y); + if (a[x][y] > n) { std::cout << a[x][y] << std::endl; return 0; } + } + } +} \ No newline at end of file diff --git a/day4_1.cpp b/day4_1.cpp new file mode 100644 index 00000000..c65c4372 --- /dev/null +++ b/day4_1.cpp @@ -0,0 +1,18 @@ +#include +#include "pystrlib.hpp" + +int ans; + +int main() { + freopen("day4.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, " "); + std::sort(vec.begin(), vec.end()); + ans += std::unique(vec.begin(), vec.end()) == vec.end(); + } + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day4_2.cpp b/day4_2.cpp new file mode 100644 index 00000000..5ad95860 --- /dev/null +++ b/day4_2.cpp @@ -0,0 +1,19 @@ +#include +#include "pystrlib.hpp" + +int ans; + +int main() { + freopen("day4.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, " "); + for (std::string &str : vec) std::sort(str.begin(), str.end()); + std::sort(vec.begin(), vec.end()); + ans += std::unique(vec.begin(), vec.end()) == vec.end(); + } + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day5_1.cpp b/day5_1.cpp new file mode 100644 index 00000000..cb04d9e5 --- /dev/null +++ b/day5_1.cpp @@ -0,0 +1,11 @@ +#include + +int code[2000], n, pt, c; + +int main() { + freopen("day5.txt", "r", stdin); + while (std::cin >> code[n]) ++n; + while (pt < n) pt += code[pt]++, ++c; + std::cout << c << std::endl; + return 0; +} \ No newline at end of file diff --git a/day5_2.cpp b/day5_2.cpp new file mode 100644 index 00000000..a2ec5e11 --- /dev/null +++ b/day5_2.cpp @@ -0,0 +1,11 @@ +#include + +int code[2000], n, pt, c; + +int main() { + freopen("day5.txt", "r", stdin); + while (std::cin >> code[n]) ++n; + while (pt < n) pt += (code[pt] >= 3 ? code[pt]-- : code[pt]++), ++c; + std::cout << c << std::endl; + return 0; +} \ No newline at end of file diff --git a/day6_1.cpp b/day6_1.cpp new file mode 100644 index 00000000..588bbe12 --- /dev/null +++ b/day6_1.cpp @@ -0,0 +1,31 @@ +#include +#include "pystrlib.hpp" +#include + +std::vector mem; +int n; +std::set< std::vector > S; + +inline int get(int i) { return i >= n ? i - n : i; } + +int main() { + freopen("day6.txt", "r", stdin); + { + std::string line; std::getline(std::cin, line); + std::vector vec = lib::split(line, "\t"); + for (const std::string &str : vec) mem.push_back(std::stoi(str)); + } + n = mem.size(); S.insert(mem); + while (1) { + int mi = 0; + for (int i = 1; i < n; ++i) + if (mem[i] > mem[mi]) mi = i; + int ed = mem[mi] / n, op = mem[mi] % n; mem[mi] = 0; + for (int i = 1; i <= op; ++i) mem[get(mi + i)] += ed + 1; + for (int i = op + 1; i <= n; ++i) mem[get(mi + i)] += ed; + if (S.count(mem)) break; + S.insert(mem); + } + std::cout << S.size() << std::endl; + return 0; +} \ No newline at end of file diff --git a/day6_2.cpp b/day6_2.cpp new file mode 100644 index 00000000..bcfa7ea6 --- /dev/null +++ b/day6_2.cpp @@ -0,0 +1,32 @@ +#include +#include "pystrlib.hpp" +#include + +std::vector mem; +int n; +std::map< std::vector, int > S; + +inline int get(int i) { return i >= n ? i - n : i; } + +int main() { + freopen("day6.txt", "r", stdin); + { + std::string line; std::getline(std::cin, line); + std::vector vec = lib::split(line, "\t"); + for (const std::string &str : vec) mem.push_back(std::stoi(str)); + } + n = mem.size(); S[mem] = 0; int cyc = 0; + while (1) { + ++cyc; + int mi = 0; + for (int i = 1; i < n; ++i) + if (mem[i] > mem[mi]) mi = i; + int ed = mem[mi] / n, op = mem[mi] % n; mem[mi] = 0; + for (int i = 1; i <= op; ++i) mem[get(mi + i)] += ed + 1; + for (int i = op + 1; i <= n; ++i) mem[get(mi + i)] += ed; + if (S.count(mem)) break; + S[mem] = cyc; + } + std::cout << cyc - S[mem] << std::endl; + return 0; +} \ No newline at end of file diff --git a/day7_1.cpp b/day7_1.cpp new file mode 100644 index 00000000..373ac28d --- /dev/null +++ b/day7_1.cpp @@ -0,0 +1,37 @@ +#include +#include "pystrlib.hpp" +#include + +struct edge { int to, nxt; } G[2005]; +int cnt, head[2005], wei[2005], indgr[2005]; + +std::unordered_map mapping; +std::string back[2005]; + +int cm; + +inline int get_order(const std::string &str) { + if (mapping.count(str)) return mapping[str]; + back[++cm] = str; return mapping[str] = cm; +} +inline void add_edge(int f, int t) { G[++cnt] = (edge) { t, head[f] }; head[f] = cnt; ++indgr[t]; } + +int main() { + freopen("day7.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + auto [f, _, t] = lib::partition(line, " -> "); + auto [n, __, w] = lib::partition(f, " ("); + int i = get_order(n); + wei[i] = std::stoi(w); + if (t != "") { + std::vector to = lib::split(t, ", "); + for (const std::string &s : to) add_edge(i, get_order(s)); + } + } + } + for (int i = 1; i <= cm; ++i) + if (!indgr[i]) std::cout << back[i] << std::endl; + return 0; +} \ No newline at end of file diff --git a/day7_2.cpp b/day7_2.cpp new file mode 100644 index 00000000..046597fa --- /dev/null +++ b/day7_2.cpp @@ -0,0 +1,52 @@ +#include +#include "pystrlib.hpp" +#include +#include + +struct edge { int to, nxt; } G[2005]; +int cnt, head[2005], wei[2005], indgr[2005], root, sum[2005]; + +std::unordered_map mapping; +std::string back[2005]; + +int cm; + +inline int get_order(const std::string &str) { + if (mapping.count(str)) return mapping[str]; + back[++cm] = str; return mapping[str] = cm; +} +inline void add_edge(int f, int t) { G[++cnt] = (edge) { t, head[f] }; head[f] = cnt; ++indgr[t]; } + +void dfs(int u) { + sum[u] = wei[u]; + for (int i = head[u]; i; i = G[i].nxt) dfs(G[i].to), sum[u] += sum[G[i].to]; + std::vector< std::pair > to_v; + for (int i = head[u]; i; i = G[i].nxt) to_v.emplace_back(sum[G[i].to], G[i].to); + std::sort(to_v.begin(), to_v.end()); + if (to_v.size() && to_v[0].first != to_v.back().first) { + if (to_v[1].first == to_v[0].first) std::cout << wei[to_v.back().second] - (to_v.back().first - to_v[0].first) << std::endl; + else std::cout << wei[to_v[0].second] - (to_v.back().first - to_v[0].first) << std::endl; + std::exit(0); + } +} + +int main() { + freopen("day7.txt", "r", stdin); + { + std::string line; + while (std::getline(std::cin, line)) { + auto [f, _, t] = lib::partition(line, " -> "); + auto [n, __, w] = lib::partition(f, " ("); + int i = get_order(n); + wei[i] = std::stoi(w); + if (t != "") { + std::vector to = lib::split(t, ", "); + for (const std::string &s : to) add_edge(i, get_order(s)); + } + } + } + for (int i = 1; i <= cm; ++i) + if (!indgr[i]) root = i; + dfs(root); + return 0; +} \ No newline at end of file diff --git a/day8_1.cpp b/day8_1.cpp new file mode 100644 index 00000000..21ef598d --- /dev/null +++ b/day8_1.cpp @@ -0,0 +1,35 @@ +#include +#include +#include "pystrlib.hpp" +#include + +int reg[2005]; + +std::unordered_map mapping; +int c; + +inline int get_order(const std::string &str) { return mapping.count(str) ? mapping[str] : (mapping[str] = ++c); } + +inline std::function get_comp(const std::string &str) { + if (str == "<") return std::less(); + else if (str == ">") return std::greater(); + else if (str == "<=") return std::less_equal(); + else if (str == ">=") return std::greater_equal(); + else if (str == "==") return std::equal_to(); + else return std::not_equal_to(); +} + +int main() { + freopen("day8.txt", "r", stdin); + int v = 0; + { + std::string line; + while (std::getline(std::cin, line)) { + std::vector vec = lib::split(line, " "); + if (get_comp(vec[5])(reg[get_order(vec[4])], std::stoi(vec[6]))) + ((reg[get_order(vec[0])] += std::stoi(vec[2]) * (vec[1] == "inc" ? 1 : -1)) > v) && (v = reg[get_order(vec[0])]); + } + } + std::cout << v << std::endl; + return 0; +} \ No newline at end of file diff --git a/day9_1.cpp b/day9_1.cpp new file mode 100644 index 00000000..58a48601 --- /dev/null +++ b/day9_1.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +std::stack lb; +int ans; + +int main() { + freopen("day9.txt", "r", stdin); + char ch; bool cancel = false; + while ((ch = getchar()) != -1) { + if (cancel) { cancel = false; continue; } + if (ch == '!') { cancel = true; continue; } + if (lb.size() && lb.top() == '<') { + if (ch == '>') lb.pop(); + } else { + if (ch == '{') lb.push('{'), ans += lb.size(); + else if (ch == '}') lb.pop(); + else if (ch == '<') lb.push('<'); + } + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/day9_2.cpp b/day9_2.cpp new file mode 100644 index 00000000..5816fd0f --- /dev/null +++ b/day9_2.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +std::stack lb; +int ans; + +int main() { + freopen("day9.txt", "r", stdin); + char ch; bool cancel = false; + while ((ch = getchar()) != -1) { + if (cancel) { cancel = false; continue; } + if (ch == '!') { cancel = true; continue; } + if (lb.size() && lb.top() == '<') { + if (ch == '>') lb.pop(); + else ++ans; + } else { + if (ch == '{') lb.push('{'); + else if (ch == '}') lb.pop(); + else if (ch == '<') lb.push('<'); + } + } + std::cout << ans << std::endl; + return 0; +} \ No newline at end of file diff --git a/pystrlib.hpp b/pystrlib.hpp new file mode 100644 index 00000000..470cdfbe --- /dev/null +++ b/pystrlib.hpp @@ -0,0 +1,245 @@ +#include +#include +#include +#include +#include + +namespace lib { + + std::string capitalize(const std::string &str) { + std::string res = str; + res[0] = std::toupper(res[0]); + for (unsigned i = 1; i < str.size(); ++i) + res[i] = std::tolower(res[i]); + return res; + } + + std::string casefold(const std::string &str) { + std::string res = str; + for (char &c : res) c = std::tolower(c); + return res; + } + + std::string center(const std::string &str, int width, char fill_char = ' ') { + int pc = width - str.size(); + return std::string(pc >> 1, fill_char) + str + std::string(pc - (pc >> 1), fill_char); + } + + int count(const std::string &str, const std::string &sub, int start = 0, int end = -1) { + if (end < 0) end = str.size(); + int cnt = 0; + for (int l = start; (int) (l + sub.size()) <= end; ++l) { + bool match = true; + for (unsigned j = 0; j < sub.size(); ++j) + if (sub[j] != str[l + j]) { match = false; break; } + if (match) ++cnt, l += sub.size(); + } + return cnt; + } + + bool ends_with(const std::string &str, const std::string &suffix, int start = 0, int end = -1) { + if (end < 0) end = str.size(); + if ((unsigned) (end - start) < suffix.size()) return false; + bool match = true; + for (int j = suffix.size(), i = str.size(); j; ) { + --i; --j; + if (str[i] != suffix[j]) { match = false; break; } + } + return match; + } + + bool starts_with(const std::string &str, const std::string &prefix, int start = 0, int end = -1) { + if (end < 0) end = str.size(); + if ((unsigned) (end - start) < prefix.size()) return false; + bool match = true; + for (unsigned j = 0, i = start; j < prefix.size(); ++i, ++j) + if (str[i] != prefix[j]) { match = false; break; } + return match; + } + + std::string expand_tabs(const std::string &str, int tab_size = 8) { + std::string res; + for (char ch : str) { + if (ch == '\t') res += std::string(tab_size, ' '); + else res += ch; + } + return res; + } + + int find(const std::string &str, const std::string &sub, int start = 0, int end = -1) { + if (end < 0) end = str.size(); + for (unsigned i = start; (int) (i + sub.size()) <= end; ++i) { + bool match = true; + for (unsigned j = 0; j < sub.size(); ++j) + if (str[i + j] != sub[j]) { match = false; break; } + if (match) return i; + } + return -1; + } + + std::string join(const std::string &str, const std::vector &slist) { + bool is_first = true; + std::string res; + for (const std::string &s : slist) { + if (is_first) is_first = false; else res += str; + res += s; + } + return res; + } + + std::string ljust(const std::string &str, int width, char fill_char = ' ') { + return str + std::string(width - str.size(), fill_char); + } + + std::string lower(const std::string &str) { + std::string res = str; + for (char &c : res) c = std::tolower(c); + return res; + } + + std::string lstrip(const std::string &str) { + unsigned i = 0; + while (i < str.size()) { + if (!std::isspace(str[i])) break; + ++i; + } + return str.substr(i); + } + + std::string lstrip(const std::string &str, const std::string &char_set) { + unsigned i = 0; + while (i < str.size()) { + bool in = false; + for (char c : char_set) + if (c == str[i]) { in = true; break; } + if (!in) break; + ++i; + } + return str.substr(i); + } + + std::tuple< std::string, std::string, std::string > partition(const std::string &str, const std::string &sep) { + int pos = find(str, sep); + return pos == -1 ? std::make_tuple(str, "", "") : std::make_tuple(str.substr(0, pos), sep, str.substr(pos + sep.size())); + } + + std::string remove_prefix(const std::string &str, const std::string &prefix) { + return starts_with(str, prefix) ? str.substr(prefix.size()) : str; + } + + std::string remove_suffix(const std::string &str, const std::string &suffix) { + return ends_with(str, suffix) ? str.substr(0, str.size() - suffix.size()) : str; + } + + std::string replace(const std::string &str, const std::string &old_sub, const std::string &new_sub, int count = INT_MAX) { + int cnt = 0; + std::string res; + unsigned i = 0; + for (; i <= str.size() - old_sub.size(); ) { + bool match = true; + for (unsigned j = 0; j < old_sub.size(); ++j) + if (str[i + j] != old_sub[j]) { match = false; break; } + if (match) res += new_sub, i += old_sub.size(), ++cnt; + else res.push_back(str[i]), ++i; + if (cnt == count) break; + } + return res + str.substr(i); + } + + int rfind(const std::string &str, const std::string &sub, int start = 0, int end = -1) { + if (end < 0) end = str.size(); + for (int i = end - sub.size(); i >= start; --i) { + bool match = true; + for (unsigned j = 0; j < sub.size(); ++j) + if (str[i + j] != sub[j]) { match = false; break; } + if (match) return i; + } + return -1; + } + + std::string rjust(const std::string &str, int width, char fill_char = ' ') { + return std::string(width - str.size(), fill_char) + str; + } + + std::tuple< std::string, std::string, std::string > rpartition(const std::string &str, const std::string &sep) { + int pos = rfind(str, sep); + return pos == -1 ? std::make_tuple(str, "", "") : std::make_tuple(str.substr(0, pos), sep, str.substr(pos + sep.size())); + } + + std::vector rsplit(const std::string &str, const std::string &sep, int count = -1) { + if (str.size() < sep.size()) return { str }; + int cnt = 0; + std::vector res; + unsigned last = str.size(); + for (int i = str.size() - sep.size(); i >= 0; ) { + bool match = true; + for (unsigned j = 0; j < sep.size(); ++j) + if (str[i + j] != sep[j]) { match = false; break; } + if (match) ++cnt, res.push_back(str.substr(i + sep.size(), last - i - sep.size())), last = i, i -= sep.size(); + else --i; + if (cnt == count) break; + } + res.push_back(str.substr(0, last)); + std::reverse(res.begin(), res.end()); + return res; + } + + std::vector split(const std::string &str, const std::string &sep, int count = -1) { + if (str.size() < sep.size()) return { str }; + int cnt = 0; + std::vector res; + unsigned last = 0; + for (unsigned i = 0; i <= str.size() - sep.size(); ) { + bool match = true; + for (unsigned j = 0; j < sep.size(); ++j) + if (str[i + j] != sep[j]) { match = false; break; } + if (match) ++cnt, res.push_back(str.substr(last, i - last)), i += sep.size(), last = i; + else ++i; + if (cnt == count) break; + } + res.push_back(str.substr(last)); + return res; + } + + std::string rstrip(const std::string &str) { + int i = str.size() - 1; + while (i >= 0) { + if (!std::isspace(str[i])) break; + --i; + } + return str.substr(0, i + 1); + } + + std::string rstrip(const std::string &str, const std::string &char_set) { + int i = str.size() - 1; + while (i >= 0) { + bool in = false; + for (char c : char_set) + if (c == str[i]) { in = true; break; } + if (!in) break; + --i; + } + return str.substr(0, i + 1); + } + + std::string strip(const std::string &str) { + return lstrip(rstrip(str)); + } + + std::string swapcase(const std::string &str) { + std::string res; + for (char ch : str) { + if (std::islower(ch)) res.push_back(std::toupper(ch)); + else if (std::isupper(ch)) res.push_back(std::tolower(ch)); + else res.push_back(ch); + } + return res; + } + + std::string upper(const std::string &str) { + std::string res; + for (char ch : str) res.push_back(std::toupper(ch)); + return res; + } + +} \ No newline at end of file