aoc25/day4/main.cpp
Feiko Wielsma eac8571ea7 Refactor CMake and VSCode settings; add Day 4 solution
- Updated .vscode/settings.json to enhance Clangd configuration and disable IntelliSense.
- Removed obsolete Clang-Format and Clang-Tidy tasks from .vscode/tasks.json.
- Modified CMakeLists.txt to enable compile commands export and adjusted target properties for Day 4.
- Added new CMakeLists.txt and main.cpp for Day 4 solution, implementing diagram parsing and movable paper counting logic.
- Included test_input and puzzle_input files for Day 4.
- Deleted the run-clang-tidy.sh script as it is no longer needed.
2025-12-04 14:22:43 +00:00

128 lines
3.8 KiB
C++

#include <algorithm>
#include <array>
#include <cstddef>
#include <expected>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <mdspan>
#include <numeric>
#include <print>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
struct Diagram {
std::vector<int> data;
size_t width;
size_t height;
auto mdspan() const { return std::mdspan(data.data(), height, width); }
};
auto printMap(const Diagram &diagram) {
auto mdspan = diagram.mdspan();
for (auto row = 0UZ; row != mdspan.extent(0); row++) {
for (auto col = 0UZ; col != mdspan.extent(1); col++) {
std::print("{}", mdspan[row, col]);
}
std::println();
}
// std::println("Map: {}", diagram.data);
std::println("Width: {}", diagram.width);
std::println("Height: {}", diagram.height);
}
auto parseMap(const std::string &filename) -> std::expected<Diagram, std::string> {
std::ifstream inputF{filename};
if (!inputF) {
return std::unexpected{"Some file open error.\n"};
}
Diagram diagram{};
std::string puzzleLine{};
while (std::getline(inputF, puzzleLine)) {
// std::println("{:3}: {}", diagram.height, puzzleLine);
diagram.data.push_back(0);
diagram.data.append_range(puzzleLine | std::views::transform([](char c) -> int { return c == '.' ? 0 : 1; }) |
std::ranges::to<std::vector<int>>());
diagram.data.push_back(0);
diagram.height++;
// ugly
diagram.width = puzzleLine.length() + 2;
}
// also ugly, bound it with 0s
diagram.data.append_range(std::vector<int>(diagram.width, 0));
diagram.data.insert_range(diagram.data.begin(), std::vector<int>(diagram.width, 0));
diagram.height += 2;
printMap(diagram);
// auto ms2 = std::mdspan(diagram.data.data(), diagram.width, diagram.height);
return std::move(diagram);
}
auto countMovablePaper(const Diagram &diagram) -> long {
long nMovablePaper{};
auto mdspan = diagram.mdspan();
for (auto row = 1UZ; row != mdspan.extent(0) - 1; row++) {
for (auto col = 1UZ; col != mdspan.extent(1) - 1; col++) {
if (mdspan[row, col] == 1) {
// std::print("checking [{},{}]:", row, col);
// shitty kernal
int upLeft = mdspan[row - 1, col - 1];
int up = mdspan[row - 1, col];
int upRight = mdspan[row - 1, col + 1];
int left = mdspan[row, col - 1];
int right = mdspan[row, col + 1];
int downLeft = mdspan[row + 1, col - 1];
int down = mdspan[row + 1, col];
int downRight = mdspan[row + 1, col + 1];
// caveman shit
if ((upLeft + up + upRight + left + right + downLeft + down + downRight) < 4) {
// std::println("Found");
nMovablePaper++;
}
}
// std::println();
}
}
return nMovablePaper;
}
auto main() -> int {
auto testCase = parseMap("test_input");
if (testCase) {
auto testResult = countMovablePaper(*testCase);
std::println("P1 Testcase result: {}", testResult);
// auto testResultP2 = countMovablePaper(*testCase);
// std::println("P2 Testcase result: {}", testResultP2);
} else {
std::print("{}\n", testCase.error());
}
auto realPuzzle = parseMap("puzzle_input");
if (realPuzzle) {
auto realResult = countMovablePaper(*realPuzzle);
std::println("P1 Real result: {}", realResult);
// auto realResultP2 = countJoltagesP2(*realPuzzle);
// std::println("P2 Real result: {}", realResultP2);
} else {
std::print("{}\n", realPuzzle.error());
}
return 0;
}