Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions day10_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include "pystrlib.hpp"
#include <algorithm>

std::vector<int> 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;
}
47 changes: 47 additions & 0 deletions day10_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include "pystrlib.hpp"
#include <algorithm>
#include <iomanip>

std::vector<int> 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;
}
41 changes: 41 additions & 0 deletions day11_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// se: (1, 0)
// ne: (0, 1)
// n: (-1, 1)
// nw: (-1, 0)
// sw: (0, -1)
// s: (1, -1)

// \|
// -s-
// |\

#include <iostream>
#include "pystrlib.hpp"
#include <cmath>

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<std::string> 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;
}
44 changes: 44 additions & 0 deletions day11_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// se: (1, 0)
// ne: (0, 1)
// n: (-1, 1)
// nw: (-1, 0)
// sw: (0, -1)
// s: (1, -1)

// \|
// -s-
// |\

#include <iostream>
#include "pystrlib.hpp"
#include <cmath>

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<std::string> 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;
}
32 changes: 32 additions & 0 deletions day12_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <queue>
#include <numeric>
#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<int> Q;
int V[2000], n;

int main() {
freopen("day12.txt", "r", stdin);
{
std::string line;
while (std::getline(std::cin, line)) {
std::vector<std::string> 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;
}
38 changes: 38 additions & 0 deletions day12_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <queue>
#include <numeric>
#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<int> 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<std::string> 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;
}
18 changes: 18 additions & 0 deletions day13_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include "pystrlib.hpp"
#include <utility>

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;
}
24 changes: 24 additions & 0 deletions day13_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include "pystrlib.hpp"
#include <utility>

std::vector< std::pair<int, int> > 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; }
}
61 changes: 61 additions & 0 deletions day14_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>

namespace hash {

std::vector<int> 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;
}
Loading