From 831755fdc6d430bd8781da1897270cf2934bc858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 4 Dec 2019 07:02:31 +0100 Subject: Initial commit --- solutions/c/01-1.cpp | 8 ++++++++ solutions/c/01-2.cpp | 14 ++++++++++++++ solutions/c/02-1.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 solutions/c/01-1.cpp create mode 100644 solutions/c/01-2.cpp create mode 100644 solutions/c/02-1.cpp (limited to 'solutions/c') 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 + +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 + +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 +#include +#include +using namespace std; + +int main() { + ifstream inFile; + inFile.open("02.in"); + + vector 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 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; +} -- cgit v1.2.1