first three days
This commit is contained in:
29
day1/CMakeLists.txt
Normal file
29
day1/CMakeLists.txt
Normal file
@@ -0,0 +1,29 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Project name and version
|
||||
project(Day1 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
|
||||
)
|
||||
9
day1/Makefile
Normal file
9
day1/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
|
||||
clear:
|
||||
rm -rf build/*
|
||||
|
||||
run:
|
||||
cp input.txt build
|
||||
cp input_test.txt build
|
||||
cd build && cmake .. && make && ./Day1
|
||||
27
day1/flake.lock
generated
Normal file
27
day1/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
day1/flake.nix
Normal file
58
day1/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
day1/input_test.txt
Normal file
10
day1/input_test.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
71
day1/src/main.cpp
Normal file
71
day1/src/main.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
struct Instruction {
|
||||
const char direction;
|
||||
const int value;
|
||||
};
|
||||
|
||||
std::vector<Instruction> parse_input_file(const std::string &path) {
|
||||
std::ifstream file;
|
||||
std::vector<Instruction> vec;
|
||||
std::string line;
|
||||
|
||||
file.open(path);
|
||||
if (file.is_open()) {
|
||||
while (std::getline(file, line)) {
|
||||
auto instr = line.at(0);
|
||||
int value = std::stoi(line.substr(1));
|
||||
vec.emplace_back(Instruction{instr, value});
|
||||
}
|
||||
file.close();
|
||||
} else {
|
||||
std::cout << "Failed to open file: " << path << std::endl;
|
||||
}
|
||||
|
||||
return vec;
|
||||
}
|
||||
|
||||
std::tuple<int, int> doit(const std::vector<Instruction> &instructions) {
|
||||
int dial_val = 50;
|
||||
int zero_counter_part1 = 0;
|
||||
int zero_counter_part2 = 0;
|
||||
for (const auto &instr : instructions) {
|
||||
for (int i = 0; i < instr.value; i++) {
|
||||
if (instr.direction == 'L') {
|
||||
dial_val--;
|
||||
} else {
|
||||
dial_val++;
|
||||
}
|
||||
|
||||
if (dial_val > 99)
|
||||
dial_val = 0;
|
||||
else if (dial_val < 0)
|
||||
dial_val = 99;
|
||||
if (dial_val == 0)
|
||||
zero_counter_part2 += 1;
|
||||
}
|
||||
if (dial_val == 0)
|
||||
zero_counter_part1 += 1;
|
||||
|
||||
std::cout << "dial_val: " << dial_val << std::endl;
|
||||
}
|
||||
return std::make_tuple(zero_counter_part1, zero_counter_part2);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cout << "One argument required: input txt" << std::endl;
|
||||
}
|
||||
|
||||
auto instructions = parse_input_file(argv[1]);
|
||||
|
||||
auto num_zeros = doit(instructions);
|
||||
|
||||
std::cout << "Number of times dial hits zero (part 1): "
|
||||
<< std::get<0>(num_zeros) << std::endl;
|
||||
std::cout << "Number of times dial hits zero (part 2): "
|
||||
<< std::get<1>(num_zeros) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user