feat: add day4
This commit is contained in:
29
day4/CMakeLists.txt
Normal file
29
day4/CMakeLists.txt
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
# Project name and version
|
||||||
|
project(Day4 VERSION 1.0)
|
||||||
|
|
||||||
|
# Set default build type
|
||||||
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
|
set(CMAKE_BUILD_TYPE Debug)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# 1. This is the critical line for Clangd.
|
||||||
|
# It generates the file clangd reads to know where headers are.
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
# Debug and Release flags
|
||||||
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -Wall -Wextra")
|
||||||
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
|
||||||
|
|
||||||
|
# Specify C standard
|
||||||
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
# Add include directory
|
||||||
|
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
# Add executable
|
||||||
|
add_executable(${PROJECT_NAME}
|
||||||
|
src/main.cpp
|
||||||
|
)
|
||||||
13
day4/Makefile
Normal file
13
day4/Makefile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
|
||||||
|
clear:
|
||||||
|
rm -rf build/*
|
||||||
|
|
||||||
|
run:
|
||||||
|
cp input.txt build
|
||||||
|
cp input_test.txt build
|
||||||
|
cd build && cmake .. && make && ./Day4 input_test.txt
|
||||||
|
|
||||||
|
release:
|
||||||
|
cp input.txt build
|
||||||
|
cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && make && ./Day4 input.txt
|
||||||
27
day4/flake.lock
generated
Normal file
27
day4/flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1756787288,
|
||||||
|
"narHash": "sha256-rw/PHa1cqiePdBxhF66V7R+WAP8WekQ0mCDG4CFqT8Y=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "d0fc30899600b9b3466ddb260fd83deb486c32f1",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
58
day4/flake.nix
Normal file
58
day4/flake.nix
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
{
|
||||||
|
description = "A Nix-flake-based C/C++ development environment";
|
||||||
|
|
||||||
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
|
|
||||||
|
outputs =
|
||||||
|
{ self, nixpkgs }:
|
||||||
|
let
|
||||||
|
supportedSystems = [
|
||||||
|
"x86_64-linux"
|
||||||
|
"aarch64-linux"
|
||||||
|
"x86_64-darwin"
|
||||||
|
"aarch64-darwin"
|
||||||
|
];
|
||||||
|
forEachSupportedSystem =
|
||||||
|
f:
|
||||||
|
nixpkgs.lib.genAttrs supportedSystems (
|
||||||
|
system:
|
||||||
|
f {
|
||||||
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
}
|
||||||
|
);
|
||||||
|
in
|
||||||
|
{
|
||||||
|
devShells = forEachSupportedSystem (
|
||||||
|
{ pkgs }:
|
||||||
|
{
|
||||||
|
default =
|
||||||
|
pkgs.mkShell.override
|
||||||
|
{
|
||||||
|
# Override stdenv in order to change compiler:
|
||||||
|
# stdenv = pkgs.clangStdenv;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
nativeBuildInputs = with pkgs; [
|
||||||
|
pkg-config
|
||||||
|
];
|
||||||
|
packages =
|
||||||
|
with pkgs;
|
||||||
|
[
|
||||||
|
clang-tools
|
||||||
|
cmake
|
||||||
|
codespell
|
||||||
|
cppcheck
|
||||||
|
doxygen
|
||||||
|
gtest
|
||||||
|
lcov
|
||||||
|
nodejs
|
||||||
|
vscode-extensions.vadimcn.vscode-lldb
|
||||||
|
]
|
||||||
|
++ (if system == "aarch64-darwin" then [ ] else [ gdb ]);
|
||||||
|
CODELLDB_PATH = "${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode-lldb/adapter/codelldb";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
10
day4/input_test.txt
Normal file
10
day4/input_test.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
..@@.@@@@.
|
||||||
|
@@@.@.@.@@
|
||||||
|
@@@@@.@.@@
|
||||||
|
@.@@@@..@.
|
||||||
|
@@.@@@@.@@
|
||||||
|
.@@@@@@@.@
|
||||||
|
.@.@.@.@@@
|
||||||
|
@.@@@.@@@@
|
||||||
|
.@@@@@@@@.
|
||||||
|
@.@.@@@.@.
|
||||||
129
day4/src/main.cpp
Normal file
129
day4/src/main.cpp
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <numeric>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
typedef std::vector<std::vector<uint8_t>> Matrix;
|
||||||
|
|
||||||
|
void print_matrix(const Matrix &matrix) {
|
||||||
|
for (const auto &row : matrix) {
|
||||||
|
for (const auto &val : row) {
|
||||||
|
std::cout << static_cast<int>(val);
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int num_papers_adjacent(const Matrix &matrix, int row, int col) {
|
||||||
|
int element_up = 0, element_up_left = 0, element_up_right = 0,
|
||||||
|
element_right = 0, element_left = 0, element_down = 0,
|
||||||
|
element_down_left = 0, element_down_right = 0;
|
||||||
|
if (row > 0) {
|
||||||
|
element_up = matrix[row - 1][col];
|
||||||
|
}
|
||||||
|
if (row < matrix.size() - 1) {
|
||||||
|
element_down = matrix[row + 1][col];
|
||||||
|
}
|
||||||
|
if (col > 0) {
|
||||||
|
element_left = matrix[row][col - 1];
|
||||||
|
}
|
||||||
|
if (col < matrix[0].size() - 1) {
|
||||||
|
element_right = matrix[row][col + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (row > 0 && col > 0) {
|
||||||
|
element_up_left = matrix[row - 1][col - 1];
|
||||||
|
}
|
||||||
|
if (row > 0 && col < matrix[0].size() - 1) {
|
||||||
|
element_up_right = matrix[row - 1][col + 1];
|
||||||
|
}
|
||||||
|
if (row < matrix.size() - 1 && col > 0) {
|
||||||
|
element_down_left = matrix[row + 1][col - 1];
|
||||||
|
}
|
||||||
|
if (row < matrix.size() - 1 && col < matrix[0].size() - 1) {
|
||||||
|
element_down_right = matrix[row + 1][col + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return element_up + element_up_left + element_up_right + element_right +
|
||||||
|
element_left + element_down + element_down_left + element_down_right;
|
||||||
|
}
|
||||||
|
|
||||||
|
int compute_accessible_papers(const Matrix &matrix) {
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < matrix.size(); i++) {
|
||||||
|
for (int j = 0; j < matrix[i].size(); j++) {
|
||||||
|
if (matrix[i][j] == 1) {
|
||||||
|
int num_adjacent = num_papers_adjacent(matrix, i, j);
|
||||||
|
sum += num_adjacent < 4 ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
int identify_and_remove_papers(Matrix &matrix, int result) {
|
||||||
|
std::vector<std::tuple<int, int>> to_remove;
|
||||||
|
for (int i = 0; i < matrix.size(); i++) {
|
||||||
|
for (int j = 0; j < matrix[i].size(); j++) {
|
||||||
|
if (matrix[i][j] == 1) {
|
||||||
|
int num_adjacent = num_papers_adjacent(matrix, i, j);
|
||||||
|
if (num_adjacent < 4) {
|
||||||
|
to_remove.emplace_back(i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result += to_remove.size();
|
||||||
|
if (to_remove.size() == 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto &t : to_remove) {
|
||||||
|
matrix[std::get<0>(t)][std::get<1>(t)] = 0;
|
||||||
|
}
|
||||||
|
return identify_and_remove_papers(matrix, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix parse_input_file(const std::string &path) {
|
||||||
|
std::ifstream file;
|
||||||
|
std::string line;
|
||||||
|
Matrix matrix;
|
||||||
|
|
||||||
|
file.open(path);
|
||||||
|
if (file.is_open()) {
|
||||||
|
std::getline(file, line);
|
||||||
|
while (!line.empty()) {
|
||||||
|
std::vector<uint8_t> row(line.length(), 0);
|
||||||
|
for (size_t i = 0; i < line.length(); i++) {
|
||||||
|
if (line[i] == '@') {
|
||||||
|
row[i] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
matrix.emplace_back(row);
|
||||||
|
std::getline(file, line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
std::cerr << "Error opening file: " << path << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if (argc < 2) {
|
||||||
|
std::cout << "One argument required: input txt" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto matrix = parse_input_file(argv[1]);
|
||||||
|
print_matrix(matrix);
|
||||||
|
int sum = compute_accessible_papers(matrix);
|
||||||
|
std::cout << "Number of accessible papers: " << sum << std::endl;
|
||||||
|
|
||||||
|
int removed_papers = identify_and_remove_papers(matrix, 0);
|
||||||
|
std::cout << "Number of removed papers: " << removed_papers << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user