diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-18 06:38:01 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-18 06:38:01 +0100 |
| commit | 6bf92f2a2517f8eebddbdb139a877dcffd381edc (patch) | |
| tree | cbe299f4501d13527260daa87b852e2a996c580f /2024 | |
| parent | a0e775b0a4805a7c1af12c1a956cd9fd74c54b2c (diff) | |
| download | aoc-6bf92f2a2517f8eebddbdb139a877dcffd381edc.tar.gz aoc-6bf92f2a2517f8eebddbdb139a877dcffd381edc.zip | |
Day 18 2024
Diffstat (limited to '2024')
| -rw-r--r-- | 2024/18/Makefile | 24 | ||||
| -rw-r--r-- | 2024/18/day18a.cpp | 63 | ||||
| -rw-r--r-- | 2024/18/day18b.cpp | 74 |
3 files changed, 161 insertions, 0 deletions
diff --git a/2024/18/Makefile b/2024/18/Makefile new file mode 100644 index 0000000..1372bff --- /dev/null +++ b/2024/18/Makefile | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | CC=g++ -std=c++20 -g -Wall | ||
| 2 | |||
| 3 | a: | ||
| 4 | ${CC} -o a.out day18a.cpp | ||
| 5 | |||
| 6 | b: | ||
| 7 | ${CC} -o b.out day18b.cpp | ||
| 8 | |||
| 9 | clean: | ||
| 10 | rm -f a b | ||
| 11 | |||
| 12 | atest: a | ||
| 13 | ./a.out | ||
| 14 | |||
| 15 | btest: b | ||
| 16 | ./b.out | ||
| 17 | |||
| 18 | arun: a | ||
| 19 | ./a.out < input | ||
| 20 | |||
| 21 | brun: b | ||
| 22 | ./b.out < input | ||
| 23 | |||
| 24 | .PHONY: a b clean atest btest arun brun | ||
diff --git a/2024/18/day18a.cpp b/2024/18/day18a.cpp new file mode 100644 index 0000000..7c795a9 --- /dev/null +++ b/2024/18/day18a.cpp | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cstdint> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <queue> | ||
| 6 | #include <ranges> | ||
| 7 | #include <set> | ||
| 8 | #include <sstream> | ||
| 9 | #include <string> | ||
| 10 | #include <string_view> | ||
| 11 | #include <vector> | ||
| 12 | using namespace std; | ||
| 13 | |||
| 14 | #define N 71 | ||
| 15 | #define S 1024 | ||
| 16 | |||
| 17 | bool b[N][N]; | ||
| 18 | |||
| 19 | class Qelem { | ||
| 20 | public: | ||
| 21 | int d, x, y; | ||
| 22 | Qelem(int dis, int xx, int yy) : d{dis}, x{xx}, y{yy} {} | ||
| 23 | bool operator<(const Qelem& other) const { return d > other.d; } | ||
| 24 | }; | ||
| 25 | |||
| 26 | |||
| 27 | int coord(int x, int y) { | ||
| 28 | return N*x+y; | ||
| 29 | } | ||
| 30 | |||
| 31 | bool inrange(int x, int y) { | ||
| 32 | return x >= 0 && x < N && y >= 0 && y < N; | ||
| 33 | } | ||
| 34 | |||
| 35 | int shortest_path(bool b[N][N]) { | ||
| 36 | vector<bool> vis(N*N); | ||
| 37 | |||
| 38 | priority_queue<Qelem> q; | ||
| 39 | q.push(Qelem(0, 0, 0)); | ||
| 40 | |||
| 41 | while (!q.empty()) { | ||
| 42 | auto v = q.top(); | ||
| 43 | q.pop(); | ||
| 44 | if (!inrange(v.x, v.y) || b[v.x][v.y] || vis[coord(v.x, v.y)]) | ||
| 45 | continue; | ||
| 46 | if (v.x == N-1 && v.y == N-1) return v.d; | ||
| 47 | vis[coord(v.x, v.y)] = true; | ||
| 48 | q.push(Qelem(v.d+1, v.x+1, v.y)); | ||
| 49 | q.push(Qelem(v.d+1, v.x-1, v.y)); | ||
| 50 | q.push(Qelem(v.d+1, v.x, v.y+1)); | ||
| 51 | q.push(Qelem(v.d+1, v.x, v.y-1)); | ||
| 52 | } | ||
| 53 | return -1; | ||
| 54 | } | ||
| 55 | |||
| 56 | int main() { | ||
| 57 | int x, y; | ||
| 58 | for (int i = 0; i < S && (cin >> x >> y); i++) | ||
| 59 | b[x][y] = true; | ||
| 60 | |||
| 61 | cout << shortest_path(b) << endl; | ||
| 62 | return 0; | ||
| 63 | } | ||
diff --git a/2024/18/day18b.cpp b/2024/18/day18b.cpp new file mode 100644 index 0000000..dadd259 --- /dev/null +++ b/2024/18/day18b.cpp | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | /* | ||
| 2 | I just look the shortest path after every byte drop and stop when there | ||
| 3 | is none. This code is not very fast (~25 seconds on my laptop), but it | ||
| 4 | took just a couple of minutes to modify part 1 to get this. | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <iostream> | ||
| 10 | #include <map> | ||
| 11 | #include <queue> | ||
| 12 | #include <ranges> | ||
| 13 | #include <set> | ||
| 14 | #include <sstream> | ||
| 15 | #include <string> | ||
| 16 | #include <string_view> | ||
| 17 | #include <vector> | ||
| 18 | using namespace std; | ||
| 19 | |||
| 20 | #define N 71 | ||
| 21 | |||
| 22 | bool b[N][N]; | ||
| 23 | |||
| 24 | class Qelem { | ||
| 25 | public: | ||
| 26 | int d, x, y; | ||
| 27 | Qelem(int dis, int xx, int yy) : d{dis}, x{xx}, y{yy} {} | ||
| 28 | bool operator<(const Qelem& other) const { return d > other.d; } | ||
| 29 | }; | ||
| 30 | |||
| 31 | |||
| 32 | int coord(int x, int y) { | ||
| 33 | return N*x+y; | ||
| 34 | } | ||
| 35 | |||
| 36 | bool inrange(int x, int y) { | ||
| 37 | return x >= 0 && x < N && y >= 0 && y < N; | ||
| 38 | } | ||
| 39 | |||
| 40 | int shortest_path() { | ||
| 41 | vector<bool> vis(N*N); | ||
| 42 | |||
| 43 | priority_queue<Qelem> q; | ||
| 44 | q.push(Qelem(0, 0, 0)); | ||
| 45 | |||
| 46 | while (!q.empty()) { | ||
| 47 | auto v = q.top(); | ||
| 48 | q.pop(); | ||
| 49 | if (!inrange(v.x, v.y) || b[v.x][v.y] || vis[coord(v.x, v.y)]) | ||
| 50 | continue; | ||
| 51 | if (v.x == N-1 && v.y == N-1) return v.d; | ||
| 52 | vis[coord(v.x, v.y)] = true; | ||
| 53 | q.push(Qelem(v.d+1, v.x+1, v.y)); | ||
| 54 | q.push(Qelem(v.d+1, v.x-1, v.y)); | ||
| 55 | q.push(Qelem(v.d+1, v.x, v.y+1)); | ||
| 56 | q.push(Qelem(v.d+1, v.x, v.y-1)); | ||
| 57 | } | ||
| 58 | return -1; | ||
| 59 | } | ||
| 60 | |||
| 61 | int main() { | ||
| 62 | int x, y; | ||
| 63 | while (cin >> x >> y) { | ||
| 64 | b[x][y] = true; | ||
| 65 | if (shortest_path() == -1) { | ||
| 66 | cout << x << "," << y << endl; | ||
| 67 | exit(0); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | cout << "Not found" << endl; | ||
| 72 | |||
| 73 | return 0; | ||
| 74 | } | ||
