112 lines
No EOL
3.1 KiB
C++
112 lines
No EOL
3.1 KiB
C++
#include <algorithm>
|
|
#include <array>
|
|
#include <charconv>
|
|
#include <cstddef>
|
|
#include <expected>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <list>
|
|
#include <map>
|
|
#include <mdspan>
|
|
#include <numeric>
|
|
#include <print>
|
|
#include <ranges>
|
|
#include <set>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
struct CoordinateHash {
|
|
auto operator()(const std::pair<long long, long long> &p) const -> std::size_t {
|
|
|
|
auto h1 = std::hash<long long>{}(p.first);
|
|
auto h2 = std::hash<long long>{}(p.second);
|
|
|
|
return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2));
|
|
}
|
|
};
|
|
|
|
using Coordinate = std::pair<int, int>;
|
|
|
|
static auto parseInput(std::string_view filename) -> std::expected<std::vector<Coordinate>, std::string> {
|
|
std::ifstream inputF{std::string(filename)};
|
|
if (!inputF) {
|
|
return std::unexpected{"Could not open file."};
|
|
}
|
|
|
|
std::vector<Coordinate> coordinates;
|
|
std::string line;
|
|
|
|
while (std::getline(inputF, line)) {
|
|
if (line.empty()) {
|
|
continue;
|
|
}
|
|
|
|
auto parts = line | std::views::split(',') |
|
|
std::views::transform([](auto &&rng) -> auto { return std::string(rng.begin(), rng.end()); }) |
|
|
std::ranges::to<std::vector<std::string>>();
|
|
|
|
if (parts.size() >= 2) {
|
|
coordinates.emplace_back(std::stoi(parts[0]), std::stoi(parts[1]));
|
|
}
|
|
}
|
|
return coordinates;
|
|
}
|
|
|
|
static auto getArea(const Coordinate &left, const Coordinate &right) -> long long {
|
|
long long leftPart = std::abs(left.first - right.first) + 1;
|
|
long long rightPart = std::abs(left.second - right.second) + 1;
|
|
// std::print("{:2} * {:2} ", leftPart, rightPart);
|
|
return leftPart * rightPart;
|
|
}
|
|
|
|
static auto solvePartOne(std::vector<Coordinate> &coordinates) -> long long {
|
|
long long maxArea{};
|
|
for (size_t i = 0; i < coordinates.size(); ++i) {
|
|
for (size_t j = i + 1; j < coordinates.size(); ++j) {
|
|
long long area = getArea(coordinates[i], coordinates[j]);
|
|
maxArea = std::max(area, maxArea);
|
|
// std::println("= {:2}; area of {} and {}", area, coordinates[i], coordinates[j]);
|
|
}
|
|
}
|
|
|
|
return maxArea;
|
|
}
|
|
|
|
auto main() -> int {
|
|
auto test = parseInput("test_input");
|
|
|
|
if (!test) {
|
|
std::println(stderr, "Error: {}", test.error());
|
|
return 1;
|
|
}
|
|
|
|
std::println("Test: Loaded {} points.", test->size());
|
|
|
|
auto testResult = solvePartOne(*test);
|
|
std::println("Test: Max Area: {}.", testResult);
|
|
|
|
auto puzzle = parseInput("puzzle_input");
|
|
|
|
if (!puzzle) {
|
|
std::println(stderr, "Error: {}", puzzle.error());
|
|
return 1;
|
|
}
|
|
|
|
std::println("Puzzle: Loaded {} points.", puzzle->size());
|
|
auto puzzleResult = solvePartOne(*puzzle);
|
|
std::println("Puzzle: Max Area: {}.", puzzleResult);
|
|
|
|
// // Part 2 Logic
|
|
// auto result = solvePartTwo(*puzzle);
|
|
// std::println("Part 2 Result: {}", result);
|
|
|
|
return 0;
|
|
} |