diff options
author | kdx <kikoodx@paranoici.org> | 2023-12-11 06:43:04 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-12-11 06:43:04 +0100 |
commit | c56c72fa331716005144c1ea6d43123571bb2068 (patch) | |
tree | 0e545ad009b0af5cda0f466bb24a42680aa691db | |
parent | b921edf1747e806bd0bfcf25c1f24f6271366fd4 (diff) | |
download | aoc23-c56c72fa331716005144c1ea6d43123571bb2068.tar.gz |
11
-rw-r--r-- | 11/_.h | 39 | ||||
-rwxr-xr-x | 11/build.sh | 2 | ||||
-rwxr-xr-x | 11/commit.sh | 3 | ||||
-rw-r--r-- | 11/compile_flags.txt | 4 | ||||
-rw-r--r-- | 11/main.cpp | 80 | ||||
-rwxr-xr-x | 11/submit.sh | 6 | ||||
-rwxr-xr-x | 11/test.sh | 2 |
7 files changed, 136 insertions, 0 deletions
@@ -0,0 +1,39 @@ +#pragma once +#include <string> +#include <iostream> +#include <vector> +using namespace std; + +void print(auto v) { + cout << v << endl; +} + +vector<string> +split(string str, const string &sep, bool ignore_empty = true) +{ + vector<string> vec; + + bool end_sep = (sep.length() <= str.length() && + str.substr(str.length() - sep.length()) == sep); + + for (;;) { + const size_t find = str.find(sep); + if (find == string::npos) { + break; + } + string const bit = str.substr(0, find); + if (!ignore_empty || !bit.empty()) { + vec.push_back(bit); + } + str.erase(0, find + sep.length()); + } + + if (!str.empty()) { + vec.push_back(str); + } + if (!ignore_empty && end_sep) { + vec.push_back(""); + } + return vec; +} + diff --git a/11/build.sh b/11/build.sh new file mode 100755 index 0000000..fe67df4 --- /dev/null +++ b/11/build.sh @@ -0,0 +1,2 @@ +#!/bin/sh +g++ -std=c++20 -Wall -Wextra -include_.h *.cpp diff --git a/11/commit.sh b/11/commit.sh new file mode 100755 index 0000000..4daa1c6 --- /dev/null +++ b/11/commit.sh @@ -0,0 +1,3 @@ +#!/bin/sh +git add . +git commit -m "$(basename $(pwd))" diff --git a/11/compile_flags.txt b/11/compile_flags.txt new file mode 100644 index 0000000..9e4630a --- /dev/null +++ b/11/compile_flags.txt @@ -0,0 +1,4 @@ +-std=c++20 +-Wall +-Wextra +-include_.h diff --git a/11/main.cpp b/11/main.cpp new file mode 100644 index 0000000..4e3eda5 --- /dev/null +++ b/11/main.cpp @@ -0,0 +1,80 @@ +struct Point { + unsigned x, y; +}; + +int ABS(int x) { + if (x < 0) return -x; + return x; +} + +int main() { + string line; + vector<string> galaxy{}; + vector<Point> points{}; + while (getline(cin, line)) { + galaxy.push_back(line); + if (line.find('#') == line.npos) { + for (auto &c : line) + c = '/'; + galaxy.push_back(line); + } + } + + auto i = 0u; + while (i < galaxy[0].length()) { + bool empty = true; + for (auto k = 0u; k < galaxy.size(); k++) { + if (galaxy[k][i] == '#') { + empty = false; + break; + } + } + if (empty) { + for (auto& line : galaxy) { + line.insert(i, "/"); + } + i += 1; + } + i += 1; + } + + for (auto y = 0u; y < galaxy.size(); y++) { + for (auto x = 0u; x < galaxy[0].length(); x++) { + if (galaxy[y][x] == '#') + points.push_back({x, y}); + } + } + + long result = 0; + for (auto i = 0u; i < points.size(); i++) { + for (auto k = i + 1; k < points.size(); k++) { + long dist = ABS(points[i].x - points[k].x) + ABS(points[i].y - points[k].y); + long dist_g = 0; + + unsigned x = min(points[i].x, points[k].x); + unsigned max_x = max(points[i].x, points[k].x); + unsigned y = min(points[i].y, points[k].y); + unsigned max_y = max(points[i].y, points[k].y); + + while (x < max_x) { + x += 1; + if (galaxy[y][x] == '/') { + dist_g += 1; + dist -= 1; + } + } + while (y < max_y) { + y += 1; + if (galaxy[y][x] == '/') { + dist_g += 1; + dist -= 1; + } + } + + result += dist + dist_g * 999999; + } + } + print(result); + + return 0; +} diff --git a/11/submit.sh b/11/submit.sh new file mode 100755 index 0000000..116caff --- /dev/null +++ b/11/submit.sh @@ -0,0 +1,6 @@ +#!/bin/sh +PART="$2" +ARG="$1" +test "$ARG" == "" && ARG="$(wl-paste)" +test "$PART" == "" && PART="1" +aoc -y 2023 -d "$(basename $(pwd))" submit "$PART" "$ARG" diff --git a/11/test.sh b/11/test.sh new file mode 100755 index 0000000..2fdcb42 --- /dev/null +++ b/11/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh +./build.sh && ./a.out <input |