Skip to content

Commit 45ad62d

Browse files
committed
Day 6
1 parent c4e953b commit 45ad62d

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed

AdventOfCode23.cabal

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ executable Day4
7878
import: warnings, dependencies
7979
main-is: Main.hs
8080
hs-source-dirs: Day4
81+
82+
executable Day6
83+
import: warnings, dependencies
84+
main-is: Main.hs
85+
hs-source-dirs: Day6

Day6/Main.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Main where
2+
3+
import Data.Tuple.Extra (both)
4+
5+
-- Count the number of integer points x where (t - x) * x > d.
6+
getRoots :: Int -> Int -> Int
7+
getRoots t d =
8+
if disc <= 0
9+
then 0
10+
else max 0 (r - l + 1)
11+
where
12+
disc :: Double
13+
disc = fromIntegral (t * t - 4 * d)
14+
l = ceil' ((fromIntegral t - sqrt disc) / 2.0)
15+
r = floor' ((fromIntegral t + sqrt disc) / 2.0)
16+
isInt x = x == fromInteger (round x)
17+
ceil' x = ceiling x + if isInt x then 1 else 0
18+
floor' x = floor x + if isInt x then -1 else 0
19+
20+
part1 :: [(Int, Int)] -> Int
21+
part1 = product . map (uncurry getRoots)
22+
23+
part2 :: (Int, Int) -> Int
24+
part2 = uncurry getRoots
25+
26+
main :: IO ()
27+
main = do
28+
times <- drop 1 . words <$> getLine
29+
distances <- drop 1 . words <$> getLine
30+
let part1Input = uncurry zip (both (map read) (times, distances))
31+
part2Input = both (read . concat) (times, distances)
32+
putStrLn $ "Part 1: " ++ show (part1 part1Input)
33+
putStrLn $ "Part 2: " ++ show (part2 part2Input)

Day6/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

Day6/old.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
int N = 4;
57+
string t;
58+
vector<int> times(N), dist(N);
59+
cin >> t;
60+
F0R(i, 4) cin >> times[i];
61+
cin >> t;
62+
F0R(i, 4) cin >> dist[i];
63+
64+
ll ans = 1;
65+
F0R(i, N) {
66+
int T = times[i];
67+
int record = dist[i];
68+
69+
int ways = 0;
70+
for (int x = 0; x <= T; x++) {
71+
int d = (T - x) * x;
72+
if (d > record) ways++;
73+
}
74+
DEBUG(ways);
75+
76+
ans *= ways;
77+
}
78+
cout << ans << '\n';
79+
}

Day6/sol.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
int N = 4;
57+
string t;
58+
vector<int> times(N), dist(N);
59+
cin >> t;
60+
string TIME;
61+
F0R(i, 4) {
62+
cin >> t;
63+
TIME += t;
64+
}
65+
66+
string DIST;
67+
cin >> t;
68+
F0R(i, 4) {
69+
cin >> t;
70+
DIST += t;
71+
}
72+
73+
ll my_time, my_dist;
74+
{
75+
stringstream ss(TIME);
76+
ss >> my_time;
77+
}
78+
{
79+
stringstream ss(DIST);
80+
ss >> my_dist;
81+
}
82+
83+
DEBUG(my_time);
84+
DEBUG(my_dist);
85+
86+
int ways = 0;
87+
vector<ll> v;
88+
for (int x = 0; x <= my_time; x++) {
89+
ll d = (my_time - x) * x;
90+
if (d > my_dist) {
91+
v.push_back(x);
92+
ways++;
93+
}
94+
}
95+
DEBUG(ways);
96+
cout << v.front() << ' ' << v.back() << endl;
97+
}

0 commit comments

Comments
 (0)