summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2019-12-04 07:02:31 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2019-12-04 07:02:31 +0100
commit831755fdc6d430bd8781da1897270cf2934bc858 (patch)
treef1d6e5609363f98534a87eb9203a7623fb409371
downloadaoc-831755fdc6d430bd8781da1897270cf2934bc858.tar.gz
Initial commit
-rw-r--r--.gitignore4
-rw-r--r--solutions/c/01-1.cpp8
-rw-r--r--solutions/c/01-2.cpp14
-rw-r--r--solutions/c/02-1.cpp47
-rw-r--r--solutions/py/01-1.py13
-rw-r--r--solutions/py/01-2.py16
-rw-r--r--solutions/py/02-1.py19
-rw-r--r--solutions/py/02-2.py25
-rw-r--r--solutions/py/03-1.py41
-rw-r--r--solutions/py/03-2.py46
10 files changed, 233 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e3f56d2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*.out
+*.pdf
+pdf.log
+__pycache__
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;
+}
diff --git a/solutions/py/01-1.py b/solutions/py/01-1.py
new file mode 100644
index 0000000..fa43b34
--- /dev/null
+++ b/solutions/py/01-1.py
@@ -0,0 +1,13 @@
+import math
+import sys
+
+s = 0
+for line in sys.stdin:
+ mass = int(line)
+ if mass == 0:
+ break
+ fuel = math.floor(mass / 3) - 2
+ s += fuel
+ print("adding", fuel)
+ print("at", s)
+print("sum", s)
diff --git a/solutions/py/01-2.py b/solutions/py/01-2.py
new file mode 100644
index 0000000..0017a8b
--- /dev/null
+++ b/solutions/py/01-2.py
@@ -0,0 +1,16 @@
+import math
+import sys
+
+def get_fuel(mass):
+ fuel = math.floor(mass / 3) - 2
+ if fuel <= 0:
+ return 0
+ return fuel + get_fuel(fuel)
+
+fuels = []
+for line in sys.stdin:
+ if line.rstrip() == "":
+ break
+ fuels.append(get_fuel(int(line)))
+
+print(sum(fuels))
diff --git a/solutions/py/02-1.py b/solutions/py/02-1.py
new file mode 100644
index 0000000..47ab15f
--- /dev/null
+++ b/solutions/py/02-1.py
@@ -0,0 +1,19 @@
+import sys
+
+program = [int(x) for x in input().split(",")]
+
+memory = program.copy()
+memory[1] = 12
+memory[2] = 2
+
+pointer = 0
+while True:
+ if memory[pointer] == 99:
+ break
+ elif memory[pointer] == 1:
+ memory[memory[pointer+3]] = memory[memory[pointer+1]] + memory[memory[pointer+2]]
+ elif memory[pointer] == 2:
+ memory[memory[pointer+3]] = memory[memory[pointer+1]] * memory[memory[pointer+2]]
+ pointer += 4
+print(memory[0])
+
diff --git a/solutions/py/02-2.py b/solutions/py/02-2.py
new file mode 100644
index 0000000..9a71a7f
--- /dev/null
+++ b/solutions/py/02-2.py
@@ -0,0 +1,25 @@
+import sys
+
+program = input().split(",")
+for i in range(len(program)):
+ program[i] = int(program[i])
+
+for n in range(100):
+ for v in range(100):
+ memory = program.copy()
+ memory[1] = n
+ memory[2] = v
+
+ pointer = 0
+ while True:
+ if memory[pointer] == 99:
+ break
+ elif memory[pointer] == 1:
+ memory[memory[pointer+3]] = memory[memory[pointer+1]] + memory[memory[pointer+2]]
+ elif memory[pointer] == 2:
+ memory[memory[pointer+3]] = memory[memory[pointer+1]] * memory[memory[pointer+2]]
+ pointer += 4
+ print(n, v, memory[0])
+ if memory[0] == 19690720:
+ sys.exit()
+
diff --git a/solutions/py/03-1.py b/solutions/py/03-1.py
new file mode 100644
index 0000000..dea1c05
--- /dev/null
+++ b/solutions/py/03-1.py
@@ -0,0 +1,41 @@
+import math
+
+
+def intersection(l1, l2):
+ tmp = set(l1)
+ return [value for value in l2 if value in tmp]
+
+def man_dist(point):
+ return abs(point[0]) + abs(point[1])
+
+
+wire1 = []
+wire2 = []
+for wire in (wire1, wire2):
+ x = 0
+ y = 0
+ dx = 0
+ dy = 0
+ for move in input().split(","):
+ if move[0] == "D":
+ dx = 0
+ dy = -1
+ elif move[0] == "U":
+ dx = 0
+ dy = 1
+ elif move[0] == "R":
+ dx = 1
+ dy = 0
+ elif move[0] == "L":
+ dx = -1
+ dy = 0
+ for i in range(int(move[1:])):
+ x += dx
+ y += dy
+ wire.append((x, y))
+points = intersection(wire1, wire2)
+print(len(points))
+dist = man_dist(points[0])
+for point in points[1:]:
+ dist = min(dist, man_dist(point))
+print(dist)
diff --git a/solutions/py/03-2.py b/solutions/py/03-2.py
new file mode 100644
index 0000000..adb95a2
--- /dev/null
+++ b/solutions/py/03-2.py
@@ -0,0 +1,46 @@
+import math
+
+def intersection(l1, l2):
+ tmp = set(l1)
+ return [value for value in l2 if value in tmp]
+
+def man_dist(point):
+ return abs(point[0]) + abs(point[1])
+
+
+wire1 = {}
+wire2 = {}
+# wire {(x,y) : dist}
+for wire in (wire1, wire2):
+ x = 0
+ y = 0
+ dx = 0
+ dy = 0
+ steps = 0
+ for move in input().split(","):
+ if move[0] == "D":
+ dx = 0
+ dy = -1
+ elif move[0] == "U":
+ dx = 0
+ dy = 1
+ elif move[0] == "R":
+ dx = 1
+ dy = 0
+ elif move[0] == "L":
+ dx = -1
+ dy = 0
+ for i in range(int(move[1:])):
+ x += dx
+ y += dy
+ steps += 1
+ wire[(x, y)] = steps
+
+points = intersection(list(wire1.keys()), list(wire2.keys()))
+print(len(points))
+for point in points:
+ print(point, wire1[point], wire2[point])
+length = wire1[points[0]] + wire2[points[0]]
+for point in points[1:]:
+ length = min(length, wire1[point] + wire2[point])
+print(length)