first three days
This commit is contained in:
29
day3/CMakeLists.txt
Normal file
29
day3/CMakeLists.txt
Normal file
@@ -0,0 +1,29 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Project name and version
|
||||
project(Day3 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
day3/Makefile
Normal file
13
day3/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 && ./Day3 input_test.txt
|
||||
|
||||
release:
|
||||
cp input.txt build
|
||||
cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && make && ./Day3 input.txt
|
||||
27
day3/flake.lock
generated
Normal file
27
day3/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
day3/flake.nix
Normal file
58
day3/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";
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
4
day3/input_test.txt
Normal file
4
day3/input_test.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111
|
||||
90
day3/src/main.cpp
Normal file
90
day3/src/main.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
|
||||
int do_part_1(const std::string &line) {
|
||||
int greatest = -1;
|
||||
int second_greatest = -1;
|
||||
for (size_t i = 0; i < line.length(); i++) {
|
||||
char c = line[i];
|
||||
int j = static_cast<int>(c - '0');
|
||||
if (j > greatest && i < (line.length() - 1)) {
|
||||
second_greatest = -1;
|
||||
greatest = j;
|
||||
} else if (j > second_greatest) {
|
||||
second_greatest = j;
|
||||
}
|
||||
}
|
||||
|
||||
auto joint = std::to_string(greatest) + std::to_string(second_greatest);
|
||||
|
||||
return std::stoi(joint);
|
||||
}
|
||||
|
||||
int find_highest_leading(std::string &line, int len) {
|
||||
int highest_leading = -1;
|
||||
int highest_pos = -1;
|
||||
for (size_t i = 0; i < (line.length() - len + 1); i++) {
|
||||
int j = static_cast<int>(line[i] - '0');
|
||||
if (j > highest_leading) {
|
||||
highest_leading = j;
|
||||
highest_pos = i;
|
||||
}
|
||||
}
|
||||
line = line.substr(highest_pos + 1);
|
||||
return highest_leading;
|
||||
}
|
||||
|
||||
long do_part_2(std::string line) {
|
||||
std::string highest_seq = "";
|
||||
for (int i = 12; i > 0; i--) {
|
||||
std::cout << "line " << line << std::endl;
|
||||
int highest_leading = find_highest_leading(line, i);
|
||||
highest_seq += std::to_string(highest_leading);
|
||||
}
|
||||
return std::stol(highest_seq);
|
||||
}
|
||||
|
||||
std::tuple<long, long> parse_input_file(const std::string &path) {
|
||||
std::ifstream file;
|
||||
std::string line;
|
||||
|
||||
file.open(path);
|
||||
long part_1 = 0;
|
||||
long part_2 = 0;
|
||||
std::vector<int> part1_ints;
|
||||
std::vector<long> part2_longs;
|
||||
if (file.is_open()) {
|
||||
std::getline(file, line);
|
||||
while (!line.empty()) {
|
||||
part_1 = do_part_1(line);
|
||||
part1_ints.emplace_back(part_1);
|
||||
part_2 = do_part_2(line);
|
||||
part2_longs.emplace_back(part_2);
|
||||
std::getline(file, line);
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Error opening file: " << path << std::endl;
|
||||
return {0, 0};
|
||||
}
|
||||
|
||||
part_1 = std::accumulate(part1_ints.begin(), part1_ints.end(), 0L);
|
||||
part_2 = std::accumulate(part2_longs.begin(), part2_longs.end(), 0L);
|
||||
|
||||
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]);
|
||||
std::cout << "Part 1: " << std::get<0>(results) << std::endl;
|
||||
std::cout << "Part 2: " << std::get<1>(results) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user