first three days

This commit is contained in:
Alessio Molinari
2025-12-03 23:29:17 +01:00
commit ba07ab68f3
19 changed files with 669 additions and 0 deletions

29
day2/CMakeLists.txt Normal file
View File

@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.10)
# Project name and version
project(Day2 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
day2/Makefile Normal file
View File

@@ -0,0 +1,13 @@
clear:
rm -rf build/*
run:
cp input.txt build
cp input_test.txt build
cd build && cmake .. && make && ./Day2 input_test.txt
release:
cp input.txt build
cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && make && ./Day2 input.txt

27
day2/flake.lock generated Normal file
View 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
day2/flake.nix Normal file
View 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";
};
}
);
};
}

1
day2/input_test.txt Normal file
View File

@@ -0,0 +1 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124

111
day2/src/main.cpp Normal file
View File

@@ -0,0 +1,111 @@
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
long check_num_part2(const std::string &num_str) {
if (num_str.length() == 2) {
if (num_str[0] == num_str[1]) {
return std::stol(num_str);
}
return 0;
}
size_t half = std::ceil(num_str.length() / 2);
for (size_t j = 0; j <= half; j++) {
auto sub = num_str.substr(0, j);
auto rest = num_str.substr(j);
if (sub.empty() || rest.empty()) {
continue;
}
int matched = 0;
bool all_matched = true;
for (size_t k = 0; k < rest.length(); k += sub.length()) {
auto rest_sub = rest.substr(k, sub.length());
if (sub != rest_sub) {
all_matched = false;
break;
} else {
matched += 1;
}
}
if (all_matched && matched >= 1) {
return std::stol(num_str);
}
}
return 0;
}
long check_range(const std::string &range, bool part_2) {
auto pos = range.find('-');
long count = 0;
std::cout << "Checking range " << range << std::endl;
if (pos != std::string::npos) {
auto range_start = range.substr(0, pos);
auto range_end = range.substr(pos + 1);
long rstart = std::stol(range_start);
long rend = std::stol(range_end);
for (long i = rstart; i < rend + 1; i++) {
auto istr = std::to_string(i);
if (!part_2) {
if (istr.length() % 2 == 0) {
auto first_half = istr.substr(0, istr.length() / 2);
auto second_half = istr.substr(istr.length() / 2);
count += (first_half == second_half) ? i : 0;
}
} else {
count += check_num_part2(istr);
}
}
} else {
std::cout << "Invalid range format: " << range << std::endl;
std::exit(1);
}
std::cout << "count: " << count << std::endl;
return count;
}
std::tuple<long, long> parse_input_file(const std::string &path,
bool do_part_2_only) {
std::ifstream file;
std::string line;
file.open(path);
long part_1 = 0;
long part_2 = 0;
if (file.is_open()) {
std::getline(file, line);
auto pos = line.find_first_of(',');
while (pos != std::string::npos) {
auto range = line.substr(0, pos);
if (!do_part_2_only)
part_1 += check_range(range, false);
part_2 += check_range(range, true);
line = line.substr(pos + 1);
pos = line.find_first_of(',');
}
if (line.find_first_of('-') != std::string::npos) {
if (!do_part_2_only)
part_1 += check_range(line, false);
part_2 += check_range(line, true);
}
file.close();
} else {
std::cout << "Failed to open file: " << path << std::endl;
}
return {part_1, part_2};
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "One argument required: input txt" << std::endl;
}
auto results = parse_input_file(argv[1], true);
std::cout << "Part 1: " << std::get<0>(results) << std::endl;
std::cout << "Part 2: " << std::get<1>(results) << std::endl;
return 0;
}