From 82409ba1df8b1c1b80f7f391797c56a93949d588 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Tue, 19 Dec 2023 14:32:26 -0500 Subject: [PATCH] C++ (yuck) --- cpp/.gitignore | 1 + cpp/2015/1/problem.cpp | 31 +++++++++++++++++++++++++++++++ cpp/bin/run | 6 ++++++ 3 files changed, 38 insertions(+) create mode 100644 cpp/.gitignore create mode 100644 cpp/2015/1/problem.cpp create mode 100755 cpp/bin/run diff --git a/cpp/.gitignore b/cpp/.gitignore new file mode 100644 index 0000000..44d4a10 --- /dev/null +++ b/cpp/.gitignore @@ -0,0 +1 @@ +problem \ No newline at end of file diff --git a/cpp/2015/1/problem.cpp b/cpp/2015/1/problem.cpp new file mode 100644 index 0000000..c33b548 --- /dev/null +++ b/cpp/2015/1/problem.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +int main() { + std::ifstream input_file ("../data/2015/1/input.txt"); + + int floor = 0; + while(input_file) { + char c = input_file.get(); + if (c == '(') floor++; + else if (c == ')') floor--; + } + + std::cout << "Part 1: " << floor << "\n"; + + input_file.clear(); + input_file.seekg(0); + + floor = 0; + int steps = 0; + while(input_file) { + char c = input_file.get(); + if (c == '(') floor++; + else if (c == ')') floor--; + steps++; + if (floor < 0) break; + } + + std::cout << "Part 2: " << steps << "\n"; +} diff --git a/cpp/bin/run b/cpp/bin/run new file mode 100755 index 0000000..2b9ad80 --- /dev/null +++ b/cpp/bin/run @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +year=$1 +day=$2 + +g++ -o $year/$day/problem $year/$day/problem.cpp && time ./$year/$day/problem