about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-12-04 19:26:06 +0100
committerLeah Neukirchen <leah@vuxu.org>2017-12-04 19:26:06 +0100
commitfa3710ada8343210155caff4498ff80277ce1b45 (patch)
treeea152a6537fc25e4c4d5d73d8268bdb9bc12320f
parent49412504eaf8e10fbb1b9f43949a44f69ec199d6 (diff)
downloadadventofcode2017-fa3710ada8343210155caff4498ff80277ce1b45.tar.gz
adventofcode2017-fa3710ada8343210155caff4498ff80277ce1b45.tar.xz
adventofcode2017-fa3710ada8343210155caff4498ff80277ce1b45.zip
day03
-rw-r--r--day031
-rw-r--r--day03.cc76
-rw-r--r--day03.k4
3 files changed, 81 insertions, 0 deletions
diff --git a/day03 b/day03
new file mode 100644
index 0000000..9cf67fc
--- /dev/null
+++ b/day03
@@ -0,0 +1 @@
+368078
diff --git a/day03.cc b/day03.cc
new file mode 100644
index 0000000..efaffc0
--- /dev/null
+++ b/day03.cc
@@ -0,0 +1,76 @@
+#include <cmath>
+#include <iostream>
+#include <iterator>
+#include <utility>
+#include <vector>
+
+using namespace std;
+
+class SpiralIterator : public std::iterator<std::input_iterator_tag, int> {
+public:
+	pair<int, int> p;
+	int i, j, d;
+
+	SpiralIterator() : p{0,1}, i{2}, j{1}, d{-1}
+	{ }
+
+	auto begin() { return *this; }
+	auto end() { return SpiralIterator{}; }
+	bool operator!=(SpiralIterator &) const { return true; }   // infinite
+
+	auto operator*() { return p; }
+	auto operator++() {
+		switch (d) {
+		case 0: p.first--; break;
+		case 1: p.second--; break;
+		case 2: p.first++; break;
+		case 3: p.second++; break;
+		}
+
+		if (--j == 0) {
+			if (++d == 4) {
+				i += 2;
+				d = 0;
+			}
+
+			j = i;
+			if (d == 0) j--;
+			if (d == 3) j++;
+		}
+	}
+};
+
+int
+manhattan(pair<int, int> c)
+{
+	return abs(c.first) + abs(c.second);
+}
+
+int
+main() {
+	int i;
+	cin >> i;
+
+	// part 1
+	SpiralIterator p1;
+	advance(p1, i-1);
+	cout << manhattan(*p1) << endl;
+
+	// part 2
+	const int N = 20;
+	vector<vector<int>> m(N, vector<int>(N, 0));
+	m[N/2][N/2] = 1;
+
+	for (auto [x,y] : SpiralIterator{}) {
+		x += N/2; y += N/2;
+
+		m[x][y] = m[x+1][y+1] + m[x+1][y] + m[x+1][y-1] +
+		          m[ x ][y+1] +	            m[ x ][y-1] +
+		          m[x-1][y+1] + m[x-1][y] + m[x-1][y-1];
+
+		if (m[x][y] > i) {
+			cout << m[x][y] << endl;
+			break;
+		}
+	}
+}
diff --git a/day03.k b/day03.k
new file mode 100644
index 0000000..ba7f753
--- /dev/null
+++ b/day03.k
@@ -0,0 +1,4 @@
+d:.*0:`day03
+r+{x|-x}((r*2)!d-(2+r*r-1)-1)-r:_(1+%d-1)%2    / 371
+/ TODO part 2
+\\