commit dc599a59c59e0fa7bbadb17d9a15c5d526dc81e8
parent 93d505b108c52ec78824ff88c60c4960ed715e65
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Sun, 1 Dec 2019 23:39:29 -0500
Add Makefile and C++ solution
Diffstat:
2 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/day-01/Makefile b/day-01/Makefile
@@ -0,0 +1,13 @@
+CC := g++
+CCFLAGS := --std=c++17 -Wall
+
+all: day-01-cpp day-01-rust day-01.jl
+ julia day-01.jl
+ ./day-01-cpp
+ ./day-01-rust
+
+day-01-cpp: day-01.cc
+ $(CC) $(CCFLAGS) $^ -o $@
+
+day-01-rust: day-01.rs
+ rustc $^ -o $@
diff --git a/day-01/day-01.cc b/day-01/day-01.cc
@@ -0,0 +1,34 @@
+#include <fstream>
+#include <iostream>
+#include <numeric>
+#include <vector>
+
+auto part_1(std::vector<int>& masses) -> int {
+ return std::accumulate(
+ masses.begin(), masses.end(), 0,
+ [](auto acc, auto mass) { return acc + mass / 3 - 2; });
+}
+
+auto part_2(std::vector<int>& masses) -> int {
+ return std::accumulate(masses.begin(), masses.end(), 0,
+ [](auto acc, auto mass) {
+ while (mass > 0) {
+ mass = mass / 3 - 2;
+ acc += mass;
+ }
+ return acc;
+ });
+}
+
+auto main() -> int {
+ auto input_file = std::ifstream("input.txt");
+ auto input = std::vector<int>{};
+ int x;
+ while (input_file >> x) {
+ input.push_back(x);
+ }
+
+ std::cout << "C++17:\n"
+ << "Part 1: " << part_1(input) << "\nPart 2: " << part_2(input)
+ << std::endl;
+}