summaryrefslogtreecommitdiffstats
path: root/solutions/c
diff options
context:
space:
mode:
Diffstat (limited to 'solutions/c')
-rw-r--r--solutions/c/01-1.cpp8
-rw-r--r--solutions/c/01-2.cpp14
-rw-r--r--solutions/c/02-1.cpp47
3 files changed, 69 insertions, 0 deletions
diff --git a/solutions/c/01-1.cpp b/solutions/c/01-1.cpp
new file mode 100644
index 0000000..fcb593a
--- /dev/null
+++ b/solutions/c/01-1.cpp
@@ -0,0 +1,8 @@
+#include<iostream>
+
+int main() {
+ int mass, sum = 0;
+ while (std::cin >> mass) sum += (mass / 3) - 2;
+ std::cout << sum << std::endl;
+}
+
diff --git a/solutions/c/01-2.cpp b/solutions/c/01-2.cpp
new file mode 100644
index 0000000..7ce1a82
--- /dev/null
+++ b/solutions/c/01-2.cpp
@@ -0,0 +1,14 @@
+#include<iostream>
+
+int getFuel(int mass) {
+ int fuel = (mass / 3) - 2;
+ if (fuel <= 0) return 0;
+ return fuel + getFuel(fuel);
+}
+
+int main() {
+ int mass, sum = 0;
+ while (std::cin >> mass) sum += getFuel(mass);
+ std::cout << sum << std::endl;
+}
+
diff --git a/solutions/c/02-1.cpp b/solutions/c/02-1.cpp
new file mode 100644
index 0000000..b6db056
--- /dev/null
+++ b/solutions/c/02-1.cpp
@@ -0,0 +1,47 @@
+#include<fstream>
+#include<iostream>
+#include<vector>
+using namespace std;
+
+int main() {
+ ifstream inFile;
+ inFile.open("02.in");
+
+ vector<int> program;
+ int noun, verb;
+ // enter noun, verb
+ cin >> noun;
+ cin >> verb;
+
+ // read program
+ int n;
+ while (inFile >> n) program.push_back(n);
+ program[1] = noun;
+ program[2] = verb;
+ //cout << "Program: " << endl;
+ //dump(program);
+
+ // copy program to mem
+ vector<int> mem(program);
+
+ // calculate
+ int pointer = 0;
+ int op;
+ do {
+ //cout << "Pointer: " << pointer << endl;
+ //cout << "Memory: " << endl;
+ //dump(mem);
+ op = mem[pointer];
+ switch (op) {
+ case 1:
+ mem[mem[pointer+3]] = mem[mem[pointer+1]] + mem[mem[pointer+2]];
+ pointer += 4;
+ break;
+ case 2:
+ mem[mem[pointer+3]] = mem[mem[pointer+1]] * mem[mem[pointer+2]];
+ pointer += 4;
+ break;
+ }
+ } while (mem[pointer] != 99);
+ cout << mem[0] << endl;
+}