Skip to content

Commit a6ca3f7

Browse files
committed
Day 5 cpp
1 parent 9a0125e commit a6ca3f7

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

Day5/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CC = g++-13
2+
CFLAGS = -O2 -std=c++20 -Wall
3+
4+
all: sol
5+
6+
sol: sol.cpp
7+
$(CC) $(CFLAGS) -o sol sol.cpp
8+
9+
gen: gen.cpp
10+
$(CC) $(CFLAGS) -o gen gen.cpp
11+
12+
brute: brute.cpp
13+
$(CC) $(CFLAGS) -o brute brute.cpp
14+
15+
clean:
16+
$(RM) sol gen brute

Day5/old.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
using ll = long long;
4+
5+
// Template
6+
#define REP(n) for (int _=0; _<(n); _++)
7+
#define FOR(i, a, b) for (int i=a; i<(b); i++)
8+
#define F0R(i, a) for (int i=0; i<(a); i++)
9+
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
10+
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
11+
12+
#define sz(x) (int)(x).size()
13+
#define all(x) x.begin(), x.end()
14+
15+
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
16+
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
17+
18+
namespace std {
19+
template<class Fun>
20+
class y_combinator_result {
21+
Fun fun_;
22+
public:
23+
template<class T>
24+
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
25+
26+
template<class ...Args>
27+
decltype(auto) operator()(Args &&...args) {
28+
return fun_(std::ref(*this), std::forward<Args>(args)...);
29+
}
30+
};
31+
32+
template<class Fun>
33+
decltype(auto) y_combinator(Fun &&fun) {
34+
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
35+
}
36+
} // namespace std
37+
38+
#define DEBUG(x) cerr << #x << ": " << x << '\n'
39+
template<typename A, typename B>
40+
ostream& operator<<(ostream &os, const pair<A, B> &p) {
41+
return os << '(' << p.first << ", " << p.second << ')';
42+
}
43+
template<typename T_container,
44+
typename T = typename enable_if<!is_same<T_container, string>::value,
45+
typename T_container::value_type>::type>
46+
ostream& operator<<(ostream &os, const T_container &v) {
47+
os << '['; string sep;
48+
for (const T &x : v)
49+
os << sep << x, sep = ", ";
50+
return os << ']';
51+
}
52+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
53+
54+
int main() {
55+
ios_base::sync_with_stdio(false); cin.tie(NULL);
56+
string line;
57+
getline(cin, line);
58+
string t;
59+
vector<ll> seeds;
60+
{
61+
stringstream ss(line.substr(7));
62+
while (getline(ss, t, ' ')) {
63+
// cout << t << endl;
64+
seeds.push_back(stoll(t));
65+
}
66+
}
67+
DEBUG(seeds);
68+
getline(cin, line);
69+
70+
71+
using P = pair<ll, ll>;
72+
using T = vector<pair<P, P>>;
73+
vector<T> V;
74+
REP(7) {
75+
getline(cin, line);
76+
77+
T cur;
78+
while (true) {
79+
getline(cin, line);
80+
if (line.empty()) break;
81+
else {
82+
stringstream ss(line);
83+
ll x, y, d; ss >> x >> y >> d;
84+
cur.push_back({{y, y + d - 1}, {x, x + d - 1}});
85+
}
86+
}
87+
V.push_back(cur);
88+
}
89+
90+
cout << V << endl;
91+
92+
auto get_pos = [&](ll x) {
93+
cout << x << ": " << endl;
94+
for (auto v: V) {
95+
ll nxt = -1;
96+
for (auto [p1, p2]: v) {
97+
auto [a, b] = p1;
98+
auto [c, d] = p2;
99+
if (a <= x && x <= b) {
100+
nxt = c + (x - a);
101+
break;
102+
}
103+
}
104+
if (nxt == -1) nxt = x;
105+
x = nxt;
106+
cout << "now " << x << endl;
107+
}
108+
return x;
109+
};
110+
111+
112+
ll ans = 1e18;
113+
for (auto x: seeds) {
114+
// cout << x << ": " << get_pos(x) << endl;
115+
ckmin(ans, get_pos(x));
116+
}
117+
cout << ans << endl;
118+
}

Day5/sol.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
using ll = long long;
4+
5+
#define REP(n) for (int _=0; _<(n); _++)
6+
#define sz(x) (int)(x).size()
7+
#define all(x) x.begin(), x.end()
8+
9+
int main() {
10+
// Parse input
11+
string line;
12+
getline(cin, line);
13+
string t;
14+
vector<pair<ll, ll>> curRanges;
15+
{
16+
stringstream ss(line);
17+
ss >> t;
18+
vector<ll> v;
19+
ll x;
20+
while (ss >> x) v.push_back(x);
21+
for (int i = 0; i < sz(v); i += 2) {
22+
curRanges.push_back({v[i], v[i]+v[i+1] - 1});
23+
}
24+
}
25+
getline(cin, line);
26+
27+
using P = pair<ll, ll>;
28+
using T = pair<P, P>;
29+
vector<vector<T>> V;
30+
REP(7) {
31+
getline(cin, line);
32+
vector<T> cur;
33+
while (getline(cin, line) && !line.empty()) {
34+
stringstream ss(line);
35+
ll x, y, d; ss >> x >> y >> d;
36+
cur.push_back({{y, y + d - 1}, {x, x + d - 1}});
37+
}
38+
sort(all(cur));
39+
V.push_back(cur);
40+
}
41+
///////////////////////////////////////////////////////////////////////
42+
const ll MAX = 1e18;
43+
for (auto v: V) {
44+
auto next_interval = [&](ll l) -> pair<P, P> {
45+
// Find the interval [X, Y] that contains l (if one exists).
46+
// Otherwise, return the nearest interval to the right (or infinity if none exists)
47+
auto it = std::lower_bound(all(v), pair<P, P>({{l+1, 0}, {0, 0}}));
48+
if (it != v.begin() && l <= prev(it)->first.second) return *prev(it);
49+
else if (it != v.end()) return *it;
50+
else return {{MAX, MAX}, {MAX, MAX}};
51+
};
52+
vector<pair<ll, ll>> nxtRanges;
53+
for (auto [l, r]: curRanges) {
54+
// Split up the range [l, r] into new ranges
55+
ll cur = l;
56+
while (cur <= r) {
57+
auto [p1, p2] = next_interval(cur);
58+
auto [X1, Y1] = p1;
59+
auto [X2, Y2] = p2;
60+
61+
if (X1 > r) {
62+
nxtRanges.push_back({cur, r});
63+
cur = r + 1;
64+
}
65+
else if (X1 <= cur && cur <= Y1) {
66+
ll R = min(r, Y1);
67+
nxtRanges.push_back({X2 + (cur - X1), X2 + (R - X1)});
68+
cur = R + 1;
69+
}
70+
else if (cur < X1) {
71+
nxtRanges.push_back({cur, X1 - 1});
72+
cur = X1;
73+
}
74+
else assert(false);
75+
}
76+
}
77+
curRanges = nxtRanges;
78+
}
79+
cout << std::min_element(all(curRanges))->first << '\n';
80+
}

0 commit comments

Comments
 (0)